Skip to content

Commit 56f6371

Browse files
committed
Pass through parameters for #if item parsing
Pass through `isAtTopLevel` and `allowInitDecl` parameters, which allow for better diagnostics.
1 parent c0fd8ad commit 56f6371

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

Sources/SwiftParser/TopLevel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ extension Parser {
229229
// If config of attributes is parsed as part of declaration parsing as it
230230
// doesn't constitute its own code block item.
231231
let directive = self.parsePoundIfDirective { (parser, _) in
232-
parser.parseCodeBlockItem(isAtTopLevel: false, allowInitDecl: true)
232+
parser.parseCodeBlockItem(isAtTopLevel: isAtTopLevel, allowInitDecl: allowInitDecl)
233233
} addSemicolonIfNeeded: { lastElement, newItemAtStartOfLine, parser in
234234
if lastElement.semicolon == nil && !newItemAtStartOfLine {
235235
return RawCodeBlockItemSyntax(

Tests/SwiftParserTest/ExpressionTests.swift

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,7 +1839,7 @@ final class StatementExpressionTests: XCTestCase {
18391839
]
18401840
)
18411841
}
1842-
1842+
18431843
func testConsecutiveStatements2() {
18441844
assertParse(
18451845
"switch x {case y: a1️⃣ b2️⃣ c}",
@@ -1849,7 +1849,7 @@ final class StatementExpressionTests: XCTestCase {
18491849
]
18501850
)
18511851
}
1852-
1852+
18531853
func testConsecutiveStatements3() {
18541854
assertParse(
18551855
"""
@@ -1861,7 +1861,7 @@ final class StatementExpressionTests: XCTestCase {
18611861
]
18621862
)
18631863
}
1864-
1864+
18651865
func testConsecutiveStatements4() {
18661866
assertParse(
18671867
"""
@@ -1874,6 +1874,43 @@ final class StatementExpressionTests: XCTestCase {
18741874
)
18751875
}
18761876

1877+
func testInitCallInPoundIf() {
1878+
// Make sure we parse 'init()' as an expr, not a decl.
1879+
assertParse(
1880+
"""
1881+
class C {
1882+
init() {
1883+
#if true
1884+
init()
1885+
#endif
1886+
}
1887+
}
1888+
""",
1889+
substructure: Syntax(
1890+
FunctionCallExprSyntax(
1891+
calledExpression: IdentifierExprSyntax(identifier: .keyword(.init("init")!)),
1892+
leftParen: .leftParenToken(),
1893+
argumentList: TupleExprElementListSyntax([]),
1894+
rightParen: .rightParenToken()
1895+
)
1896+
)
1897+
)
1898+
}
1899+
1900+
func testUnexpectedCloseBraceInPoundIf() {
1901+
assertParse(
1902+
"""
1903+
#if true
1904+
1️⃣}
1905+
class C {}
1906+
#endif
1907+
""",
1908+
diagnostics: [
1909+
DiagnosticSpec(message: "unexpected brace before class")
1910+
]
1911+
)
1912+
}
1913+
18771914
func testStringLiteralAfterKeyPath() {
18781915
assertParse(
18791916
#"""

0 commit comments

Comments
 (0)