Skip to content

Commit edfaf7f

Browse files
committed
Sema: Fix name lookup of nominal types nested inside a class from a protocol
1 parent 8c98b7a commit edfaf7f

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,13 @@ Type TypeChecker::resolveTypeInContext(
274274

275275
assert(foundDC);
276276

277+
// selfType is the self type of the context, unless the
278+
// context is a protocol type, in which case we might have
279+
// to use the existential type or superclass bound as a
280+
// parent type instead.
277281
Type selfType;
278-
if (isa<NominalTypeDecl>(typeDecl)) {
282+
if (isa<NominalTypeDecl>(typeDecl) &&
283+
typeDecl->getDeclContext()->getAsProtocolOrProtocolExtensionContext()) {
279284
// When looking up a nominal type declaration inside of a
280285
// protocol extension, always use the nominal type and
281286
// not the protocol 'Self' type.
@@ -292,7 +297,8 @@ Type TypeChecker::resolveTypeInContext(
292297

293298
if (selfType->is<GenericTypeParamType>() &&
294299
typeDecl->getDeclContext()->getAsClassOrClassExtensionContext()) {
295-
// We found a member of a class from a protocol extension.
300+
// We found a member of a class from a protocol or protocol
301+
// extension.
296302
//
297303
// Get the superclass of the 'Self' type parameter.
298304
auto *sig = foundDC->getGenericSignatureOfContext();

test/decl/ext/protocol.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ protocol ConformedProtocol {
237237
class BaseWithAlias<T> : ConformedProtocol {
238238
typealias ConcreteAlias = T
239239

240+
struct NestedNominal {}
241+
240242
func baseMethod(_: T) {}
241243
}
242244

@@ -261,6 +263,8 @@ extension ExtendedProtocol where Self : DerivedWithAlias {
261263
func f3(x: AbstractConformanceAlias) {
262264
let _: DerivedWithAlias = x
263265
}
266+
267+
func f4(x: NestedNominal) {}
264268
}
265269

266270
// ----------------------------------------------------------------------------

validation-test/compiler_crashers/28825-isa-classdecl-nominaldecl-expected-a-class-here.swift renamed to validation-test/compiler_crashers_fixed/28825-isa-classdecl-nominaldecl-expected-a-class-here.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

88
// REQUIRES: asserts
9-
// RUN: not --crash %target-swift-frontend %s -emit-ir
9+
// RUN: not %target-swift-frontend %s -emit-ir
1010
protocol b:a{init(t:a}class a{class a

0 commit comments

Comments
 (0)