Skip to content

Macros who's expansions contain macros do not compile in some cases #85469

@alex-reilly-dd

Description

@alex-reilly-dd

Description

Reproduction code is below. Errors are as follows

Cannot find '$s23PropertyTestingKitTests5fuzzy4TestfMp_24generator51b5d19e81bfb5ffMu_' in scope
Cannot find '$s23PropertyTestingKitTests5fuzzy4TestfMp_23accessor51b5d19e81bfb5ffMu_' in scope

In this case, the expanded macro contains swift-testings @Test which further expands.

Use case

I'm trying to build a fuzzer for a property testing library I'm building out. The non-toy example is a macro that looks at a test function's parameter types and produces a parameterized test with randomized input while searching for boundary cases.

Reproduction

Macro

public struct FuzzTestMacro: PeerMacro {
    public static func expansion(
        of node: AttributeSyntax,
        providingPeersOf declaration: some DeclSyntaxProtocol,
        in context: some MacroExpansionContext
    ) throws -> [DeclSyntax] {
        return [
            DeclSyntax(
                stringLiteral: """
                @Test
                func fuzzy() {
                    fatalError()
                }
                """
            )
        ]
    }
}

Declaration

@attached(peer, names: named(fuzzy))
public macro FuzzTest() = #externalMacro(module: "PropertyTestingKitMacros", type: "FuzzTestMacro")

Test

@FuzzTest
func fuzzTest(_ input: String, _ otherInput: Int) async throws {
    print()
}

The above expands to

@Test
func fuzzy() {
    fatalError()
}

which does compile if pasted into the editor directly.

Expected behavior

I would expect this code to compile.

Environment

Xcode 26.1 beta 3. Running against macOS
Same errors running swift test from the command line

Additional information

Discussed here: https://forums.swift.org/t/is-it-expected-that-multiple-levels-of-macro-expansions-should-compile/83178/3

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.swift macroFeature → declarations: Swift `macro` declarationstriage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions