Possible Bug in Macro-Generated Code Availability

I ran into a case where compiler-generated code that calls availability-gated code compiles and runs successfully:

struct Test {
    @MakeIndirectCall
    static func greet(name: String) -> String {
        return "Hello, \(name)!"
    }
    /* Expanded Macro
    static func greet_indirect(name: String) -> String {
        return greet(name: name)
    }
    */

    @available(macOS 99, iOS 99, *)
    @MakeIndirectCall
    static func triple(x: Int) -> Int {
        return 3 * x
    }
    /* Expanded Macro
    static func triple_indirect(x: Int) -> Int {
        return triple(x: x)
    }
    */
}

@main
struct Client {
    static func main() {
        print(Test.greet(name: "World"))           // βœ… OK
        print(Test.greet_indirect(name: "World"))  // βœ… OK
//      print(Test.triple(x: 5))                   // ❌ 'triple(x:)' is only available in macOS 99 or newer
        print(Test.triple_indirect(x: 5))          // ⚠️ SHOULD ERROR (?) , BUT DOESN'T
    }
}
Hello, World!
Hello, World!
15

However, if I manually inline the macro expansion in Xcode I get an availability error:

static func triple_indirect(x: Int) -> Int {
    return triple(x: x) // ❌ 'triple(x:)' is only available in macOS 99 or newer
}

Is this expected or a compiler bug?

It's a bug, the macro expansion should definitely be getting checked for availability. Mind filing a Github issue?

2 Likes
2 Likes

Thanks! This would be a compiler bug, though, not a swift-syntax bug. I'm not sure if it's possible to move an already created Issue.

1 Like

I've just transferred it to swiftlang/swift: Macro-Generated Code Availability Check Β· Issue #89936 Β· swiftlang/swift Β· GitHub

2 Likes

Thanks!