Skip to content
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

Open
grynspan opened this issue Oct 3, 2023 · 5 comments
Open
Labels
bug Something isn't working

Comments

@grynspan
Copy link
Contributor

grynspan commented Oct 3, 2023

Description

If I attempt to label closure arguments during macro expansion:

let arg0 = context.makeUniqueName("")
return """
{ \(arg0) in
  ...
}
"""

The compiler complains:

🛑 Inferred projection type 'Int' is not a property wrapper
🛑 Inferred projection type '() -> Int' is not a property wrapper

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:

@Test func foo() {
  let x = [2, 4, 6]
  x.forEach {
    #expect($0 % 2 == 0) // 🛑 Anonymous closure arguments cannot be used inside
                         // a closure that has explicit arguments; did you mean '$0'?
  }
}

Steps to Reproduce

See above.

@grynspan grynspan added the bug Something isn't working label Oct 3, 2023
@grynspan
Copy link
Contributor Author

grynspan commented Oct 3, 2023

See also apple/swift#68475

@grynspan
Copy link
Contributor Author

grynspan commented Oct 3, 2023

I can repro with swift-DEVELOPMENT-SNAPSHOT-2023-10-02-a-osx, which I think means #68475 isn't a complete fix. My mistake, I was using a different toolchain.

@grynspan grynspan marked this as a duplicate of apple/swift#68475 Oct 3, 2023
@grynspan grynspan closed this as completed Oct 3, 2023
@grynspan grynspan reopened this Oct 3, 2023
@grynspan
Copy link
Contributor Author

grynspan commented Oct 3, 2023

Having a day here… :) The issue with the spurious error is fixed, but I still can't use makeUniqueName() to generate my labels.

@ahoppen
Copy link
Collaborator

ahoppen commented Oct 3, 2023

Tracked in Apple’s issue tracker as rdar://116402749

@ahoppen
Copy link
Collaborator

ahoppen commented Oct 4, 2023

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)

Reproducing Package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants