Skip to content

Introduce -warn-soft-deprecated option #75182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,9 @@ namespace swift {
/// Diagnose implicit 'override'.
bool WarnImplicitOverrides = false;

/// Diagnose use of declarations that are soft-deprecated.
bool WarnSoftDeprecated = false;

/// Diagnose uses of NSCoding with classes that have unstable mangled names.
bool EnableNSKeyedArchiverDiagnostics = true;

Expand Down
5 changes: 5 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,11 @@ def warn_implicit_overrides :
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
HelpText<"Warn about implicit overrides of protocol members">;

def warn_soft_deprecated :
Flag<["-"], "warn-soft-deprecated">,
Flags<[FrontendOption, DoesNotAffectIncrementalBuild, HelpHidden]>,
HelpText<"Warn when soft-deprecated declarations are referenced">;

def typo_correction_limit : Separate<["-"], "typo-correction-limit">,
Flags<[FrontendOption, HelpHidden]>,
MetaVarName<"<n>">,
Expand Down
2 changes: 2 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
Opts.WarnImplicitOverrides =
Args.hasArg(OPT_warn_implicit_overrides);

Opts.WarnSoftDeprecated = Args.hasArg(OPT_warn_soft_deprecated);

Opts.EnableNSKeyedArchiverDiagnostics =
Args.hasFlag(OPT_enable_nskeyedarchiver_diagnostics,
OPT_disable_nskeyedarchiver_diagnostics,
Expand Down
10 changes: 9 additions & 1 deletion lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2214,9 +2214,17 @@ void TypeChecker::diagnosePotentialUnavailability(
}

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

if (Ctx.LangOpts.WarnSoftDeprecated) {
// When -warn-soft-deprecated is specified, treat any declaration that is
// deprecated in the future as deprecated.
if (auto *Attr = D->getAttrs().getSoftDeprecated(Ctx))
return Attr;
}

// Treat extensions methods as deprecated if their extension
// is deprecated.
DeclContext *DC = D->getDeclContext();
Expand Down
46 changes: 46 additions & 0 deletions test/Sema/availability_soft_deprecated.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -warn-soft-deprecated -verify-additional-prefix soft-deprecated-

// REQUIRES: OS=macosx || OS=ios || OS=tvos || OS=watchos || OS=xros

@available(*, deprecated)
func alwaysDeprecated() {}

@available(macOS, deprecated: 1.0)
@available(iOS, deprecated: 1.0)
@available(tvOS, deprecated: 1.0)
@available(watchOS, deprecated: 1.0)
@available(visionOS, deprecated: 1.0)
func deprecatedEarly() {}

@available(macOS, deprecated: 10000)
@available(iOS, deprecated: 10000)
@available(tvOS, deprecated: 10000)
@available(watchOS, deprecated: 10000)
@available(visionOS, deprecated: 10000)
func deprecatedFarFuture() {}

protocol Proto {}
struct HasSoftDeprecatedConformanceToProto {}

@available(macOS, deprecated: 10000)
@available(iOS, deprecated: 10000)
@available(tvOS, deprecated: 10000)
@available(watchOS, deprecated: 10000)
@available(visionOS, deprecated: 10000)
extension HasSoftDeprecatedConformanceToProto: Proto {}

func test() {
alwaysDeprecated() // expected-warning {{'alwaysDeprecated()' is deprecated}}
deprecatedEarly() // expected-warning {{'deprecatedEarly()' was deprecated in}}
deprecatedFarFuture() // expected-soft-deprecated-warning {{'deprecatedFarFuture()' was deprecated in}}
let _: any Proto = HasSoftDeprecatedConformanceToProto() // expected-soft-deprecated-warning {{conformance of 'HasSoftDeprecatedConformanceToProto' to 'Proto' was deprecated in}}
}

@available(*, deprecated)
func testDeprecated() {
alwaysDeprecated()
deprecatedEarly()
deprecatedFarFuture()
let _: any Proto = HasSoftDeprecatedConformanceToProto()
}