Skip to content

Commit 1b3d9d8

Browse files
authored
Merge pull request #68262 from tshortli/emit-module-lazy-typecheck-conformances
Serialization: Don't serialize conformances that should be skipped
2 parents fc640db + fde2016 commit 1b3d9d8

File tree

7 files changed

+103
-12
lines changed

7 files changed

+103
-12
lines changed

lib/SIL/IR/SILSymbolVisitor.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ class SILSymbolVisitorImpl : public ASTVisitor<SILSymbolVisitorImpl> {
287287
rootConformance->forEachValueWitness([&](ValueDecl *valueReq,
288288
Witness witness) {
289289
auto witnessDecl = witness.getDecl();
290+
if (!witnessDecl)
291+
return;
292+
290293
if (isa<AbstractFunctionDecl>(valueReq)) {
291294
addSymbolIfNecessary(valueReq, witnessDecl);
292295
} else if (auto *storage = dyn_cast<AbstractStorageDecl>(valueReq)) {

lib/Serialization/Serialization.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,8 +1665,6 @@ void Serializer::writeLocalNormalProtocolConformance(
16651665

16661666
PrettyStackTraceConformance trace("serializing", conformance);
16671667

1668-
// The conformance must be complete, or we can't serialize it.
1669-
assert(conformance->isComplete() || allowCompilerErrors());
16701668
assert(ConformancesToSerialize.hasRef(conformance));
16711669

16721670
auto protocol = conformance->getProtocol();
@@ -1688,7 +1686,7 @@ void Serializer::writeLocalNormalProtocolConformance(
16881686
data.push_back(addDeclRef(typeDecl, /*allowTypeAliasXRef*/true));
16891687
++numTypeWitnesses;
16901688
return false;
1691-
});
1689+
}, /*useResolver=*/true);
16921690

16931691
conformance->forEachValueWitness([&](ValueDecl *req, Witness witness) {
16941692
PrettyStackTraceDecl traceValueWitness(
@@ -1717,7 +1715,7 @@ void Serializer::writeLocalNormalProtocolConformance(
17171715

17181716
data.push_back(addSubstitutionMapRef(subs));
17191717
data.push_back(witness.getEnterIsolation().has_value() ? 1 : 0);
1720-
});
1718+
}, /*useResolver=*/true);
17211719

17221720
unsigned abbrCode
17231721
= DeclTypeAbbrCodes[NormalProtocolConformanceLayout::Code];
@@ -3261,8 +3259,14 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
32613259
size_t addConformances(const IterableDeclContext *declContext,
32623260
ConformanceLookupKind lookupKind,
32633261
SmallVectorImpl<TypeID> &data) {
3262+
// We don't expect to be serializing conformances for skipped decls.
3263+
assert(!S.shouldSkipDecl(declContext->getDecl()));
3264+
32643265
size_t count = 0;
32653266
for (auto conformance : declContext->getLocalConformances(lookupKind)) {
3267+
if (S.shouldSkipDecl(conformance->getProtocol()))
3268+
continue;
3269+
32663270
data.push_back(S.addConformanceRef(conformance));
32673271
count++;
32683272
}

test/Inputs/lazy_typecheck.swift

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ public func publicFuncWithOpaqueReturnType() -> some PublicProto { // expected-n
5050
// MARK: - Nominal types
5151

5252
public protocol PublicProto {
53-
func req() -> Int
53+
func req() -> Int // expected-note 2 {{protocol requires function 'req()' with type '() -> Int'; add a stub for conformance}}
5454
}
5555

5656
protocol InternalProto {
57-
// FIXME: Serialization causes typechecking of protocols regardless of access level
58-
// func req() -> DoesNotExist
57+
func goodReq() -> Int // expected-note {{protocol requires function 'goodReq()' with type '() -> Int'; add a stub for conformance}}
58+
func badReq() -> DoesNotExist // expected-error {{cannot find type 'DoesNotExist' in scope}}
5959
}
6060

6161
public struct PublicStruct {
@@ -114,6 +114,37 @@ public class PublicClass {
114114
class InternalClass: DoesNotExist { // expected-error {{cannot find type 'DoesNotExist' in scope}}
115115
init(x: DoesNotExist) {} // expected-error {{cannot find type 'DoesNotExist' in scope}}
116116
}
117+
118+
// MARK: - Conformances
119+
120+
public struct PublicStructConformingToPublicProto: PublicProto {
121+
public init() {}
122+
public func req() -> Int {
123+
return true // expected-error {{cannot convert return expression of type 'Bool' to return type 'Int'}}
124+
}
125+
}
126+
127+
public class PublicClassConformingToPublicProto: PublicProto {
128+
public init() {}
129+
public func req() -> Int {
130+
return true // expected-error {{cannot convert return expression of type 'Bool' to return type 'Int'}}
131+
}
132+
}
133+
134+
extension String: PublicProto {
135+
public func req() -> Int {
136+
return true // expected-error {{cannot convert return expression of type 'Bool' to return type 'Int'}}
137+
}
138+
}
139+
140+
struct InternalStructConformingToPublicProto: PublicProto { // expected-error {{type 'InternalStructConformingToPublicProto' does not conform to protocol 'PublicProto'}}
141+
}
142+
143+
extension InternalStruct: PublicProto { // expected-error {{type 'InternalStruct' does not conform to protocol 'PublicProto'}}
144+
}
145+
146+
struct InternalStructConformingToInternalProto: InternalProto { // expected-error {{type 'InternalStructConformingToInternalProto' does not conform to protocol 'InternalProto'}}
147+
}
148+
117149
// FIXME: Test enums
118-
// FIXME: Test conformances
119150
// FIXME: Test global vars

test/Inputs/lazy_typecheck_client.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ func testPublicClass() {
3131
_ = c.publicMethod()
3232
PublicClass.publicClassMethod()
3333
}
34+
35+
func testConformances() {
36+
let _: [any PublicProto] = [
37+
PublicStructConformingToPublicProto(),
38+
PublicClassConformingToPublicProto(),
39+
"string",
40+
]
41+
}

test/ModuleInterface/lazy-typecheck.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,26 @@
2929
// CHECK: public protocol PublicProto {
3030
// CHECK: func req() -> Swift.Int
3131
// CHECK: }
32-
3332
// CHECK: public struct PublicStruct {
3433
// CHECK: public init(x: Swift.Int)
3534
// CHECK: public func publicMethod() -> Swift.Int
3635
// CHECK: public static func publicStaticMethod()
3736
// CHECK: }
38-
3937
// CHECK: public class PublicClass {
4038
// CHECK: public init(x: Swift.Int)
4139
// CHECK: public func publicMethod() -> Swift.Int
4240
// CHECK: public class func publicClassMethod()
4341
// CHECK: deinit
4442
// CHECK: }
43+
// CHECK: public struct PublicStructConformingToPublicProto : PublicProto {
44+
// CHECK: public init()
45+
// CHECK: public func req() -> Swift.Int
46+
// CHECK: }
47+
// CHECK: public class PublicClassConformingToPublicProto : PublicProto {
48+
// CHECK: public init()
49+
// CHECK: public func req() -> Swift.Int
50+
// CHECK: deinit
51+
// CHECK: }
52+
// CHECK: extension Swift.String : PublicProto {
53+
// CHECK: public func req() -> Swift.Int
54+
// CHECK: }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -typecheck -experimental-lazy-typecheck -emit-tbd -emit-tbd-path %t/lazy.tbd %s -enable-library-evolution -parse-as-library
3+
4+
public protocol P {
5+
func req()
6+
}
7+
8+
// FIXME: This malformed conformance should probably be diagnosed.
9+
public struct S: P {
10+
}

test/TBD/lazy-typecheck.swift

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,30 @@ compatibility-version: 0
1717
swift-abi-version: 7
1818
exports:
1919
- targets: [ arm64-macos ]
20-
symbols: [ '_$s14lazy_typecheck10publicFuncSiyF', '_$s14lazy_typecheck11PublicClassC06publicD6MethodyyFZTj',
20+
symbols: [ '_$s14lazy_typecheck023PublicClassConformingToC5ProtoC3reqSiyFTj',
21+
'_$s14lazy_typecheck023PublicClassConformingToC5ProtoC3reqSiyFTq',
22+
'_$s14lazy_typecheck023PublicClassConformingToC5ProtoCAA0cG0AAMc',
23+
'_$s14lazy_typecheck023PublicClassConformingToC5ProtoCAA0cG0AAWP',
24+
'_$s14lazy_typecheck023PublicClassConformingToC5ProtoCACycfC',
25+
'_$s14lazy_typecheck023PublicClassConformingToC5ProtoCACycfCTj',
26+
'_$s14lazy_typecheck023PublicClassConformingToC5ProtoCACycfCTq',
27+
'_$s14lazy_typecheck023PublicClassConformingToC5ProtoCACycfc',
28+
'_$s14lazy_typecheck023PublicClassConformingToC5ProtoCMa',
29+
'_$s14lazy_typecheck023PublicClassConformingToC5ProtoCMm',
30+
'_$s14lazy_typecheck023PublicClassConformingToC5ProtoCMn',
31+
'_$s14lazy_typecheck023PublicClassConformingToC5ProtoCMo',
32+
'_$s14lazy_typecheck023PublicClassConformingToC5ProtoCMu',
33+
'_$s14lazy_typecheck023PublicClassConformingToC5ProtoCN',
34+
'_$s14lazy_typecheck023PublicClassConformingToC5ProtoCfD',
35+
'_$s14lazy_typecheck023PublicClassConformingToC5ProtoCfd',
36+
'_$s14lazy_typecheck024PublicStructConformingToC5ProtoV3reqSiyF',
37+
'_$s14lazy_typecheck024PublicStructConformingToC5ProtoVAA0cG0AAMc',
38+
'_$s14lazy_typecheck024PublicStructConformingToC5ProtoVAA0cG0AAWP',
39+
'_$s14lazy_typecheck024PublicStructConformingToC5ProtoVACycfC',
40+
'_$s14lazy_typecheck024PublicStructConformingToC5ProtoVMa',
41+
'_$s14lazy_typecheck024PublicStructConformingToC5ProtoVMn',
42+
'_$s14lazy_typecheck024PublicStructConformingToC5ProtoVN',
43+
'_$s14lazy_typecheck10publicFuncSiyF', '_$s14lazy_typecheck11PublicClassC06publicD6MethodyyFZTj',
2144
'_$s14lazy_typecheck11PublicClassC06publicD6MethodyyFZTq',
2245
'_$s14lazy_typecheck11PublicClassC12publicMethodSiyFTj', '_$s14lazy_typecheck11PublicClassC12publicMethodSiyFTq',
2346
'_$s14lazy_typecheck11PublicClassC1xACSi_tcfC', '_$s14lazy_typecheck11PublicClassC1xACSi_tcfCTj',
@@ -35,5 +58,7 @@ exports:
3558
'_$s14lazy_typecheck13inlinableFuncSiyF', '_$s14lazy_typecheck24publicFuncWithDefaultArgyS2iF',
3659
'_$s14lazy_typecheck30publicFuncWithOpaqueReturnTypeQryF',
3760
'_$s14lazy_typecheck30publicFuncWithOpaqueReturnTypeQryFQOMQ',
38-
'_$s14lazy_typecheck32constrainedGenericPublicFunctionyyxAA0E5ProtoRzlF' ]
61+
'_$s14lazy_typecheck32constrainedGenericPublicFunctionyyxAA0E5ProtoRzlF',
62+
'_$sSS14lazy_typecheck11PublicProtoAAMc', '_$sSS14lazy_typecheck11PublicProtoAAWP',
63+
'_$sSS14lazy_typecheckE3reqSiyF' ]
3964
...

0 commit comments

Comments
 (0)