swiftlang/swift-syntax

New lexicalContext only works in macro tests, not in actual code

Closed this issue · 4 comments

Description

Attempted to use the new lexicalContext field in MacroExpansionContext to get the enclosing context. Expected behavior in macro test but not in actual use.

This test passes:

        assertMacroExpansion(
            """
            enum L10n {
                enum CommonUI {
                    #Localize("Country")
                }
            }
            """,
            expandedSource: """
            enum L10n {
                enum CommonUI {
                    static var country: String {
                        String(localized: "L10n.CommonUI.country", bundle: .module)
                    }
                }
            }
            """,
            macros: testMacros
        )

But using macro in code produces this result:

enum L10n {
    enum CommonUI {
        static var country: String {
            String(localized: "country", bundle: .module)
       }
    }
}

Steps to Reproduce

Running this package will replicate the behavior:

https://github.com/rickbdotcom/LocalizeMacro

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

Which compiler are you using to build the macro? You will need to use a recent Swift Development Snapshot so the compiler has support for lexical contexts.

Ah! I will try that. I guess that would explain why it works in the unit tests, since it's calling the latest code in swift syntax parser, but the compiler itself was built with the older version.

I installed the snapshot and that works. Thanks! I will close this issue.