Skip to content

Commit 6d1a6c5

Browse files
committed
Sema: Avoid application extension platforms in if #available fix-its.
When the type checker noticed that a declaration with application extension availability markup was used somewhere that it would be potentially unavailable at runtime, the fix-it emitted by the compiler would check for the application extension platform's availability: ``` if #available(macOSApplicationExtension 12, *) { // Use potentially unavailable declarations } ``` This runtime check won't work. The fix-it should just suggest checking the availability of the base, non-extension platform instead. Resolves rdar://125860317.
1 parent 4229a91 commit 6d1a6c5

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,6 +1978,12 @@ static void fixAvailabilityByAddingVersionCheck(
19781978

19791979
PlatformKind Target = targetPlatform(Context.LangOpts);
19801980

1981+
// Runtime availability checks that specify app extension platforms don't
1982+
// work, so only suggest checks against the base platform.
1983+
if (auto TargetRemovingAppExtension =
1984+
basePlatformForExtensionPlatform(Target))
1985+
Target = *TargetRemovingAppExtension;
1986+
19811987
Out << "if #available(" << platformString(Target)
19821988
<< " " << RequiredRange.getLowerEndpoint().getAsString()
19831989
<< ", *) {\n";

test/attr/attr_availability_transitive_osx_appext.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,11 @@ extension NotOnOSXApplicationExtension {
6969
osx_extension() // OK
7070
}
7171
}
72+
73+
@available(OSXApplicationExtension, introduced: 12)
74+
func osx_introduced_in_macOS_extesnions_12() {}
75+
76+
func call_osx_introduced_in_macOS_extesnions_12() { // expected-note {{add @available attribute to enclosing global function}} {{1-1=@available(macOSApplicationExtension 12, *)\n}}
77+
osx_introduced_in_macOS_extesnions_12() // expected-error {{'osx_introduced_in_macOS_extesnions_12()' is only available in application extensions for macOS 12 or newer}}
78+
// expected-note@-1 {{add 'if #available' version check}} {{3-42=if #available(macOS 12, *) {\n osx_introduced_in_macOS_extesnions_12()\n \} else {\n // Fallback on earlier versions\n \}}}
79+
}

0 commit comments

Comments
 (0)