Skip to content

Commit 5e24617

Browse files
authored
Merge pull request #83607 from tshortli/allow-obsolete-in-unavailable-contexts
Sema: Rationalize availability checking in unavailable contexts
2 parents 0bd975a + 4d41db3 commit 5e24617

13 files changed

+125
-143
lines changed

lib/AST/AvailabilityConstraint.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -172,28 +172,9 @@ static bool canIgnoreConstraintInUnavailableContexts(
172172
return true;
173173

174174
case AvailabilityConstraint::Reason::Unintroduced:
175-
switch (domain.getKind()) {
176-
case AvailabilityDomain::Kind::Universal:
177-
case AvailabilityDomain::Kind::SwiftLanguage:
178-
case AvailabilityDomain::Kind::PackageDescription:
179-
case AvailabilityDomain::Kind::Embedded:
180-
case AvailabilityDomain::Kind::Custom:
181-
return false;
182-
case AvailabilityDomain::Kind::Platform:
183-
// Platform availability only applies to the target triple that the
184-
// binary is being compiled for. Since the same declaration can be
185-
// potentially unavailable from a given context when compiling for one
186-
// platform, but available from that context when compiling for a
187-
// different platform, it is overly strict to enforce potential platform
188-
// unavailability constraints in contexts that are unavailable to that
189-
// platform.
190-
return true;
191-
}
192-
return constraint.getDomain().isPlatform();
193-
194175
case AvailabilityConstraint::Reason::UnavailableObsolete:
195176
case AvailabilityConstraint::Reason::UnavailableUnintroduced:
196-
return false;
177+
return domain.isVersioned();
197178
}
198179
}
199180

