Skip to content

Commit 017a1fd

Browse files
committed
Fix parsing of "return <macro-expansion>"
1 parent deef247 commit 017a1fd

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

Sources/SwiftParser/Declarations.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,16 @@ extension TokenConsumer {
3232
mutating func atStartOfDeclaration(
3333
isAtTopLevel: Bool = false,
3434
allowInitDecl: Bool = true,
35-
allowRecovery: Bool = false
35+
allowRecovery: Bool = false,
36+
preferPoundAsExpression: Bool = false
3637
) -> Bool {
3738
if self.at(anyIn: PoundDeclarationStart.self) != nil {
39+
// If we are in a context where we prefer # to be an expression,
40+
// treat it as one if it's not at the start of the line.
41+
if preferPoundAsExpression && self.at(.pound) && !self.currentToken.isAtStartOfLine {
42+
return false
43+
}
44+
3845
return true
3946
}
4047

Sources/SwiftParser/Statements.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,8 @@ extension Parser {
985985
.poundIfKeyword, .poundErrorKeyword, .poundWarningKeyword,
986986
.poundEndifKeyword, .poundElseKeyword, .poundElseifKeyword,
987987
])
988-
&& !self.atStartOfStatement() && !self.atStartOfDeclaration()
988+
&& !self.atStartOfStatement() &&
989+
!self.atStartOfDeclaration(preferPoundAsExpression: true)
989990
{
990991
let parsedExpr = self.parseExpression()
991992
if hasMisplacedTry && !parsedExpr.is(RawTryExprSyntax.self) {

Tests/SwiftParserTest/ExpressionTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ final class ExpressionTests: XCTestCase {
726726
"#keyPath((b:1️⃣)2️⃣",
727727
diagnostics: [
728728
DiagnosticSpec(locationMarker: "1️⃣", message: "expected value in tuple"),
729-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected ')' to end pound literal expression"),
729+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected ')' to end pound literal declaration"),
730730
]
731731
)
732732
}

0 commit comments

Comments
 (0)