Skip to content

Commit 435f623

Browse files
committed
AST: Resolve types when computing inherited conformances for classes.
Previously, conformances inherited through a base class could be missed in lazy typechecking mode if types in the inheritance clause were not already resolved.
1 parent fd3714e commit 435f623

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed

lib/AST/ConformanceLookupTable.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,18 @@ void ConformanceLookupTable::inheritConformances(ClassDecl *classDecl,
226226
if (superclassLoc.isValid())
227227
return superclassLoc;
228228

229-
for (const auto &inherited : classDecl->getInherited().getEntries()) {
230-
if (auto inheritedType = inherited.getType()) {
229+
auto inheritedTypes = classDecl->getInherited();
230+
for (unsigned i : inheritedTypes.getIndices()) {
231+
if (auto inheritedType = inheritedTypes.getResolvedType(i)) {
232+
231233
if (inheritedType->getClassOrBoundGenericClass()) {
232-
superclassLoc = inherited.getSourceRange().Start;
234+
superclassLoc = inheritedTypes.getEntry(i).getSourceRange().Start;
233235
return superclassLoc;
234236
}
235237
if (inheritedType->isExistentialType()) {
236238
auto layout = inheritedType->getExistentialLayout();
237239
if (layout.explicitSuperclass) {
238-
superclassLoc = inherited.getSourceRange().Start;
240+
superclassLoc = inheritedTypes.getEntry(i).getSourceRange().Start;
239241
return superclassLoc;
240242
}
241243
}

test/Inputs/lazy_typecheck.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ public class PublicClass {
115115
// class func internalClassMethod() -> DoesNotExist {}
116116
}
117117

118+
public class PublicDerivedClass: PublicClass {}
119+
118120
class InternalClass: DoesNotExist { // expected-error {{cannot find type 'DoesNotExist' in scope}}
119121
init(x: DoesNotExist) {} // expected-error {{cannot find type 'DoesNotExist' in scope}}
120122
}
@@ -135,6 +137,8 @@ public class PublicClassConformingToPublicProto: PublicProto {
135137
}
136138
}
137139

140+
public class PublicClassInheritingConformanceToPublicProto: PublicClassConformingToPublicProto {}
141+
138142
extension String: PublicProto {
139143
public func req() -> Int {
140144
return true // expected-error {{cannot convert return expression of type 'Bool' to return type 'Int'}}

test/Inputs/lazy_typecheck_client.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,21 @@ func testPublicClass() {
3030
let c = PublicClass(x: 2)
3131
_ = c.publicMethod()
3232
PublicClass.publicClassMethod()
33+
34+
let d = PublicDerivedClass(x: 3)
35+
_ = d.publicMethod()
36+
PublicDerivedClass.publicClassMethod()
3337
}
3438

3539
func testConformances() {
36-
let _: [any PublicProto] = [
40+
let array: [any PublicProto] = [
3741
PublicStructConformingToPublicProto(),
3842
PublicClassConformingToPublicProto(),
3943
"string",
44+
PublicClassInheritingConformanceToPublicProto(),
4045
]
46+
47+
for x in array {
48+
_ = x.req()
49+
}
4150
}

test/ModuleInterface/lazy-typecheck.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
// CHECK: public class func publicClassMethod()
4646
// CHECK: deinit
4747
// CHECK: }
48+
// CHECK: @_inheritsConvenienceInitializers public class PublicDerivedClass : lazy_typecheck.PublicClass {
49+
// CHECK: override public init(x: Swift.Int)
50+
// CHECK: deinit
51+
// CHECK: }
4852
// CHECK: public struct PublicStructConformingToPublicProto : lazy_typecheck.PublicProto {
4953
// CHECK: public init()
5054
// CHECK: public func req() -> Swift.Int
@@ -54,6 +58,10 @@
5458
// CHECK: public func req() -> Swift.Int
5559
// CHECK: deinit
5660
// CHECK: }
61+
// CHECK: @_inheritsConvenienceInitializers public class PublicClassInheritingConformanceToPublicProto : lazy_typecheck.PublicClassConformingToPublicProto {
62+
// CHECK: override public init()
63+
// CHECK: deinit
64+
// CHECK: }
5765
// CHECK: extension Swift.String : lazy_typecheck.PublicProto {
5866
// CHECK: public func req() -> Swift.Int
5967
// CHECK: }

test/TBD/lazy-typecheck.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ exports:
4040
'_$s14lazy_typecheck024PublicStructConformingToC5ProtoVMa',
4141
'_$s14lazy_typecheck024PublicStructConformingToC5ProtoVMn',
4242
'_$s14lazy_typecheck024PublicStructConformingToC5ProtoVN',
43+
'_$s14lazy_typecheck034PublicClassInheritingConformanceToC5ProtoCACycfC',
44+
'_$s14lazy_typecheck034PublicClassInheritingConformanceToC5ProtoCACycfc',
45+
'_$s14lazy_typecheck034PublicClassInheritingConformanceToC5ProtoCMa',
46+
'_$s14lazy_typecheck034PublicClassInheritingConformanceToC5ProtoCMm',
47+
'_$s14lazy_typecheck034PublicClassInheritingConformanceToC5ProtoCMn',
48+
'_$s14lazy_typecheck034PublicClassInheritingConformanceToC5ProtoCMo',
49+
'_$s14lazy_typecheck034PublicClassInheritingConformanceToC5ProtoCN',
50+
'_$s14lazy_typecheck034PublicClassInheritingConformanceToC5ProtoCfD',
51+
'_$s14lazy_typecheck034PublicClassInheritingConformanceToC5ProtoCfd',
4352
'_$s14lazy_typecheck10publicFuncSiyF', '_$s14lazy_typecheck11PublicClassC06publicD6MethodyyFZTj',
4453
'_$s14lazy_typecheck11PublicClassC06publicD6MethodyyFZTq',
4554
'_$s14lazy_typecheck11PublicClassC12publicMethodSiyFTj', '_$s14lazy_typecheck11PublicClassC12publicMethodSiyFTq',
@@ -55,10 +64,14 @@ exports:
5564
'_$s14lazy_typecheck12PublicStructV18publicStaticMethodyyFZ',
5665
'_$s14lazy_typecheck12PublicStructV1xACSi_tcfC', '_$s14lazy_typecheck12PublicStructVMa',
5766
'_$s14lazy_typecheck12PublicStructVMn', '_$s14lazy_typecheck12PublicStructVN',
58-
'_$s14lazy_typecheck13inlinableFuncSiyF', '_$s14lazy_typecheck19PublicRethrowsProtoMp',
59-
'_$s14lazy_typecheck19PublicRethrowsProtoP3reqSiyKFTj', '_$s14lazy_typecheck19PublicRethrowsProtoP3reqSiyKFTq',
60-
'_$s14lazy_typecheck19PublicRethrowsProtoTL', '_$s14lazy_typecheck24publicFuncWithDefaultArgyS2iF',
61-
'_$s14lazy_typecheck30publicFuncWithOpaqueReturnTypeQryF',
67+
'_$s14lazy_typecheck13inlinableFuncSiyF', '_$s14lazy_typecheck18PublicDerivedClassC1xACSi_tcfC',
68+
'_$s14lazy_typecheck18PublicDerivedClassC1xACSi_tcfc', '_$s14lazy_typecheck18PublicDerivedClassCMa',
69+
'_$s14lazy_typecheck18PublicDerivedClassCMm', '_$s14lazy_typecheck18PublicDerivedClassCMn',
70+
'_$s14lazy_typecheck18PublicDerivedClassCMo', '_$s14lazy_typecheck18PublicDerivedClassCN',
71+
'_$s14lazy_typecheck18PublicDerivedClassCfD', '_$s14lazy_typecheck18PublicDerivedClassCfd',
72+
'_$s14lazy_typecheck19PublicRethrowsProtoMp', '_$s14lazy_typecheck19PublicRethrowsProtoP3reqSiyKFTj',
73+
'_$s14lazy_typecheck19PublicRethrowsProtoP3reqSiyKFTq', '_$s14lazy_typecheck19PublicRethrowsProtoTL',
74+
'_$s14lazy_typecheck24publicFuncWithDefaultArgyS2iF', '_$s14lazy_typecheck30publicFuncWithOpaqueReturnTypeQryF',
6275
'_$s14lazy_typecheck30publicFuncWithOpaqueReturnTypeQryFQOMQ',
6376
'_$s14lazy_typecheck32constrainedGenericPublicFunctionyyxAA0E5ProtoRzlF',
6477
'_$sSS14lazy_typecheck11PublicProtoAAMc', '_$sSS14lazy_typecheck11PublicProtoAAWP',

0 commit comments

Comments
 (0)