-
Notifications
You must be signed in to change notification settings - Fork 416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
makeUniqueName() produces tokens that cannot be used as closure arguments #2256
Comments
See also swiftlang/swift#68475 |
|
Having a day here… :) The issue with the spurious error is fixed, but I still can't use |
Tracked in Apple’s issue tracker as rdar://116402749 |
Reduced reproducer public struct StringifyMacro: ExpressionMacro {
public static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) -> ExprSyntax {
guard let argument = node.argumentList.first?.expression else {
fatalError("compiler bug: the macro does not have any arguments")
}
let arg0 = context.makeUniqueName("")
return """
{ \(arg0) in
return \(argument)
}(1)
"""
}
} Usage _ = #stringify(a + b) Expands to the following, which doesn’t compile _ = { $s17mac_adsfjlkClient33_E5FD3D533106409A33F51D71F9BB5B8CLl9stringifyfMf_7__localfMu_ in
return a + b
}(1) |
@ahoppen It appears to me extension PluginMacroExpansionContext: MacroExpansionContext {
/// Generate a unique name for use in the macro.
public func makeUniqueName(_ providedName: String) -> TokenSyntax {
// If provided with an empty name, substitute in something.
let name = providedName.isEmpty ? "__local" : providedName
// Grab a unique index value for this name.
let uniqueIndex = uniqueNames[name, default: 0]
uniqueNames[name] = uniqueIndex + 1
// Start with the discriminator.
var resultString = expansionDiscriminator
// Mangle the name
resultString += "\(name.count)\(name)"
// Mangle the operator for unique macro names.
resultString += "fMu"
// Mangle the index.
if uniqueIndex > 0 {
resultString += "\(uniqueIndex - 1)"
}
resultString += "_"
return TokenSyntax(.identifier(resultString), presence: .present)
}
Can we just trim the leading "$" from |
The |
|
@grynspan does it warrant a pitch? |
As this is not my area, I wouldn't presume to answer that question. @DougGregor or @ahoppen probably know more than I do. |
@ahoppen, drawing on the opinions of @grynspan and @j-f1, I think relaxing the use of compiler-generated |
I'd be fine with relaxing the use of compiler-generated |
Description
If I attempt to label closure arguments during macro expansion:
The compiler complains:
This means I can't reliably produce closure argument names, which is problematic if my macro expansion generates a closure and the macro is being used inside another closure:
Steps to Reproduce
See above.
The text was updated successfully, but these errors were encountered: