Skip to content

Commit ef86409

Browse files
committed
Sema: Fix crash when accessing nominal member of protocol
1 parent 4fa4133 commit ef86409

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3035,16 +3035,22 @@ Type TypeChecker::substMemberTypeWithBase(ModuleDecl *module,
30353035
// The declared interface type for a generic type will have the type
30363036
// arguments; strip them off.
30373037
if (auto *nominalDecl = dyn_cast<NominalTypeDecl>(member)) {
3038+
// If the base type is not a nominal type, we might be looking up a
3039+
// nominal member of a generic parameter. This is not supported right
3040+
// now, but at least don't crash.
3041+
if (member->getDeclContext()->getAsProtocolOrProtocolExtensionContext())
3042+
return nominalDecl->getDeclaredType();
3043+
30383044
if (!isa<ProtocolDecl>(nominalDecl) &&
30393045
nominalDecl->getGenericParams()) {
30403046
return UnboundGenericType::get(
30413047
nominalDecl, baseTy,
30423048
nominalDecl->getASTContext());
3043-
} else {
3044-
return NominalType::get(
3045-
nominalDecl, baseTy,
3046-
nominalDecl->getASTContext());
30473049
}
3050+
3051+
return NominalType::get(
3052+
nominalDecl, baseTy,
3053+
nominalDecl->getASTContext());
30483054
}
30493055

30503056
auto *aliasDecl = dyn_cast<TypeAliasDecl>(member);

test/decl/nested/protocol.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,9 @@ class OtherGenericClass<T> {
7373
protocol InnerProtocol : OtherGenericClass { }
7474
// expected-error@-1{{protocol 'InnerProtocol' cannot be nested inside another declaration}}
7575
}
76+
77+
protocol SelfDotTest {
78+
func f(_: Self.Class)
79+
class Class {}
80+
// expected-error@-1{{type 'Class' cannot be nested in protocol 'SelfDotTest'}}
81+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// REQUIRES: asserts
9-
// RUN: not --crash %target-swift-frontend %s -emit-ir
8+
9+
// RUN: not %target-swift-frontend %s -emit-ir
1010
protocol P{func a:Self.a.a{}class a{class a

validation-test/compiler_crashers/28849-hasval.swift renamed to validation-test/compiler_crashers_fixed/28849-hasval.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// REQUIRES: asserts
9-
// RUN: not --crash %target-swift-frontend %s -emit-ir
8+
9+
// RUN: not %target-swift-frontend %s -emit-ir
1010
protocol A:P{}
1111
protocol P{{}typealias a:A
1212
class a:A

0 commit comments

Comments
 (0)