Skip to content

[Sema][GSB] Fix crash on cond conformances with invalid req #17284

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

Closed
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: 14 additions & 5 deletions lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4721,12 +4721,21 @@ ConstraintResult GenericSignatureBuilder::addTypeRequirement(
// type that makes sense to use here, but, in practice, all
// getLookupConformanceFns used in here don't use that parameter anyway.
auto dependentType = CanType();
auto conformance =
getLookupConformanceFn()(dependentType, subjectType, proto->getDecl());

// FIXME: diagnose if there's no conformance.
if (conformance) {
addConditionalRequirements(*this, *conformance, inferForModule);
auto D = getGenericParams().front()->getDecl();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I think this could do with some explanation: is this new code trying to deduce if we're in a circular situation, where the requirement is referring to the "original" type (the one for which we're building the generic signature)? And these few lines are trying to work out that original type?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I am proceeding with the conformance lookup if the subject type has a null decl context or isn't a nominal type declaration context, or otherwise isn't the type we're extending or declaring. Simply put, I am just making sure not to look up conformances if the base type of the subject and the current context match.

auto DC = D ? D->getDeclContext() : nullptr;
auto NTD = DC ? DC->getAsNominalTypeOrNominalTypeExtensionContext()
: nullptr;
auto subjectNTD = subjectType->getAnyNominal();

if (!NTD || !subjectNTD || NTD != subjectNTD) {
auto conformance =
getLookupConformanceFn()(dependentType, subjectType,
proto->getDecl());
// FIXME: diagnose if there's no conformance.
if (conformance) {
addConditionalRequirements(*this, *conformance, inferForModule);
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/decl/ext/generic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,23 @@ extension GenericClass where Self : P3 { }
// expected-error@-1{{'Self' is only available in a protocol or as the result of a method in a class; did you mean 'GenericClass'?}} {{30-34=GenericClass}}
// expected-error@-2{{type 'GenericClass<T>' in conformance requirement does not refer to a generic parameter or associated type}}

protocol Prot1 {}
protocol Prot2 {}
protocol Prot3 {}
protocol Prot4 {}
protocol Prot5 {}

extension GenericClass: Prot1 where T: Prot1 {}
extension GenericClass: Prot2 where Self: Prot1 {}
// expected-error@-1 {{'Self' is only available in a protocol or as the result of a method in a class; did you mean 'GenericClass'?}}
// expected-error@-2 {{type 'GenericClass<T>' in conformance requirement does not refer to a generic parameter or associated type}}
extension GenericClass: Prot3 where GenericClass<T>: Prot1 {}
// expected-error @-1 {{type 'GenericClass<T>' in conformance requirement does not refer to a generic parameter or associated type}}
extension GenericClass: Prot4 where GenericClass<Int>: Prot1 {}
// expected-error @-1 {{type 'GenericClass<Int>' in conformance requirement does not refer to a generic parameter or associated type}}
extension GenericClass: Prot5 where GenericClass: Prot5 {}
// expected-error @-1 {{type 'GenericClass<T>' in conformance requirement does not refer to a generic parameter or associated type}}

protocol P4 {
associatedtype T
init(_: T)
Expand Down