Skip to content

Commit 9a64e50

Browse files
committed
Sema: Fix type resolution with invalid nested protocol
If you have a class with an (invalid) protocol nested inside: class OuterGenericClass<T, U> { protocol InnerProtocol : OuterGenericClass {} } The reference to 'OuterGenericClass' in the protocol's inheritance clause is not "in context", and should not have inferred generic arguments <T, U>, because the protocol does not inherit the generic arguments of the outer scope. Previously, this would trigger an assertion in the requirement machine, because the generic parameter τ_0_1 is not a valid generic parameter in the protocol's generic signature.
1 parent 5790c70 commit 9a64e50

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ Type TypeResolution::resolveTypeInContext(TypeDecl *typeDecl,
401401
// type within the context.
402402
if (auto *nominalType = dyn_cast<NominalTypeDecl>(typeDecl)) {
403403
for (auto *parentDC = fromDC; !parentDC->isModuleScopeContext();
404-
parentDC = parentDC->getParent()) {
404+
parentDC = parentDC->getParentForLookup()) {
405405
auto *parentNominal = parentDC->getSelfNominalTypeDecl();
406406
if (parentNominal == nominalType)
407407
return mapTypeIntoContext(parentDC->getDeclaredInterfaceType());
@@ -421,7 +421,7 @@ Type TypeResolution::resolveTypeInContext(TypeDecl *typeDecl,
421421
// referenced without generic arguments as well.
422422
if (auto *aliasDecl = dyn_cast<TypeAliasDecl>(typeDecl)) {
423423
for (auto *parentDC = fromDC; !parentDC->isModuleScopeContext();
424-
parentDC = parentDC->getParent()) {
424+
parentDC = parentDC->getParentForLookup()) {
425425
if (auto *ext = dyn_cast<ExtensionDecl>(parentDC)) {
426426
auto extendedType = ext->getExtendedType();
427427
if (auto *unboundGeneric =

test/decl/nested/protocol.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,14 @@ class OuterClass {
6868
// expected-error@-1{{protocol 'InnerProtocol' cannot be nested inside another declaration}}
6969
}
7070

71-
class OtherGenericClass<T> {
71+
// 'InnerProtocol' does not inherit the generic parameters of
72+
// 'OtherGenericClass', so the occurrence of 'OtherGenericClass'
73+
// in 'InnerProtocol' is not "in context" with implicitly
74+
// inferred generic arguments <T, U>.
75+
class OtherGenericClass<T, U> { // expected-note {{generic type 'OtherGenericClass' declared here}}
7276
protocol InnerProtocol : OtherGenericClass { }
7377
// expected-error@-1{{protocol 'InnerProtocol' cannot be nested inside another declaration}}
74-
// expected-error@-2{{superclass constraint 'Self' : 'OtherGenericClass<Self>' is recursive}}
78+
// expected-error@-2{{reference to generic type 'OtherGenericClass' requires arguments in <...>}}
7579
}
7680

7781
protocol SelfDotTest {

0 commit comments

Comments
 (0)