[ASTGen] Handle 'prefixed(_)' in macro introduced names

'_' is not a 'DeclReferenceExprSyntax', but a
'DiscardAssignmentExprSyntax'.
This commit is contained in:
Rintaro Ishizaki
2025-03-15 15:41:14 -07:00
parent 6a7aacd86c
commit 3d91c33c14
2 changed files with 42 additions and 4 deletions

View File

@@ -1246,12 +1246,19 @@ extension ASTGenVisitor {
// E.g. 'named(foo())', use the callee to generate the name.
arg = call.calledExpression
}
guard let arg = arg.as(DeclReferenceExprSyntax.self) else {
// TODO: Diagnose.
return nil
}
if let arg = arg.as(DeclReferenceExprSyntax.self) {
name = self.generateDeclNameRef(declReferenceExpr: arg).name
} else if arg.is(DiscardAssignmentExprSyntax.self) {
name = BridgedDeclNameRef.createParsed(.createIdentifier(self.ctx.getIdentifier("_")))
} else {
// TODO: Diagnose
fatalError("expected name")
//return nil
}
if arguments.count >= 2 {
fatalError("unexpected arguments")
// TODO: Diagnose.
}

View File

@@ -115,3 +115,34 @@ func f(a: Int, b: String) async throws -> String
struct TestArbitrary {
#bitwidthNumberedStructs("MyIntOne")
}
// Stored properties generated by a peer macro
@attached(accessor)
@attached(peer, names: prefixed(_))
macro myPropertyWrapper() =
#externalMacro(module: "MacroDefinition", type: "PropertyWrapperMacro")
struct MyWrapperThingy<T> {
var storage: T
var wrappedValue: T {
get {
print("Getting value \(storage)")
return storage
}
set {
print("Setting value \(newValue)")
storage = newValue
}
}
}
struct S3 {
@myPropertyWrapper
var x: Int = 0
init(x: Int) {
self._x = MyWrapperThingy(storage: x)
}
}