What checks are performed on closures in attached macro invocations?

I'm experimenting with macros that generate code based on the contents of a closure that's passed directly into the invocation. My understanding was that the closure would be type checked, but I was (pleasantly) surprised to realize that there's a lot of other checks that don't seem to be performed for these closures. For example, given a macro defined like this:

@attached(member)
public macro MyMacro(_ decl: () -> ()) = #externalMacro(module: "MacrosSandboxMacros", type: "MyMacro") // outputs [] when run

I get this output when applying it

func f() {
    var a: Int // warning: variable 'a' was never used
}

@MyMacro({
    var a: Int // no warning!
})
struct S {
    
    
}

This extends to reassigning lets

@MyMacro({
    let a: Int
    a = 10
    a = 9 // no warning
})
struct S {
    
    
}

As I mentioned before, type checking is still performed. let a: Int = "b" will fail to compile.

I have some ideas about how to take advantage of this behavior, but I want to confirm it's intended before I start relying on it being around in future Swift language versions.

2 Likes