Skip to content

Commit f39a4a6

Browse files
authored
Merge pull request #76018 from benlangmuir/fix-availability-narrowing-fixit
[sema] Fix availability narrowing fixits for macOS >= 11
2 parents 993becc + c957826 commit f39a4a6

File tree

3 files changed

+53
-25
lines changed

3 files changed

+53
-25
lines changed

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,15 +1902,16 @@ static bool fixAvailabilityByNarrowingNearbyVersionCheck(
19021902
AvailabilityContext(RunningRange))) {
19031903

19041904
// Only fix situations that are "nearby" versions, meaning
1905-
// disagreement on a minor-or-less version for non-macOS,
1906-
// or disagreement on a subminor-or-less version for macOS.
1905+
// disagreement on a minor-or-less version (subminor-or-less version for
1906+
// macOS 10.x.y).
19071907
auto RunningVers = RunningRange.getLowerEndpoint();
19081908
auto RequiredVers = RequiredRange.getLowerEndpoint();
19091909
auto Platform = targetPlatform(Context.LangOpts);
19101910
if (RunningVers.getMajor() != RequiredVers.getMajor())
19111911
return false;
19121912
if ((Platform == PlatformKind::macOS ||
19131913
Platform == PlatformKind::macOSApplicationExtension) &&
1914+
RunningVers.getMajor() == 10 &&
19141915
!(RunningVers.getMinor().has_value() &&
19151916
RequiredVers.getMinor().has_value() &&
19161917
RunningVers.getMinor().value() ==

test/Sema/availability_versions_multi.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,14 @@ func useFromOtherOn99_51() {
3333
let o10_9 = OtherIntroduced10_9()
3434
o10_9.extensionMethodOnOtherIntroduced10_9AvailableOn99_51(o99_51)
3535
_ = o99_51.returns99_52Introduced99_52() // expected-error {{'returns99_52Introduced99_52()' is only available in macOS 99.52 or newer}}
36-
// expected-note@-1 {{add 'if #available' version check}}
3736

3837
_ = OtherIntroduced99_52()
3938
// expected-error@-1 {{'OtherIntroduced99_52' is only available in macOS 99.52 or newer}}
40-
// expected-note@-2 {{add 'if #available' version check}}
4139

4240
o99_51.extensionMethodOnOtherIntroduced99_51AvailableOn99_52() // expected-error {{'extensionMethodOnOtherIntroduced99_51AvailableOn99_52()' is only available in macOS 99.52 or newer}}
43-
// expected-note@-1 {{add 'if #available' version check}}
4441

4542
_ = OtherIntroduced99_51.NestedIntroduced99_52()
4643
// expected-error@-1 {{'NestedIntroduced99_52' is only available in macOS 99.52 or newer}}
47-
// expected-note@-2 {{add 'if #available' version check}}
4844
}
4945

5046
@available(OSX, introduced: 99.52)
@@ -54,7 +50,6 @@ func useFromOtherOn99_52() {
5450
let n99_52 = OtherIntroduced99_51.NestedIntroduced99_52()
5551
_ = n99_52.returns99_52()
5652
_ = n99_52.returns99_53() // expected-error {{'returns99_53()' is only available in macOS 99.53 or newer}}
57-
// expected-note@-1 {{add 'if #available' version check}}
5853

5954
// This will trigger validation of the global in availability_in_multi_other.swift
6055
_ = globalFromOtherOn99_52
Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,101 @@
1-
// RUN: %target-typecheck-verify-swift
1+
// RUN: %target-typecheck-verify-swift -target %target-cpu-apple-macos10.15
22
// REQUIRES: OS=macosx
33
// <rdar://problem/17669805> Suggest narrowing the range of bad availabile checks
44

55
import Foundation
66

7-
@available(macOS 10.50.2, *)
7+
@available(macOS 50.2, *)
88
func foo() { }
99

1010
func useFoo() {
11-
if #available(macOS 10.50.1, *) {
12-
foo() // expected-error {{'foo()' is only available in macOS 10.50.2 or newer}} {{-1:23-30=10.50.2}}
11+
if #available(macOS 50.1, *) {
12+
foo() // expected-error {{'foo()' is only available in macOS 50.2 or newer}} {{-1:23-27=50.2}}
1313
}
1414
}
1515

1616
func useFooDifferentSpelling() {
17-
if #available(OSX 10.50.1, *) {
18-
foo() // expected-error {{'foo()' is only available in macOS 10.50.2 or newer}} {{-1:21-28=10.50.2}}
17+
if #available(OSX 50.1, *) {
18+
foo() // expected-error {{'foo()' is only available in macOS 50.2 or newer}} {{-1:21-25=50.2}}
1919
}
2020
}
2121

2222
func useFooAlreadyOkRange() {
23-
if #available(OSX 10.51, *) {
23+
if #available(OSX 51, *) {
2424
foo()
2525
}
2626
}
2727

2828
func useFooUnaffectedSimilarText() {
29-
if #available(iOS 10.50.10, OSX 10.50.1, *) {
30-
foo() // expected-error {{'foo()' is only available in macOS 10.50.2 or newer}} {{-1:35-42=10.50.2}}
29+
if #available(iOS 50.10, OSX 50.1, *) {
30+
foo() // expected-error {{'foo()' is only available in macOS 50.2 or newer}} {{-1:32-36=50.2}}
3131
}
3232
}
3333

