Skip to content

AST: Desugar typed patterns in swiftinterfaces consistently. #69998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3926,9 +3926,20 @@ void PrintAST::visitPatternBindingDecl(PatternBindingDecl *decl) {
for (auto idx : range(decl->getNumPatternEntries())) {
auto *pattern = decl->getPattern(idx);

// Force the entry to be typechecked before attempting to print.
if (shouldPrintAllSemanticDetails(Options) && !pattern->hasType())
(void)decl->getCheckedPatternBindingEntry(idx);
if (shouldPrintAllSemanticDetails(Options)) {
// Force the entry to be typechecked before attempting to print.
if (!pattern->hasType())
(void)decl->getCheckedPatternBindingEntry(idx);

// HACK: If the pattern type is a typealias, trigger a request that will
// fully typecheck the init. This ensures typealiases are desguared
// consistently.
if (decl->isInitialized(idx)) {
if (auto type = pattern->getType())
if (type->getKind() == TypeKind::TypeAlias)
(void)decl->getCheckedAndContextualizedInit(idx);
}
}

if (!shouldPrintPattern(pattern))
continue;
Expand Down
10 changes: 10 additions & 0 deletions test/Inputs/lazy_typecheck.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public func publicFunc() -> Int {
return NoTypecheck.int
}

public func publicFuncReturnsTypealias() -> PublicIntAlias {
return NoTypecheck.int
}

public func publicFuncWithDefaultArg(_ x: Int = 1) -> Int {
return NoTypecheck.int
}
Expand Down Expand Up @@ -88,11 +92,15 @@ struct InternalWrapper {
// MARK: - Global vars

public var publicGlobalVar: Int = NoTypecheck.int
public var publicGlobalVarTypealias: PublicIntAlias = 1
public var publicGlobalVarInferredType = ""
public var publicGlobalVarInferredInferredGeneric: [_] = [1]
public var publicGlobalVarTypealiasGeneric: PublicIntAlias? = 1
public var (publicGlobalVarInferredTuplePatX, publicGlobalVarInferredTuplePatY) = (0, 1)

var internalGlobalVar: NoTypecheck = NoTypecheck()
var internalGlobalVarInferredType = NoTypecheck()
var internalGlobalTypealiasVar: PublicIntAlias = NoTypecheck.int

// MARK: - Nominal types

Expand Down Expand Up @@ -132,6 +140,7 @@ protocol InternalProtoConformingToPublicProto: PublicProto {

public struct PublicStruct {
public var publicProperty: Int = NoTypecheck.int
public var publicTypealiasProperty: PublicIntAlias = 1
public var publicPropertyInferredType = ""
public var publicLazyProperty: Int = NoTypecheck.int
public var publicLazyPropertyInferred = 1
Expand Down Expand Up @@ -334,6 +343,7 @@ extension PublicGenericStruct: EmptyPublicProto where T == InternalStructForCons

// MARK: - Type aliases

public typealias PublicIntAlias = Int
public typealias PublicStructAlias = PublicStruct
typealias InternalTypeAlias = NoTypecheck

Expand Down
19 changes: 12 additions & 7 deletions test/Inputs/lazy_typecheck_client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,35 @@ struct ConformsToMainActorProto: MainActorProtocol {
}

func testGlobalFunctions() {
_ = publicFunc()
_ = publicFuncWithDefaultArg()
let _: Int = publicFunc()
let _: Int = publicFuncReturnsTypealias()
let _: Int = publicFuncWithDefaultArg()
#if TEST_PACKAGE
_ = packageFunc()
let _: Int = packageFunc()
#endif
constrainedGenericPublicFunction(ConformsToPublicProto())
_ = publicSpecializedFunc(4)
_ = publicSpecializedFunc(ConformsToPublicProto())
let _: Int = publicSpecializedFunc(4)
let _: ConformsToPublicProto = publicSpecializedFunc(ConformsToPublicProto())
if #available(SwiftStdlib 5.1, *) {
_ = publicFuncWithOpaqueReturnType()
_ = publicAEICFuncWithOpaqueReturnType()
let _: any PublicProto = publicFuncWithOpaqueReturnType()
let _: Any = publicAEICFuncWithOpaqueReturnType()
}
}

func testGobalVars() {
let _: Int = publicGlobalVar
let _: Int = publicGlobalVarTypealias
let _: String = publicGlobalVarInferredType
let _: [Int] = publicGlobalVarInferredInferredGeneric
let _: Int? = publicGlobalVarTypealiasGeneric
let _: (Int, Int) = (publicGlobalVarInferredTuplePatX, publicGlobalVarInferredTuplePatY)
}

func testPublicStructs() {
let s = PublicStruct(x: 1)
let _: Int = s.publicMethod()
let _: Int = s.publicProperty
let _: Int = s.publicTypealiasProperty
let _: String = s.publicPropertyInferredType
let _: Int = s.publicLazyProperty
let _: Int = s.publicLazyPropertyInferred
Expand Down