Skip to content

Commit d4f21f1

Browse files
authored
Merge pull request #1803 from xedin/allow-init-expr-for-comp-prop-with-init-accessors-5.9
[5.9] InitAccessors: Parse initializer exprs associated with computed prope…
2 parents f1e9245 + 0eb8fd7 commit d4f21f1

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
@@ -1960,4 +1960,82 @@ final class DeclarationTests: XCTestCase {
19601960
fixedSource: "protocol X<<#identifier#>> {}"
19611961
)
19621962
}
1963+
1964+
func testInitAccessorsWithDefaultValues() {
1965+
assertParse(
1966+
"""
1967+
struct Test {
1968+
var pair: (Int, Int) = (42, 0) {
1969+
init(initialValue) {}
1970+
1971+
get { (0, 42) }
1972+
set { }
1973+
}
1974+
}
1975+
"""
1976+
)
1977+
1978+
assertParse(
1979+
"""
1980+
struct Test {
1981+
var pair: (Int, Int) = (42, 0) {
1982+
init initializes(a) {}
1983+
1984+
get { (0, 42) }
1985+
set { }
1986+
}
1987+
}
1988+
"""
1989+
)
1990+
1991+
assertParse(
1992+
"""
1993+
struct Test {
1994+
var pair: (Int, Int) = (42, 0) {
1995+
get { (0, 42) }
1996+
set { }
1997+
1998+
init(initialValue1️⃣) {}
1999+
}
2000+
}
2001+
""",
2002+
substructure: Syntax(
2003+
InitializerDeclSyntax(
2004+
initKeyword: .keyword(.`init`),
2005+
signature: FunctionSignatureSyntax(
2006+
input: ParameterClauseSyntax(
2007+
leftParen: .leftParenToken(),
2008+
parameterList: FunctionParameterListSyntax([
2009+
FunctionParameterSyntax(
2010+
firstName: .identifier("initialValue"),
2011+
colon: .colonToken(presence: .missing),
2012+
type: TypeSyntax(MissingTypeSyntax(placeholder: .identifier("<#type#>", presence: .missing)))
2013+
)
2014+
]),
2015+
rightParen: .rightParenToken(trailingTrivia: .space)
2016+
)
2017+
),
2018+
body: CodeBlockSyntax(
2019+
leftBrace: .leftBraceToken(),
2020+
statements: CodeBlockItemListSyntax([]),
2021+
rightBrace: .rightBraceToken()
2022+
)
2023+
)
2024+
),
2025+
diagnostics: [
2026+
DiagnosticSpec(message: "expected ':' and type in parameter", fixIts: ["insert ':' and type"])
2027+
],
2028+
fixedSource:
2029+
"""
2030+
struct Test {
2031+
var pair: (Int, Int) = (42, 0) {
2032+
get { (0, 42) }
2033+
set { }
2034+
2035+
init(initialValue: <#type#>) {}
2036+
}
2037+
}
2038+
"""
2039+
)
2040+
}
19632041
}

0 commit comments

Comments
 (0)