Skip to content
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
6 changes: 3 additions & 3 deletions Sources/SwiftParser/Lookahead.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ extension Parser.Lookahead {
// If we have a 'didSet' or a 'willSet' label, disambiguate immediately as
// an accessor block.
let nextToken = self.peek()
if TokenSpec(.didSet) ~= nextToken || TokenSpec(.willSet) ~= nextToken {
if TokenSpec(.didSet) ~= nextToken || TokenSpec(.willSet) ~= nextToken || TokenSpec(.`init`) ~= nextToken {
return true
}

Expand All @@ -278,8 +278,8 @@ extension Parser.Lookahead {
}
}

// Check if we have 'didSet'/'willSet' after attributes.
return lookahead.at(.keyword(.didSet), .keyword(.willSet))
// Check if we have 'didSet'/'willSet' or 'init' after attributes.
return lookahead.at(.keyword(.didSet), .keyword(.willSet), .keyword(.`init`))
}
}

Expand Down
78 changes: 78 additions & 0 deletions Tests/SwiftParserTest/DeclarationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2540,4 +2540,82 @@ final class DeclarationTests: XCTestCase {
fixedSource: "let foo: [Int] = []"
)
}

func testInitAccessorsWithDefaultValues() {
assertParse(
"""
struct Test {
var pair: (Int, Int) = (42, 0) {
init(initialValue) {}

get { (0, 42) }
set { }
}
}
"""
)

assertParse(
"""
struct Test {
var pair: (Int, Int) = (42, 0) {
init initializes(a) {}

get { (0, 42) }
set { }
}
}
"""
)

assertParse(
"""
struct Test {
var pair: (Int, Int) = (42, 0) {
get { (0, 42) }
set { }

init(initialValue1️⃣) {}
}
}
""",
substructure: Syntax(
InitializerDeclSyntax(
initKeyword: .keyword(.`init`),
signature: FunctionSignatureSyntax(
input: ParameterClauseSyntax(
leftParen: .leftParenToken(),
parameterList: FunctionParameterListSyntax([
FunctionParameterSyntax(
firstName: .identifier("initialValue"),
colon: .colonToken(presence: .missing),
type: TypeSyntax(MissingTypeSyntax(placeholder: .identifier("<#type#>", presence: .missing)))
)
]),
rightParen: .rightParenToken(trailingTrivia: .space)
)
),
body: CodeBlockSyntax(
leftBrace: .leftBraceToken(),
statements: CodeBlockItemListSyntax([]),
rightBrace: .rightBraceToken()
)
)
),
diagnostics: [
DiagnosticSpec(message: "expected ':' and type in parameter", fixIts: ["insert ':' and type"])
],
fixedSource:
"""
struct Test {
var pair: (Int, Int) = (42, 0) {
get { (0, 42) }
set { }

init(initialValue: <#type#>) {}
}
}
"""
)
}
}