Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ private enum MacroApplicationError: DiagnosticMessage, Error {
case accessorMacroOnVariableWithMultipleBindings
case peerMacroOnVariableWithMultipleBindings
case malformedAccessor
case accessorMacroOnLetVariable

var diagnosticID: MessageID {
return MessageID(domain: diagnosticDomain, id: "\(self)")
Expand All @@ -650,6 +651,8 @@ private enum MacroApplicationError: DiagnosticMessage, Error {
return """
macro returned a malformed accessor. Accessors should start with an introducer like 'get' or 'set'.
"""
case .accessorMacroOnLetVariable:
return "accessor macro is not allowed to be applied to a 'let' variable"
}
}
}
Expand Down Expand Up @@ -923,6 +926,11 @@ private class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
return DeclSyntax(node)
}

guard node.bindingSpecifier.tokenKind != .keyword(.let) else {
contextGenerator(Syntax(node)).addDiagnostics(from: MacroApplicationError.accessorMacroOnLetVariable, node: node)
return DeclSyntax(node)
}

guard node.bindings.count == 1, let binding = node.bindings.first else {
contextGenerator(Syntax(node)).addDiagnostics(
from: MacroApplicationError.accessorMacroOnVariableWithMultipleBindings,
Expand Down
17 changes: 17 additions & 0 deletions Tests/SwiftSyntaxMacroExpansionTest/AccessorMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,21 @@ final class AccessorMacroTests: XCTestCase {
indentationWidth: indentationWidth
)
}

func testAccessorOnLetVariableDeclaration() {
assertMacroExpansion(
"""
@constantOne
let x: Int = 1
""",
expandedSource: """
let x: Int = 1
""",
diagnostics: [
DiagnosticSpec(message: "accessor macro is not allowed to be applied to a 'let' variable", line: 1, column: 1)
],
macros: ["constantOne": ConstantOneGetter.self],
indentationWidth: indentationWidth
)
}
}