Skip to content

Commit 1d715e2

Browse files
committed
Sema: Diagnose references to soft-deprecated declarations.
When `-warn-soft-deprecated` is specified, diagnose references to declarations that are deprecated in future OS versions. Resolves rdar://130424183.
1 parent 10d249b commit 1d715e2

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

lib/Sema/TypeCheckAvailability.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -2214,9 +2214,17 @@ void TypeChecker::diagnosePotentialUnavailability(
22142214
}
22152215

22162216
const AvailableAttr *TypeChecker::getDeprecated(const Decl *D) {
2217-
if (auto *Attr = D->getAttrs().getDeprecated(D->getASTContext()))
2217+
auto &Ctx = D->getASTContext();
2218+
if (auto *Attr = D->getAttrs().getDeprecated(Ctx))
22182219
return Attr;
22192220

2221+
if (Ctx.LangOpts.WarnSoftDeprecated) {
2222+
// When -warn-soft-deprecated is specified, treat any declaration that is
2223+
// deprecated in the future as deprecated.
2224+
if (auto *Attr = D->getAttrs().getSoftDeprecated(Ctx))
2225+
return Attr;
2226+
}
2227+
22202228
// Treat extensions methods as deprecated if their extension
22212229
// is deprecated.
22222230
DeclContext *DC = D->getDeclContext();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// RUN: %target-typecheck-verify-swift
2+
// RUN: %target-typecheck-verify-swift -warn-soft-deprecated -verify-additional-prefix soft-deprecated-
3+
4+
// REQUIRES: OS=macosx || OS=ios || OS=tvos || OS=watchos || OS=xros
5+
6+
@available(*, deprecated)
7+
func alwaysDeprecated() {}
8+
9+
@available(macOS, deprecated: 1.0)
10+
@available(iOS, deprecated: 1.0)
11+
@available(tvOS, deprecated: 1.0)
12+
@available(watchOS, deprecated: 1.0)
13+
@available(visionOS, deprecated: 1.0)
14+
func deprecatedEarly() {}
15+
16+
@available(macOS, deprecated: 10000)
17+
@available(iOS, deprecated: 10000)
18+
@available(tvOS, deprecated: 10000)
19+
@available(watchOS, deprecated: 10000)
20+
@available(visionOS, deprecated: 10000)
21+
func deprecatedFarFuture() {}
22+
23+
protocol Proto {}
24+
struct HasSoftDeprecatedConformanceToProto {}
25+
26+
@available(macOS, deprecated: 10000)
27+
@available(iOS, deprecated: 10000)
28+
@available(tvOS, deprecated: 10000)
29+
@available(watchOS, deprecated: 10000)
30+
@available(visionOS, deprecated: 10000)
31+
extension HasSoftDeprecatedConformanceToProto: Proto {}
32+
33+
func test() {
34+
alwaysDeprecated() // expected-warning {{'alwaysDeprecated()' is deprecated}}
35+
deprecatedEarly() // expected-warning {{'deprecatedEarly()' was deprecated in}}
36+
deprecatedFarFuture() // expected-soft-deprecated-warning {{'deprecatedFarFuture()' was deprecated in}}
37+
let _: any Proto = HasSoftDeprecatedConformanceToProto() // expected-soft-deprecated-warning {{conformance of 'HasSoftDeprecatedConformanceToProto' to 'Proto' was deprecated in}}
38+
}
39+
40+
@available(*, deprecated)
41+
func testDeprecated() {
42+
alwaysDeprecated()
43+
deprecatedEarly()
44+
deprecatedFarFuture()
45+
let _: any Proto = HasSoftDeprecatedConformanceToProto()
46+
}

0 commit comments

Comments
 (0)