Skip to content

Commit d58ca93

Browse files
committed
[Macros] Don't fold operators in unnecessary nodes
For attached macros, operator folding should only be done for the attribute syntax node, but not for the target declaration syntax and the parent decl syntax. This aligns with the compiler's ASTGen.
1 parent 17e83de commit d58ca93

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

Sources/SwiftCompilerPluginMessageHandling/Macros.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import SwiftBasicFormat
1414
import SwiftDiagnostics
15+
import SwiftOperators
1516
import SwiftSyntax
1617
import SwiftSyntaxMacros
1718

@@ -28,7 +29,7 @@ extension CompilerPluginMessageHandler {
2829
expandingSyntax: PluginMessage.Syntax
2930
) throws {
3031
let sourceManager = SourceManager()
31-
let syntax = sourceManager.add(expandingSyntax)
32+
let syntax = sourceManager.add(expandingSyntax, foldingWith: .standardOperators)
3233

3334
let context = PluginMacroExpansionContext(
3435
sourceManager: sourceManager,
@@ -97,7 +98,10 @@ extension CompilerPluginMessageHandler {
9798
expansionDiscriminator: discriminator
9899
)
99100

100-
let attributeNode = sourceManager.add(attributeSyntax).cast(AttributeSyntax.self)
101+
let attributeNode = sourceManager.add(
102+
attributeSyntax,
103+
foldingWith: .standardOperators
104+
).cast(AttributeSyntax.self)
101105
let declarationNode = sourceManager.add(declSyntax).cast(DeclSyntax.self)
102106

103107
let expandedSources: [String]

Sources/SwiftCompilerPluginMessageHandling/PluginMacroExpansionContext.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ class SourceManager {
6464

6565
/// Convert syntax information to a `Syntax` node. The location informations
6666
/// are cached in the source manager to provide `location(of:)` et al.
67-
func add(_ syntaxInfo: PluginMessage.Syntax) -> Syntax {
67+
func add(
68+
_ syntaxInfo: PluginMessage.Syntax,
69+
foldingWith operatorTable: OperatorTable? = nil
70+
) -> Syntax {
6871

6972
var node: Syntax
7073
var parser = Parser(syntaxInfo.source)
@@ -82,7 +85,9 @@ class SourceManager {
8285
case .attribute:
8386
node = Syntax(AttributeSyntax.parse(from: &parser))
8487
}
85-
node = OperatorTable.standardOperators.foldAll(node, errorHandler: { _ in /*ignore*/ })
88+
if let operatorTable = operatorTable {
89+
node = operatorTable.foldAll(node, errorHandler: { _ in /*ignore*/ })
90+
}
8691

8792
// Copy the location info from the plugin message.
8893
let location = KnownSourceSyntax.Location(

0 commit comments

Comments
 (0)