Skip to content

Tiny RequirementMachine fixes #38717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,11 @@ class ASTContext final {
rewriting::RequirementMachine *getOrCreateRequirementMachine(
CanGenericSignature sig);

/// This is a hack to break cycles. Don't introduce new callers of this
/// method.
bool isRecursivelyConstructingRequirementMachine(
CanGenericSignature sig);

/// Retrieve a generic signature with a single unconstrained type parameter,
/// like `<T>`.
CanGenericSignature getSingleGenericParameterSignature() const;
Expand Down
16 changes: 16 additions & 0 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1997,6 +1997,22 @@ ASTContext::getOrCreateRequirementMachine(CanGenericSignature sig) {
return machine;
}

bool ASTContext::isRecursivelyConstructingRequirementMachine(
CanGenericSignature sig) {
auto &rewriteCtx = getImpl().TheRewriteContext;
if (!rewriteCtx)
return false;

auto arena = getArena(sig);
auto &machines = getImpl().getArena(arena).RequirementMachines;

auto found = machines.find(sig);
if (found == machines.end())
return false;

return !found->second->isComplete();
}

Optional<llvm::TinyPtrVector<ValueDecl *>>
OverriddenDeclsRequest::getCachedResult() const {
auto decl = std::get<0>(getStorage());
Expand Down
33 changes: 28 additions & 5 deletions lib/AST/GenericSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,22 +349,36 @@ GenericSignatureImpl::getLocalRequirements(Type depType) const {
auto rqmResult = computeViaRQM();
auto gsbResult = computeViaGSB();

auto typesEqual = [&](Type lhs, Type rhs) {
auto typesEqual = [&](Type lhs, Type rhs, bool canonical) {
if (!lhs || !rhs)
return !lhs == !rhs;
return lhs->isEqual(rhs);
if (lhs->isEqual(rhs))
return true;

if (canonical)
return false;

if (getCanonicalTypeInContext(lhs) ==
getCanonicalTypeInContext(rhs))
return true;

return false;
};

auto compare = [&]() {
// If the types are concrete, we don't care about the rest.
if (gsbResult.concreteType || rqmResult.concreteType) {
if (!typesEqual(gsbResult.concreteType, rqmResult.concreteType))
if (!typesEqual(gsbResult.concreteType,
rqmResult.concreteType,
false))
return false;

return true;
}

if (!typesEqual(gsbResult.anchor, rqmResult.anchor))
if (!typesEqual(gsbResult.anchor,
rqmResult.anchor,
true))
return false;

if (gsbResult.layout != rqmResult.layout)
Expand All @@ -378,6 +392,11 @@ GenericSignatureImpl::getLocalRequirements(Type depType) const {
if (lhsProtos != rhsProtos)
return false;

if (!typesEqual(gsbResult.superclass,
rqmResult.superclass,
false))
return false;

return true;
};

Expand Down Expand Up @@ -770,7 +789,11 @@ Type GenericSignatureImpl::getConcreteType(Type type) const {
auto check = [&]() {
if (!gsbResult || !rqmResult)
return !gsbResult == !rqmResult;
return gsbResult->isEqual(rqmResult);
if (gsbResult->isEqual(rqmResult))
return true;

return (getCanonicalTypeInContext(gsbResult)
== getCanonicalTypeInContext(rqmResult));
};

if (!check()) {
Expand Down
11 changes: 8 additions & 3 deletions lib/Sema/TypeCheckProtocolInference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,9 +819,14 @@ Type AssociatedTypeInference::computeFixedTypeWitness(
!conformedProto->inheritsFrom(assocType->getProtocol()))
continue;

const auto ty =
conformedProto->getGenericSignature()->getCanonicalTypeInContext(
structuralTy);
auto sig = conformedProto->getGenericSignature();

// FIXME: The RequirementMachine will assert on re-entrant construction.
// We should find a more principled way of breaking this cycle.
if (ctx.isRecursivelyConstructingRequirementMachine(sig.getCanonicalSignature()))
continue;

const auto ty = sig->getCanonicalTypeInContext(structuralTy);

// A dependent member type with an identical base and name indicates that
// the protocol does not same-type constrain it in any way; move on to
Expand Down