Skip to content

Commit dd976be

Browse files
authored
Merge pull request #1795 from xedin/allow-init-expr-for-comp-prop-with-init-accessors
InitAccessors: Parse initializer exprs associated with computed prope…
2 parents 526958d + b05cd4e commit dd976be

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

Sources/SwiftParser/Lookahead.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ extension Parser.Lookahead {
254254
// If we have a 'didSet' or a 'willSet' label, disambiguate immediately as
255255
// an accessor block.
256256
let nextToken = self.peek()
257-
if TokenSpec(.didSet) ~= nextToken || TokenSpec(.willSet) ~= nextToken {
257+
if TokenSpec(.didSet) ~= nextToken || TokenSpec(.willSet) ~= nextToken || TokenSpec(.`init`) ~= nextToken {
258258
return true
259259
}
260260

@@ -278,8 +278,8 @@ extension Parser.Lookahead {
278278
}
279279
}
280280

281-
// Check if we have 'didSet'/'willSet' after attributes.
282-
return lookahead.at(.keyword(.didSet), .keyword(.willSet))
281+
// Check if we have 'didSet'/'willSet' or 'init' after attributes.
282+
return lookahead.at(.keyword(.didSet), .keyword(.willSet), .keyword(.`init`))
283283
}
284284
}
285285

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,4 +2529,82 @@ final class DeclarationTests: XCTestCase {
25292529
fixedSource: "let foo: [Int] = []"
25302530
)
25312531
}
2532+
2533+
func testInitAccessorsWithDefaultValues() {
2534+
assertParse(
2535+
"""
2536+
struct Test {
2537+
var pair: (Int, Int) = (42, 0) {
2538+
init(initialValue) {}
2539+
2540+
get { (0, 42) }
2541+
set { }
2542+
}
2543+
}
2544+
"""
2545+
)
2546+
2547+
assertParse(
2548+
"""
2549+
struct Test {
2550+
var pair: (Int, Int) = (42, 0) {
2551+
init initializes(a) {}
2552+
2553+
get { (0, 42) }
2554+
set { }
2555+
}
2556+
}
2557+
"""
2558+
)
2559+
2560+
assertParse(
2561+
"""
2562+
struct Test {
2563+
var pair: (Int, Int) = (42, 0) {
2564+
get { (0, 42) }
2565+
set { }
2566+
2567+
init(initialValue1️⃣) {}
2568+
}
2569+
}
2570+
""",
2571+
substructure: Syntax(
2572+
InitializerDeclSyntax(
2573+
initKeyword: .keyword(.`init`),
2574+
signature: FunctionSignatureSyntax(
2575+
input: ParameterClauseSyntax(
2576+
leftParen: .leftParenToken(),
2577+
parameterList: FunctionParameterListSyntax([
2578+
FunctionParameterSyntax(
2579+
firstName: .identifier("initialValue"),
2580+
colon: .colonToken(presence: .missing),
2581+
type: TypeSyntax(MissingTypeSyntax(placeholder: .identifier("<#type#>", presence: .missing)))
2582+
)
2583+
]),
2584+
rightParen: .rightParenToken(trailingTrivia: .space)
2585+
)
2586+
),
2587+
body: CodeBlockSyntax(
2588+
leftBrace: .leftBraceToken(),
2589+
statements: CodeBlockItemListSyntax([]),
2590+
rightBrace: .rightBraceToken()
2591+
)
2592+
)
2593+
),
2594+
diagnostics: [
2595+
DiagnosticSpec(message: "expected ':' and type in parameter", fixIts: ["insert ':' and type"])
2596+
],
2597+
fixedSource:
2598+
"""
2599+
struct Test {
2600+
var pair: (Int, Int) = (42, 0) {
2601+
get { (0, 42) }
2602+
set { }
2603+
2604+
init(initialValue: <#type#>) {}
2605+
}
2606+
}
2607+
"""
2608+
)
2609+
}
25322610
}

0 commit comments

Comments
 (0)