Skip to content

Commit 278960d

Browse files
authored
Merge pull request #1296 from DougGregor/freestanding-expansion-as-expr-again
[Parser] Go back to parsing macro expansions as expressions where possible
2 parents eb5b363 + f12ffc7 commit 278960d

File tree

12 files changed

+42
-29
lines changed

12 files changed

+42
-29
lines changed

CodeGeneration/Sources/SyntaxSupport/gyb_generated/ExprNodes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ public let EXPR_NODES: [Node] = [
11601160
]),
11611161

11621162
Node(name: "MacroExpansionExpr",
1163-
nameForDiagnostics: "macro expansion expression",
1163+
nameForDiagnostics: "macro expansion",
11641164
kind: "Expr",
11651165
traits: [
11661166
"FreestandingMacroExpansion"

Sources/SwiftParser/Declarations.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@ extension TokenConsumer {
3232
mutating func atStartOfDeclaration(
3333
isAtTopLevel: Bool = false,
3434
allowInitDecl: Bool = true,
35-
allowRecovery: Bool = false,
36-
preferPoundAsExpression: Bool = false
35+
allowRecovery: Bool = false
3736
) -> Bool {
3837
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 {
38+
// Don't treat freestanding macro expansions as declarations. They'll be
39+
// parsed as expressions.
40+
if self.at(.pound) {
4241
return false
4342
}
4443

Sources/SwiftParser/Statements.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ extension Parser {
959959
.poundIfKeyword, .poundEndifKeyword, .poundElseKeyword,
960960
.poundElseifKeyword,
961961
])
962-
&& !self.atStartOfStatement() && !self.atStartOfDeclaration(preferPoundAsExpression: true)
962+
&& !self.atStartOfStatement() && !self.atStartOfDeclaration()
963963
{
964964
let parsedExpr = self.parseExpression()
965965
if hasMisplacedTry && !parsedExpr.is(RawTryExprSyntax.self) {

Sources/SwiftSyntax/generated/Misc.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ extension SyntaxKind {
11081108
case .macroExpansionDecl:
11091109
return "macro expansion"
11101110
case .macroExpansionExpr:
1111-
return "macro expansion expression"
1111+
return "macro expansion"
11121112
case .matchingPatternCondition:
11131113
return "pattern matching"
11141114
case .memberAccessExpr:

Sources/SwiftSyntaxMacros/MacroProtocols/CodeItemMacro.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ import SwiftSyntax
1313
public protocol CodeItemMacro: FreestandingMacro {
1414
/// Expand a macro described by the given freestanding macro expansion
1515
/// declaration within the given context to produce a set of declarations.
16-
static func expansion<Context: MacroExpansionContext>(
17-
of node: MacroExpansionDeclSyntax,
16+
static func expansion<
17+
Node: FreestandingMacroExpansionSyntax,
18+
Context: MacroExpansionContext
19+
>(
20+
of node: Node,
1821
in context: Context
1922
) throws -> [CodeBlockItemSyntax]
2023
}

Sources/SwiftSyntaxMacros/MacroProtocols/DeclarationMacro.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ public protocol DeclarationMacro: FreestandingMacro {
1414
/// Expand a macro described by the given freestanding macro expansion
1515
/// declaration within the given context to produce a set of declarations.
1616
static func expansion<
17+
Node: FreestandingMacroExpansionSyntax,
1718
Context: MacroExpansionContext
1819
>(
19-
of node: MacroExpansionDeclSyntax,
20+
of node: Node,
2021
in context: Context
2122
) throws -> [DeclSyntax]
2223
}

Sources/SwiftSyntaxMacros/MacroProtocols/ExpressionMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import SwiftSyntax
1414

1515
/// Describes a macro that is explicitly expanded as an expression.
1616
public protocol ExpressionMacro: FreestandingMacro {
17-
/// Expand a macro described by the given macro expansion expression
17+
/// Expand a macro described by the given freestanding macro expansion
1818
/// within the given context to produce a replacement expression.
1919
static func expansion<
2020
Node: FreestandingMacroExpansionSyntax,

Sources/SwiftSyntaxMacros/MacroSystem.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,14 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
127127
for item in node {
128128
// Expand declaration macros that were parsed as macro expansion
129129
// expressions in this context.
130-
if case let .decl(declItem) = item.item,
131-
let declExpansion = declItem.as(MacroExpansionDeclSyntax.self),
132-
let macro = macroSystem.macros[declExpansion.macro.text]
130+
if case let .expr(exprItem) = item.item,
131+
let exprExpansion = exprItem.as(MacroExpansionExprSyntax.self),
132+
let macro = macroSystem.macros[exprExpansion.macro.text]
133133
{
134134
do {
135135
if let macro = macro as? DeclarationMacro.Type {
136136
let expandedItemList = try macro.expansion(
137-
of: declExpansion,
137+
of: exprExpansion,
138138
in: context
139139
)
140140
newItems.append(
@@ -144,7 +144,7 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
144144
)
145145
} else if let macro = macro as? ExpressionMacro.Type {
146146
let expandedExpr = try macro.expansion(
147-
of: declExpansion,
147+
of: exprExpansion,
148148
in: context
149149
)
150150
newItems.append(CodeBlockItemSyntax(item: .init(expandedExpr)))

Tests/SwiftParserTest/ExpressionTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,10 @@ final class ExpressionTests: XCTestCase {
896896
}
897897

898898
func testMacroExpansionExpression() {
899+
AssertParse(
900+
#"#file == $0.path"#
901+
)
902+
899903
AssertParse(
900904
#"let a = #embed("filename.txt")"#
901905
)

Tests/SwiftParserTest/translated/ObjectLiteralsTests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class ObjectLiteralsTests: XCTestCase {
2121
let _ = [#Color(colorLiteralRed: red, green: green, blue: blue, alpha: alpha)#1️⃣]
2222
""",
2323
diagnostics: [
24-
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in macro expansion expression")
24+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in macro expansion")
2525
]
2626
)
2727
}
@@ -32,7 +32,7 @@ final class ObjectLiteralsTests: XCTestCase {
3232
let _ = [#Image(imageLiteral: localResourceNameAsString)#1️⃣]
3333
""",
3434
diagnostics: [
35-
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in macro expansion expression")
35+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in macro expansion")
3636
]
3737
)
3838
}
@@ -43,7 +43,7 @@ final class ObjectLiteralsTests: XCTestCase {
4343
let _ = [#FileReference(fileReferenceLiteral: localResourceNameAsString)#1️⃣]
4444
""",
4545
diagnostics: [
46-
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in macro expansion expression")
46+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in macro expansion")
4747
]
4848
)
4949
}
@@ -102,8 +102,8 @@ final class ObjectLiteralsTests: XCTestCase {
102102
let _ = [#1️⃣#2️⃣]
103103
""",
104104
diagnostics: [
105-
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in macro expansion expression"),
106-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected identifier in macro expansion expression"),
105+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in macro expansion"),
106+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected identifier in macro expansion"),
107107
]
108108
)
109109
}
@@ -125,7 +125,7 @@ final class ObjectLiteralsTests: XCTestCase {
125125
let _ = [1️⃣#Color(red: 1, green: 1, blue: 1)#2️⃣3️⃣
126126
""",
127127
diagnostics: [
128-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected identifier in macro expansion expression"),
128+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected identifier in macro expansion"),
129129
DiagnosticSpec(locationMarker: "3️⃣", message: "expected ']' to end array"),
130130
]
131131
)
@@ -137,7 +137,7 @@ final class ObjectLiteralsTests: XCTestCase {
137137
let _ = [#Color(withRed: 1, green: 1, whatever: 2)#1️⃣]
138138
""",
139139
diagnostics: [
140-
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in macro expansion expression")
140+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in macro expansion")
141141
]
142142
)
143143
}

0 commit comments

Comments
 (0)