Files
swift-mirror/test/Macros/macro_availability_overload_macos.swift
Allan Shortlidge cec90e24af AST: Fix availability scopes for peer and extension macro expansions.
For peer, conformance, and extension macros, the macro expansion buffer is
logically a sibling of the attached declaration rather than nested inside it.
Looking up the buffer's parent availability scope using the attached
declaration's source location therefore lands inside the attached declaration's
own scope and the expansion silently inherits its @available constraints.
Compute the parent scope from the buffer's logical declaration context instead
so that sibling expansions are checked at the enclosing context's availability,
matching the behavior of equivalent code written by hand.

Resolves https://github.com/swiftlang/swift/issues/89936 and rdar://179585474.
2026-06-23 23:17:57 -07:00

37 lines
1.3 KiB
Swift

// REQUIRES: swift_swift_parser, asserts
// REQUIRES: OS=macosx
// RUN: %target-typecheck-verify-swift -swift-version 5 -module-name MacrosTest -target %target-cpu-apple-macos50
@freestanding(expression)
macro overloadedOnAvailability(_: Any) -> Int = #externalMacro(module: "MacroLibrary", type: "MyOldMacro")
//expected-warning@-1{{external macro implementation type 'MacroLibrary.MyOldMacro'}}
// expected-note@-2 2{{'overloadedOnAvailability' declared here}}
@available(macOS 60, *)
@freestanding(expression)
macro overloadedOnAvailability(_: Int) -> Double = #externalMacro(module: "MacroLibrary", type: "MyNewMacro")
//expected-warning@-1{{external macro implementation type 'MacroLibrary.MyNewMacro'}}
// expected-note@-2{{'overloadedOnAvailability' declared here}}
func mutateInt(_: inout Int) { }
func mutateDouble(_: inout Double) { }
func testAvailabilityOld() {
var a = #overloadedOnAvailability(1)
mutateInt(&a)
// expected-error@-2{{external macro implementation type 'MacroLibrary.MyOldMacro'}}
}
@available(macOS 60, *)
func testAvailabilitNew(a: Any) {
var a = #overloadedOnAvailability(1)
mutateDouble(&a)
// expected-error@-2{{external macro implementation type 'MacroLibrary.MyNewMa}}
var b = #overloadedOnAvailability(a)
mutateInt(&b)
// expected-error@-2{{external macro implementation type 'MacroLibrary.MyOldMacro'}}
}