lib/Sema/TypeCheckAccess.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,11 +2073,6 @@ class DeclAvailabilityChecker : public DeclVisitor<DeclAvailabilityChecker> {
20732073
// Don't bother checking errors.
20742074
if (type && type->hasError())
20752075
return;
2076-
2077-
// If the decl which references this type is unavailable on the current
2078-
// platform, don't diagnose the availability of the type.
2079-
if (Where.getAvailability().isUnavailable())
2080-
return;
20812076

20822077
diagnoseTypeAvailability(typeRepr, type, context->getLoc(),
20832078
Where.withReason(reason), flags);

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,9 +1502,12 @@ static void diagnoseIfDeprecated(SourceRange ReferenceRange,
15021502
// We match the behavior of clang to not report deprecation warnings
15031503
// inside declarations that are themselves deprecated on all deployment
15041504
// targets.
1505-
if (Availability.isDeprecated()) {
1505+
if (Availability.isDeprecated())
1506+
return;
1507+
1508+
// Skip diagnosing deprecated types in unavailable contexts.
1509+
if (Availability.isUnavailable() && isa<TypeDecl>(DeprecatedDecl))
15061510
return;
1507-
}
15081511

15091512
auto *ReferenceDC = Where.getDeclContext();
15101513
auto &Context = ReferenceDC->getASTContext();

test/ASTGen/embedded_availability.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public struct UnavailableInEmbedded {}
1111

1212
@available(*, unavailable, message: "always unavailable")
1313
public struct UniverallyUnavailable {}
14-
// expected-note@-1 {{'UniverallyUnavailable' has been explicitly marked unavailable here}}
14+
// expected-note@-1 3 {{'UniverallyUnavailable' has been explicitly marked unavailable here}}
1515

1616
@_unavailableInEmbedded
1717
public func unavailable_in_embedded() { }
@@ -43,7 +43,7 @@ public struct Available {}
4343
extension Available {
4444
public func unavailable_in_embedded_method( // expected-note {{'unavailable_in_embedded_method' has been explicitly marked unavailable here}}
4545
_ uie: UnavailableInEmbedded,
46-
_ uu: UniverallyUnavailable,
46+
_ uu: UniverallyUnavailable, // expected-error {{'UniverallyUnavailable' is unavailable: always unavailable}}
4747
_ a: Available,
4848
) {
4949
unavailable_in_embedded()
@@ -67,7 +67,7 @@ public func available(
6767
@_unavailableInEmbedded
6868
public func also_unavailable_in_embedded(
6969
_ uie: UnavailableInEmbedded, // OK
70-
_ uu: UniverallyUnavailable, // OK
70+
_ uu: UniverallyUnavailable, // expected-error {{'UniverallyUnavailable' is unavailable: always unavailable}}
7171
_ a: Available,
7272
) {
7373
unavailable_in_embedded() // OK

test/Availability/availability_custom_domains.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,11 +522,11 @@ class DerivedLessAvailable: BaseAvailableInEnabledDomain { // expected-error {{'
522522
}
523523

524524
@available(EnabledDomain, unavailable)
525-
class DerivedUnavailable: BaseAvailableInEnabledDomain { }
525+
class DerivedUnavailable: BaseAvailableInEnabledDomain { } // expected-error {{'BaseAvailableInEnabledDomain' is only available in EnabledDomain}}
526526

527-
// FIXME: This shouldn't be accepted
528527
@available(DisabledDomain, unavailable)
529-
class DerivedUnavailable2: BaseAvailableInEnabledDomain { }
528+
class DerivedUnavailable2: BaseAvailableInEnabledDomain { } // expected-error {{'BaseAvailableInEnabledDomain' is only available in EnabledDomain}}
529+
// expected-note@-1 {{add '@available' attribute to enclosing class}}
530530

531531
@available(EnabledDomain)
532532
@available(DisabledDomain, unavailable)

test/Availability/availability_target_min_inlining.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ public func alwaysUnavailable(
350350
_ = BetweenTargets()
351351
_ = AtDeploymentTarget()
352352
_ = AfterDeploymentTarget()
353-
_ = ObsoletedBetweenTargets() // expected-error {{'ObsoletedBetweenTargets' is unavailable in macOS}}
353+
_ = ObsoletedBetweenTargets()
354354
_ = Unavailable()
355355

356356
if #available(macOS 11, *) {
@@ -630,20 +630,20 @@ public func spiDeployedUseNoAvailable( // expected-note 3 {{add '@available' att
630630
defer {
631631
_ = AtDeploymentTarget()
632632
_ = AfterDeploymentTarget()
633-
_ = ObsoletedBetweenTargets() // expected-error {{'ObsoletedBetweenTargets' is unavailable in macOS}}
633+
_ = ObsoletedBetweenTargets()
634634
}
635635
_ = NoAvailable()
636636
_ = BeforeInliningTarget()
637637
_ = AtInliningTarget()
638638
_ = BetweenTargets()
639639
_ = AtDeploymentTarget()
640640
_ = AfterDeploymentTarget()
641-
_ = ObsoletedBetweenTargets() // expected-error {{'ObsoletedBetweenTargets' is unavailable in macOS}}
641+
_ = ObsoletedBetweenTargets()
642642
_ = Unavailable()
643643

644644
if #available(macOS 11, *) {
645645
_ = AfterDeploymentTarget()
646-
_ = ObsoletedBetweenTargets() // expected-error {{'ObsoletedBetweenTargets' is unavailable in macOS}}
646+
_ = ObsoletedBetweenTargets()
647647
}
648648
}
649649

@@ -930,7 +930,7 @@ public func defaultArgsUseUnavailable(
930930
_: Any = BetweenTargets.self,
931931
_: Any = AtDeploymentTarget.self,
932932
_: Any = AfterDeploymentTarget.self,
933-
_: Any = ObsoletedBetweenTargets.self, // expected-error {{'ObsoletedBetweenTargets' is unavailable in macOS}}
933+
_: Any = ObsoletedBetweenTargets.self,
934934
_: Any = Unavailable.self
935935
) {}
936936

@@ -1056,7 +1056,7 @@ public struct PublicStruct { // expected-note 21 {{add '@available' attribute}}
10561056

10571057
@available(macOS, unavailable)
10581058
public var gUnavailable: ObsoletedBetweenTargets {
1059-
ObsoletedBetweenTargets() // expected-error {{'ObsoletedBetweenTargets' is unavailable in macOS}}
1059+
ObsoletedBetweenTargets()
10601060
}
10611061

10621062
// The inferred types of public properties are exposed.

test/Availability/conformance_availability.swift

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public struct HasUnavailableConformance3 {}
7070

7171
@available(swift 12)
7272
extension HasUnavailableConformance3 : Horse {}
73-
// expected-note@-1 12{{conformance of 'HasUnavailableConformance3' to 'Horse' was introduced in Swift 12}}
73+
// expected-note@-1 6{{conformance of 'HasUnavailableConformance3' to 'Horse' was introduced in Swift 12}}
7474

7575
func passUnavailableConformance3(x: HasUnavailableConformance3) {
7676
takesHorse(x) // expected-error {{conformance of 'HasUnavailableConformance3' to 'Horse' is unavailable}}
@@ -83,20 +83,20 @@ func passUnavailableConformance3(x: HasUnavailableConformance3) {
8383

8484
@available(swift 12)
8585
func passUnavailableConformance3a(x: HasUnavailableConformance3) {
86-
takesHorse(x) // expected-error {{conformance of 'HasUnavailableConformance3' to 'Horse' is unavailable}}
87-
takesHorseExistential(x) // expected-error {{conformance of 'HasUnavailableConformance3' to 'Horse' is unavailable}}
88-
x.giddyUp() // expected-error {{conformance of 'HasUnavailableConformance3' to 'Horse' is unavailable}}
89-
_ = x.isGalloping // expected-error {{conformance of 'HasUnavailableConformance3' to 'Horse' is unavailable}}
90-
_ = x[keyPath: \.isGalloping] // expected-error {{conformance of 'HasUnavailableConformance3' to 'Horse' is unavailable}}
91-
_ = UsesHorse<HasUnavailableConformance3>.self // expected-error {{conformance of 'HasUnavailableConformance3' to 'Horse' is unavailable}}
86+
takesHorse(x)
87+
takesHorseExistential(x)
88+
x.giddyUp()
89+
_ = x.isGalloping
90+
_ = x[keyPath: \.isGalloping]
91+
_ = UsesHorse<HasUnavailableConformance3>.self
9292
}
9393

9494
// Platform obsoleted
9595
public struct HasUnavailableConformance4 {}
9696

9797
@available(macOS, obsoleted: 10.1)
9898
extension HasUnavailableConformance4 : Horse {}
99-
// expected-note@-1 12{{conformance of 'HasUnavailableConformance4' to 'Horse' was obsoleted in macOS 10.1}}
99+
// expected-note@-1 6{{conformance of 'HasUnavailableConformance4' to 'Horse' was obsoleted in macOS 10.1}}
100100

101101
func passUnavailableConformance4(x: HasUnavailableConformance4) {
102102
takesHorse(x) // expected-error {{conformance of 'HasUnavailableConformance4' to 'Horse' is unavailable in macOS}}
@@ -109,20 +109,20 @@ func passUnavailableConformance4(x: HasUnavailableConformance4) {
109109

110110
@available(macOS, obsoleted: 10.1)
111111
func passUnavailableConformance4a(x: HasUnavailableConformance4) {
112-
takesHorse(x) // expected-error {{conformance of 'HasUnavailableConformance4' to 'Horse' is unavailable in macOS}}
113-
takesHorseExistential(x) // expected-error {{conformance of 'HasUnavailableConformance4' to 'Horse' is unavailable in macOS}}
114-
x.giddyUp() // expected-error {{conformance of 'HasUnavailableConformance4' to 'Horse' is unavailable in macOS}}
115-
_ = x.isGalloping // expected-error {{conformance of 'HasUnavailableConformance4' to 'Horse' is unavailable}}
116-
_ = x[keyPath: \.isGalloping] // expected-error {{conformance of 'HasUnavailableConformance4' to 'Horse' is unavailable}}
117-
_ = UsesHorse<HasUnavailableConformance4>.self // expected-error {{conformance of 'HasUnavailableConformance4' to 'Horse' is unavailable in macOS}}
112+
takesHorse(x)
113+
takesHorseExistential(x)
114+
x.giddyUp()
115+
_ = x.isGalloping
116+
_ = x[keyPath: \.isGalloping]
117+
_ = UsesHorse<HasUnavailableConformance4>.self
118118
}
119119

120120
// Swift obsoleted
121121
public struct HasUnavailableConformance5 {}
122122

123123
@available(swift, obsoleted: 4)
124124
extension HasUnavailableConformance5 : Horse {}
125-
// expected-note@-1 12{{conformance of 'HasUnavailableConformance5' to 'Horse' was obsoleted in Swift 4}}
125+
// expected-note@-1 6{{conformance of 'HasUnavailableConformance5' to 'Horse' was obsoleted in Swift 4}}
126126

127127
func passUnavailableConformance5(x: HasUnavailableConformance5) {
128128
takesHorse(x) // expected-error {{conformance of 'HasUnavailableConformance5' to 'Horse' is unavailable}}
@@ -135,12 +135,12 @@ func passUnavailableConformance5(x: HasUnavailableConformance5) {
135135

136136
@available(swift, obsoleted: 4)
137137
func passUnavailableConformance5a(x: HasUnavailableConformance5) {
138-
takesHorse(x) // expected-error {{conformance of 'HasUnavailableConformance5' to 'Horse' is unavailable}}
139-
takesHorseExistential(x) // expected-error {{conformance of 'HasUnavailableConformance5' to 'Horse' is unavailable}}
140-
x.giddyUp() // expected-error {{conformance of 'HasUnavailableConformance5' to 'Horse' is unavailable}}
141-
_ = x.isGalloping // expected-error {{conformance of 'HasUnavailableConformance5' to 'Horse' is unavailable}}
142-
_ = x[keyPath: \.isGalloping] // expected-error {{conformance of 'HasUnavailableConformance5' to 'Horse' is unavailable}}
143-
_ = UsesHorse<HasUnavailableConformance5>.self // expected-error {{conformance of 'HasUnavailableConformance5' to 'Horse' is unavailable}}
138+
takesHorse(x)
139+
takesHorseExistential(x)
140+
x.giddyUp()
141+
_ = x.isGalloping
142+
_ = x[keyPath: \.isGalloping]
143+
_ = UsesHorse<HasUnavailableConformance5>.self
144144
}
145145

146146
// Unavailable with message

test/Availability/property_wrapper_availability.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct DeprecatedWrapper<T> {
2323

2424
@available(*, unavailable)
2525
@propertyWrapper
26-
struct UnavailableWrapper<T> { // expected-note 8 {{'UnavailableWrapper' has been explicitly marked unavailable here}}
26+
struct UnavailableWrapper<T> { // expected-note 12 {{'UnavailableWrapper' has been explicitly marked unavailable here}}
2727
var wrappedValue: T
2828
}
2929

@@ -113,8 +113,8 @@ struct UnavailableOnMacOSStruct {
113113
@DeprecatedWrapper var deprecatedExplicit: S
114114
@DeprecatedWrapper var deprecatedInferred = S()
115115

116-
@UnavailableWrapper var unavailableExplicit: S
117-
@UnavailableWrapper var unavailableInferred = S()
116+
@UnavailableWrapper var unavailableExplicit: S // expected-error {{'UnavailableWrapper' is unavailable}}
117+
@UnavailableWrapper var unavailableInferred = S() // expected-error {{'UnavailableWrapper' is unavailable}}
118118

119119
@WrappedValueUnavailableOnMacOS var unavailableWrappedValue: S
120120
@WrappedValueAvailable51 var wrappedValueAavailable51: S
@@ -175,14 +175,14 @@ func unavailableOnMacOSFunc(
175175
@AlwaysAvailableWrapper _ alwaysAvailable: S,
176176
@Available51Wrapper _ available51: S,
177177
@DeprecatedWrapper _ deprecated: S,
178-
@UnavailableWrapper _ unavailable: S,
178+
@UnavailableWrapper _ unavailable: S, // expected-error {{'UnavailableWrapper' is unavailable}}
179179
@WrappedValueUnavailableOnMacOS _ unavailableWrappedValue: S,
180180
@WrappedValueAvailable51 _ wrappedValueAavailable51: S
181181
) {
182182
@AlwaysAvailableWrapper var alwaysAvailableLocal = S()
183183
@Available51Wrapper var available51Local = S()
184184
@DeprecatedWrapper var deprecatedLocal = S()
185-
@UnavailableWrapper var unavailableLocal = S()
185+
@UnavailableWrapper var unavailableLocal = S() // expected-error {{'UnavailableWrapper' is unavailable}}
186186
@WrappedValueUnavailableOnMacOS var unavailableWrappedValueLocal = S()
187187
@WrappedValueAvailable51 var wrappedValueAavailable51 = S()
188188
}

0 commit comments

Comments
 (0)