Skip to content

Commit 3d91c33

Browse files
committed
[ASTGen] Handle 'prefixed(_)' in macro introduced names
'_' is not a 'DeclReferenceExprSyntax', but a 'DiscardAssignmentExprSyntax'.
1 parent 6a7aacd commit 3d91c33

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

lib/ASTGen/Sources/ASTGen/DeclAttrs.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,12 +1246,19 @@ extension ASTGenVisitor {
12461246
// E.g. 'named(foo())', use the callee to generate the name.
12471247
arg = call.calledExpression
12481248
}
1249-
guard let arg = arg.as(DeclReferenceExprSyntax.self) else {
1250-
// TODO: Diagnose.
1251-
return nil
1249+
1250+
if let arg = arg.as(DeclReferenceExprSyntax.self) {
1251+
name = self.generateDeclNameRef(declReferenceExpr: arg).name
1252+
} else if arg.is(DiscardAssignmentExprSyntax.self) {
1253+
name = BridgedDeclNameRef.createParsed(.createIdentifier(self.ctx.getIdentifier("_")))
1254+
} else {
1255+
// TODO: Diagnose
1256+
fatalError("expected name")
1257+
//return nil
12521258
}
1253-
name = self.generateDeclNameRef(declReferenceExpr: arg).name
1259+
12541260
if arguments.count >= 2 {
1261+
fatalError("unexpected arguments")
12551262
// TODO: Diagnose.
12561263
}
12571264

test/ASTGen/macros.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,34 @@ func f(a: Int, b: String) async throws -> String
115115
struct TestArbitrary {
116116
#bitwidthNumberedStructs("MyIntOne")
117117
}
118+
119+
// Stored properties generated by a peer macro
120+
@attached(accessor)
121+
@attached(peer, names: prefixed(_))
122+
macro myPropertyWrapper() =
123+
#externalMacro(module: "MacroDefinition", type: "PropertyWrapperMacro")
124+
125+
struct MyWrapperThingy<T> {
126+
var storage: T
127+
128+
var wrappedValue: T {
129+
get {
130+
print("Getting value \(storage)")
131+
return storage
132+
}
133+
134+
set {
135+
print("Setting value \(newValue)")
136+
storage = newValue
137+
}
138+
}
139+
}
140+
141+
struct S3 {
142+
@myPropertyWrapper
143+
var x: Int = 0
144+
145+
init(x: Int) {
146+
self._x = MyWrapperThingy(storage: x)
147+
}
148+
}

0 commit comments

Comments
 (0)