[ASTPrinter/Parse] Disambiguate accessor block in .swiftinterface

.swiftinterface sometimes prints a pattern binding initializer and the
accessor block. However the parser doesn't expect such constructs and
the disambiguation from trailing closures would be fragile. To make it
reliable, introduce a disambiguation marker `@_accessorBlock` .
`ASTPrinter` prints it right after `{` only if 1) The accessor block is
for a pattern binding declaration, 2) the decl has an initializer
printed, and 3) the non-observer accessor block is being printed. In the
parser, if the block after an initializer starts with
`{ @_accessorBlock`, it's always parsed as an accessor block instead of
a trailing closure.

rdar://140943107
This commit is contained in:
Rintaro Ishizaki
2025-10-16 16:35:10 -07:00
parent 412fffce7d
commit 6a1604ba09
5 changed files with 60 additions and 3 deletions

View File

@@ -0,0 +1,17 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -o %t/Test.swiftmodule -emit-module-interface-path %t/Test.swiftinterface -module-name Test %s
// RUN: %target-swift-typecheck-module-from-interface(%t/Test.swiftinterface) -module-name Test
// RUN: %FileCheck %s --check-prefix INTERFACE --input-file %t/Test.swiftinterface
@frozen public struct Struct {
public var values: [Int] = .init() {
willSet {}
}
}
// INTERFACE: @frozen public struct Struct {
// INTERFACE-LABEL: @_hasStorage public var values: [Swift.Int] = .init() { @_accessorBlock
// INTERFACE-NEXT: @_transparent get
// INTERFACE-NEXT: set
// INTERFACE-NEXT: }
// INTERFACE: }