1

I have a Swift Macro that does the following:

assertMacroExpansion(
    #"""
    struct MyStruct { var count: Int }
    let obj = MyStruct(count: 42)
    #destr(obj, \.count)
    """#,
    expandedSource:
    #"""
    struct MyStruct { var count: Int }
    let obj = MyStruct(count: 42)
    let count = obj[keyPath: \.count]
    """#,
    macros: testMacros)

The test passes, and expanding the macro in "main.swift" yields the desired result. However, I get the compiler error that the constant with the identifier cannot be found in scope like so:

Declaration generated through macro is not available in scope

I have found this Github Issue that may be related to the issue I'm experiencing.

Is this possibly a bug or am I doing something wrong?

5
  • What is your macro definition? Just because the unit test runs and expands correctly it's not 100% that the declaration is correct. Commented Apr 9, 2024 at 7:41
  • I'm not sure how that is relevant. The only thing that matters to the issue at hand is whether constant declarations that were generated through Swift Macros can be used in the source code following the macro. Commented Apr 10, 2024 at 23:33
  • Not really sadly. For example you can create a Peer Macro which defines an extension, your unit test will work fine, but it's still not gonna be considered as a valid extension. Same thing, by default you cannot create arbitrary named variables in an attached member macro, yet it won't tell you anything is wrong in the unit test. Things like these ultimately mean it's very important how you've exposed your macro and defined it. Commented Apr 11, 2024 at 8:44
  • 1
    Do take a look at the proposal's restrictions section and check if you have kept those in mind during your macro definition. Commented Apr 11, 2024 at 8:54
  • If I had to guess, I'd say you have forgotten to tell with the named: argument in the macro definition what new variables your macro will create during expansion. But we need to see your code to be able to say anything with certainty. Commented Apr 11, 2024 at 8:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.