3434
func useFooWayOff() {
35-
// expected-note@-1{{add @available attribute to enclosing global function}}
35+
// expected-note@-1 2 {{add @available attribute to enclosing global function}}
3636
if #available(OSX 10.10, *) {
37-
foo() // expected-error {{'foo()' is only available in macOS 10.50.2 or newer}}
37+
foo() // expected-error {{'foo()' is only available in macOS 50.2 or newer}}
38+
// expected-note@-1{{add 'if #available' version check}}
39+
}
40+
41+
if #available(OSX 49.0, *) {
42+
foo() // expected-error {{'foo()' is only available in macOS 50.2 or newer}}
3843
// expected-note@-1{{add 'if #available' version check}}
3944
}
4045
}
4146

42-
@available(OSX 10.50, *)
47+
@available(OSX 50, *)
4348
class FooUser {
4449
func useFoo() {
45-
foo() // expected-error {{'foo()' is only available in macOS 10.50.2 or newer}} {{-3:16-21=10.50.2}}
50+
foo() // expected-error {{'foo()' is only available in macOS 50.2 or newer}} {{-3:16-18=50.2}}
4651
}
4752
}
4853

49-
@available(OSX, introduced: 10.50, obsoleted: 10.50.4)
54+
@available(OSX, introduced: 50, obsoleted: 50.4)
5055
class FooUser2 {
5156
func useFoo() {
52-
foo() // expected-error {{'foo()' is only available in macOS 10.50.2 or newer}} {{-3:29-34=10.50.2}}
57+
foo() // expected-error {{'foo()' is only available in macOS 50.2 or newer}} {{-3:29-31=50.2}}
5358
}
5459
}
5560

56-
@available(OSX, introduced: 10.50, obsoleted: 10.50.4)
61+
@available(OSX, introduced: 50, obsoleted: 50.4)
5762
@objc
5863
class FooUser3 : NSObject {
5964
func useFoo() {
60-
foo() // expected-error {{'foo()' is only available in macOS 10.50.2 or newer}} {{-4:29-34=10.50.2}}
65+
foo() // expected-error {{'foo()' is only available in macOS 50.2 or newer}} {{-4:29-31=50.2}}
6166
}
6267
}
6368

64-
@available(OSX, introduced: 10.50.4)
69+
@available(OSX, introduced: 50.4)
6570
class FooUserOkRange {
6671
func useFoo() {
6772
foo()
6873
}
6974
}
75+
76+
@available(macOS 10.50.2, *)
77+
func foo10() { }
78+
79+
func useFoo10() {
80+
if #available(macOS 10.50.1, *) {
81+
foo10() // expected-error {{'foo10()' is only available in macOS 10.50.2 or newer}} {{-1:23-30=10.50.2}}
82+
}
83+
}
84+
85+
func useFoo10WayOff() {
86+
// expected-note@-1 3 {{add @available attribute to enclosing global function}}
87+
if #available(OSX 10.10, *) {
88+
foo10() // expected-error {{'foo10()' is only available in macOS 10.50.2 or newer}}
89+
// expected-note@-1{{add 'if #available' version check}}
90+
}
91+
92+
if #available(OSX 10.49.0, *) {
93+
foo10() // expected-error {{'foo10()' is only available in macOS 10.50.2 or newer}}
94+
// expected-note@-1{{add 'if #available' version check}}
95+
}
96+
97+
if #available(OSX 9, *) {
98+
foo10() // expected-error {{'foo10()' is only available in macOS 10.50.2 or newer}}
99+
// expected-note@-1{{add 'if #available' version check}}
100+
}
101+
}

0 commit comments

Comments
 (0)