Skip to content

Commit f52769a

Browse files
authored
Merge pull request #69998 from tshortli/lazy-typecheck-module-interface-desguar-typealias
2 parents 1f29471 + 8999ed3 commit f52769a

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3926,9 +3926,20 @@ void PrintAST::visitPatternBindingDecl(PatternBindingDecl *decl) {
39263926
for (auto idx : range(decl->getNumPatternEntries())) {
39273927
auto *pattern = decl->getPattern(idx);
39283928

3929-
// Force the entry to be typechecked before attempting to print.
3930-
if (shouldPrintAllSemanticDetails(Options) && !pattern->hasType())
3931-
(void)decl->getCheckedPatternBindingEntry(idx);
3929+
if (shouldPrintAllSemanticDetails(Options)) {
3930+
// Force the entry to be typechecked before attempting to print.
3931+
if (!pattern->hasType())
3932+
(void)decl->getCheckedPatternBindingEntry(idx);
3933+
3934+
// HACK: If the pattern type is a typealias, trigger a request that will
3935+
// fully typecheck the init. This ensures typealiases are desguared
3936+
// consistently.
3937+
if (decl->isInitialized(idx)) {
3938+
if (auto type = pattern->getType())
3939+
if (type->getKind() == TypeKind::TypeAlias)
3940+
(void)decl->getCheckedAndContextualizedInit(idx);
3941+
}
3942+
}
39323943

39333944
if (!shouldPrintPattern(pattern))
39343945
continue;

test/Inputs/lazy_typecheck.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public func publicFunc() -> Int {
1616
return NoTypecheck.int
1717
}
1818

19+
public func publicFuncReturnsTypealias() -> PublicIntAlias {
20+
return NoTypecheck.int
21+
}
22+
1923
public func publicFuncWithDefaultArg(_ x: Int = 1) -> Int {
2024
return NoTypecheck.int
2125
}
@@ -88,11 +92,15 @@ struct InternalWrapper {
8892
// MARK: - Global vars
8993

9094
public var publicGlobalVar: Int = NoTypecheck.int
95+
public var publicGlobalVarTypealias: PublicIntAlias = 1
9196
public var publicGlobalVarInferredType = ""
97+
public var publicGlobalVarInferredInferredGeneric: [_] = [1]
98+
public var publicGlobalVarTypealiasGeneric: PublicIntAlias? = 1
9299
public var (publicGlobalVarInferredTuplePatX, publicGlobalVarInferredTuplePatY) = (0, 1)
93100

94101
var internalGlobalVar: NoTypecheck = NoTypecheck()
95102
var internalGlobalVarInferredType = NoTypecheck()
103+
var internalGlobalTypealiasVar: PublicIntAlias = NoTypecheck.int
96104

97105
// MARK: - Nominal types
98106

@@ -132,6 +140,7 @@ protocol InternalProtoConformingToPublicProto: PublicProto {
132140

133141
public struct PublicStruct {
134142
public var publicProperty: Int = NoTypecheck.int
143+
public var publicTypealiasProperty: PublicIntAlias = 1
135144
public var publicPropertyInferredType = ""
136145
public var publicLazyProperty: Int = NoTypecheck.int
137146
public var publicLazyPropertyInferred = 1
@@ -334,6 +343,7 @@ extension PublicGenericStruct: EmptyPublicProto where T == InternalStructForCons
334343

335344
// MARK: - Type aliases
336345

346+
public typealias PublicIntAlias = Int
337347
public typealias PublicStructAlias = PublicStruct
338348
typealias InternalTypeAlias = NoTypecheck
339349

test/Inputs/lazy_typecheck_client.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,35 @@ struct ConformsToMainActorProto: MainActorProtocol {
1212
}
1313

1414
func testGlobalFunctions() {
15-
_ = publicFunc()
16-
_ = publicFuncWithDefaultArg()
15+
let _: Int = publicFunc()
16+
let _: Int = publicFuncReturnsTypealias()
17+
let _: Int = publicFuncWithDefaultArg()
1718
#if TEST_PACKAGE
18-
_ = packageFunc()
19+
let _: Int = packageFunc()
1920
#endif
2021
constrainedGenericPublicFunction(ConformsToPublicProto())
21-
_ = publicSpecializedFunc(4)
22-
_ = publicSpecializedFunc(ConformsToPublicProto())
22+
let _: Int = publicSpecializedFunc(4)
23+
let _: ConformsToPublicProto = publicSpecializedFunc(ConformsToPublicProto())
2324
if #available(SwiftStdlib 5.1, *) {
24-
_ = publicFuncWithOpaqueReturnType()
25-
_ = publicAEICFuncWithOpaqueReturnType()
25+
let _: any PublicProto = publicFuncWithOpaqueReturnType()
26+
let _: Any = publicAEICFuncWithOpaqueReturnType()
2627
}
2728
}
2829

2930
func testGobalVars() {
3031
let _: Int = publicGlobalVar
32+
let _: Int = publicGlobalVarTypealias
3133
let _: String = publicGlobalVarInferredType
34+
let _: [Int] = publicGlobalVarInferredInferredGeneric
35+
let _: Int? = publicGlobalVarTypealiasGeneric
3236
let _: (Int, Int) = (publicGlobalVarInferredTuplePatX, publicGlobalVarInferredTuplePatY)
3337
}
3438

3539
func testPublicStructs() {
3640
let s = PublicStruct(x: 1)
3741
let _: Int = s.publicMethod()
3842
let _: Int = s.publicProperty
43+
let _: Int = s.publicTypealiasProperty
3944
let _: String = s.publicPropertyInferredType
4045
let _: Int = s.publicLazyProperty
4146
let _: Int = s.publicLazyPropertyInferred

0 commit comments

Comments
 (0)