Skip to content

Commit c9fba67

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

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
@@ -1820,7 +1820,7 @@ final class StatementExpressionTests: XCTestCase {
18201820
]
18211821
)
18221822
}
1823-
1823+
18241824
func testConsecutiveStatements2() {
18251825
assertParse(
18261826
"switch x {case y: a1️⃣ b2️⃣ c}",
@@ -1830,7 +1830,7 @@ final class StatementExpressionTests: XCTestCase {
18301830
]
18311831
)
18321832
}
1833-
1833+
18341834
func testConsecutiveStatements3() {
18351835
assertParse(
18361836
"""
@@ -1842,7 +1842,7 @@ final class StatementExpressionTests: XCTestCase {
18421842
]
18431843
)
18441844
}
1845-
1845+
18461846
func testConsecutiveStatements4() {
18471847
assertParse(
18481848
"""
@@ -1855,6 +1855,43 @@ final class StatementExpressionTests: XCTestCase {
18551855
)
18561856
}
18571857

1858+
func testInitCallInPoundIf() {
1859+
// Make sure we parse 'init()' as an expr, not a decl.
1860+
assertParse(
1861+
"""
1862+
class C {
1863+
init() {
1864+
#if true
1865+
init()
1866+
#endif
1867+
}
1868+
}
1869+
""",
1870+
substructure: Syntax(
1871+
FunctionCallExprSyntax(
1872+
calledExpression: IdentifierExprSyntax(identifier: .keyword(.init("init")!)),
1873+
leftParen: .leftParenToken(),
1874+
argumentList: TupleExprElementListSyntax([]),
1875+
rightParen: .rightParenToken()
1876+
)
1877+
)
1878+
)
1879+
}
1880+
1881+
func testUnexpectedCloseBraceInPoundIf() {
1882+
assertParse(
1883+
"""
1884+
#if true
1885+
1️⃣}
1886+
class C {}
1887+
#endif
1888+
""",
1889+
diagnostics: [
1890+
DiagnosticSpec(message: "unexpected brace before class")
1891+
]
1892+
)
1893+
}
1894+
18581895
func testStringLiteralAfterKeyPath() {
18591896
AssertParse(
18601897
#"""

0 commit comments

Comments
 (0)