Skip to content

Commit 5ebcc29

Browse files
authored
Merge pull request #1657 from rintaro/plugin-limit-operatorfolding
[Macros] Don't fold operators in unnecessary nodes
2 parents 4cd9e58 + d58ca93 commit 5ebcc29

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
import SwiftSyntaxMacroExpansion
@@ -29,7 +30,7 @@ extension CompilerPluginMessageHandler {
2930
expandingSyntax: PluginMessage.Syntax
3031
) throws {
3132
let sourceManager = SourceManager()
32-
let syntax = sourceManager.add(expandingSyntax)
33+
let syntax = sourceManager.add(expandingSyntax, foldingWith: .standardOperators)
3334

3435
let context = PluginMacroExpansionContext(
3536
sourceManager: sourceManager,
@@ -78,7 +79,10 @@ extension CompilerPluginMessageHandler {
7879
expansionDiscriminator: discriminator
7980
)
8081

81-
let attributeNode = sourceManager.add(attributeSyntax).cast(AttributeSyntax.self)
82+
let attributeNode = sourceManager.add(
83+
attributeSyntax,
84+
foldingWith: .standardOperators
85+
).cast(AttributeSyntax.self)
8286
let declarationNode = sourceManager.add(declSyntax).cast(DeclSyntax.self)
8387
let parentDeclNode = parentDeclSyntax.map { sourceManager.add($0).cast(DeclSyntax.self) }
8488

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)