Skip to content

Commit a8a1eb2

Browse files
authored
Merge pull request #75182 from tshortli/warn-soft-deprecated
Introduce `-warn-soft-deprecated` option
2 parents 4a2942b + 1d715e2 commit a8a1eb2

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

include/swift/Basic/LangOptions.h

+3
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,9 @@ namespace swift {
459459
/// Diagnose implicit 'override'.
460460
bool WarnImplicitOverrides = false;
461461

462+
/// Diagnose use of declarations that are soft-deprecated.
463+
bool WarnSoftDeprecated = false;
464+
462465
/// Diagnose uses of NSCoding with classes that have unstable mangled names.
463466
bool EnableNSKeyedArchiverDiagnostics = true;
464467

include/swift/Option/Options.td

+5
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,11 @@ def warn_implicit_overrides :
842842
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
843843
HelpText<"Warn about implicit overrides of protocol members">;
844844

845+
def warn_soft_deprecated :
846+
Flag<["-"], "warn-soft-deprecated">,
847+
Flags<[FrontendOption, DoesNotAffectIncrementalBuild, HelpHidden]>,
848+
HelpText<"Warn when soft-deprecated declarations are referenced">;
849+
845850
def typo_correction_limit : Separate<["-"], "typo-correction-limit">,
846851
Flags<[FrontendOption, HelpHidden]>,
847852
MetaVarName<"<n>">,

lib/Frontend/CompilerInvocation.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
11641164
Opts.WarnImplicitOverrides =
11651165
Args.hasArg(OPT_warn_implicit_overrides);
11661166

1167+
Opts.WarnSoftDeprecated = Args.hasArg(OPT_warn_soft_deprecated);
1168+
11671169
Opts.EnableNSKeyedArchiverDiagnostics =
11681170
Args.hasFlag(OPT_enable_nskeyedarchiver_diagnostics,
11691171
OPT_disable_nskeyedarchiver_diagnostics,

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)