Skip to content

RequirementMachine: Concrete contraction needs to substitute the parent type of a subject type sometimes [5.7] #59325

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
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
19 changes: 11 additions & 8 deletions lib/AST/RequirementMachine/ConcreteContraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ class ConcreteContraction {
Optional<Type> ConcreteContraction::substTypeParameterRec(
Type type, Position position) const {

// If the requirement is of the form 'T == C' or 'T : C', don't
// substitute T, since then we end up with 'C == C' or 'C : C',
// If we have a superclass (T : C) or same-type requirement (T == C),
// don't substitute T, since then we end up with 'C == C' or 'C : C',
// losing the requirement.
if (position == Position::BaseType ||
position == Position::ConformanceRequirement) {
Expand Down Expand Up @@ -399,9 +399,10 @@ ConcreteContraction::substRequirement(const Requirement &req) const {
!module->lookupConformance(substFirstType, proto,
allowMissing, allowUnavailable)) {
// Handle the case of <T where T : P, T : C> where C is a class and
// C does not conform to P by leaving the conformance requirement
// unsubstituted.
return req;
// C does not conform to P and only substitute the parent type of T
// by pretending we have a same-type requirement here.
substFirstType = substTypeParameter(
firstType, Position::SameTypeRequirement);
}

// Otherwise, replace the generic parameter in the conformance
Expand All @@ -418,9 +419,11 @@ ConcreteContraction::substRequirement(const Requirement &req) const {
if (!substFirstType->isTypeParameter() &&
!substFirstType->satisfiesClassConstraint() &&
req.getLayoutConstraint()->isClass()) {
// If the concrete type doesn't satisfy the layout constraint,
// leave it unsubstituted so that we produce a better diagnostic.
return req;
// If the concrete type doesn't satisfy the layout constraint, produce
// a better diagnostic and only substitute the parent type by pretending
// we have a same-type requirement here.
substFirstType = substTypeParameter(
firstType, Position::SameTypeRequirement);
}

return Requirement(req.getKind(),
Expand Down
1 change: 1 addition & 0 deletions lib/AST/RequirementMachine/MinimalConformances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ MinimalConformances::decomposeTermIntoConformanceRuleLeftHandSides(
bool simplified = System.simplify(term, &steps);
if (!simplified) {
llvm::errs() << "Term does not conform to protocol: " << term << "\n";
System.dump(llvm::errs());
abort();
}

Expand Down
41 changes: 26 additions & 15 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,21 +242,8 @@ bool TypeResolution::areSameType(Type type1, Type type2) const {
}
}

// Otherwise, perform a structural check.
assert(stage == TypeResolutionStage::Structural);

// FIXME: We should be performing a deeper equality check here.
// If both refer to associated types with the same name, they'll implicitly
// be considered equivalent.
auto depMem1 = type1->getAs<DependentMemberType>();
if (!depMem1) return false;

auto depMem2 = type2->getAs<DependentMemberType>();
if (!depMem2) return false;

if (depMem1->getName() != depMem2->getName()) return false;

return areSameType(depMem1->getBase(), depMem2->getBase());
return false;
}

Type TypeChecker::getOptionalType(SourceLoc loc, Type elementType) {
Expand Down Expand Up @@ -430,7 +417,7 @@ Type TypeResolution::resolveTypeInContext(TypeDecl *typeDecl,
selfType = foundDC->getSelfInterfaceType();

if (selfType->is<GenericTypeParamType>()) {
if (typeDecl->getDeclContext()->getSelfProtocolDecl()) {
if (isa<ProtocolDecl>(typeDecl->getDeclContext())) {
if (isa<AssociatedTypeDecl>(typeDecl) ||
(isa<TypeAliasDecl>(typeDecl) &&
!cast<TypeAliasDecl>(typeDecl)->isGeneric() &&
Expand Down Expand Up @@ -1434,6 +1421,23 @@ static Type resolveTopLevelIdentTypeComponent(TypeResolution resolution,
auto globals = TypeChecker::lookupUnqualifiedType(DC, id, comp->getLoc(),
lookupOptions);

// If we're doing structural resolution and one of the results is an
// associated type, ignore any other results found from the same
// DeclContext; they are going to be protocol typealiases, possibly
// from constrained extensions, and trying to compute their type in
// resolveTypeInContext() might hit request cycles since structural
// resolution is performed while computing the requirement signature
// of the protocol.
DeclContext *assocTypeDC = nullptr;
if (resolution.getStage() == TypeResolutionStage::Structural) {
for (const auto &entry : globals) {
if (isa<AssociatedTypeDecl>(entry.getValueDecl())) {
assocTypeDC = entry.getDeclContext();
break;
}
}
}

// Process the names we found.
Type current;
TypeDecl *currentDecl = nullptr;
Expand All @@ -1443,6 +1447,13 @@ static Type resolveTopLevelIdentTypeComponent(TypeResolution resolution,
auto *foundDC = entry.getDeclContext();
auto *typeDecl = cast<TypeDecl>(entry.getValueDecl());

// See the comment above.
if (assocTypeDC != nullptr &&
foundDC == assocTypeDC && !isa<AssociatedTypeDecl>(typeDecl))
continue;

// Compute the type of the found declaration when referenced from this
// location.
Type type = resolveTypeDecl(typeDecl, foundDC, resolution, silParams, comp);
if (type->is<ErrorType>())
return type;
Expand Down
29 changes: 29 additions & 0 deletions test/Generics/rdar94150249.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: %target-swift-frontend -typecheck %s -debug-generic-signatures 2>&1 | %FileCheck %s

protocol P1 {
associatedtype Value
}

protocol P2 {
associatedtype Value
}

class G<Value> : P2 {}

protocol P3 {}

class C {}

extension P1 where Value: P2 {
typealias Element = Value.Value

// Make sure we can resolve 'Element' to 'V' on the left hand side of 'Element: P3'.

// CHECK-LABEL: .P1 extension.set()@
// CHECK-NEXT: Generic signature: <Self, V where Self : P1, V : C, V : P3, Self.[P1]Value == G<V>>
func set<V>() where Element: P3 & C, Value == G<V> {
takeP3(V.self)
}
}

func takeP3<T : P3>(_: T.Type) {}