Skip to content

Sema: Downgrade potentially unavailable enum cases to a warning in module interfaces #37843

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
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
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -5354,6 +5354,10 @@ ERROR(availability_enum_element_no_potential,
none, "enum cases with associated values cannot be marked potentially unavailable with "
"'@available'", ())

WARNING(availability_enum_element_no_potential_warn,
none, "enum cases with associated values cannot be marked potentially unavailable with "
"'@available'", ())

ERROR(availability_protocol_requires_version,
none, "protocol %0 requires %1 to be available in %2 %3 and newer",
(Identifier, DeclName, StringRef, llvm::VersionTuple))
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ namespace swift {
/// Should conformance availability violations be diagnosed as errors?
bool EnableConformanceAvailabilityErrors = false;

/// Should potential unavailability on enum cases be downgraded to a warning?
bool WarnOnPotentiallyUnavailableEnumCase = false;

/// Maximum number of typo corrections we are allowed to perform.
/// This is disabled by default until we can get typo-correction working within acceptable performance bounds.
unsigned TypoCorrectionLimit = 0;
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,10 @@ def disable_conformance_availability_errors : Flag<["-"],
"disable-conformance-availability-errors">,
HelpText<"Diagnose conformance availability violations as warnings">;

def warn_on_potentially_unavailable_enum_case : Flag<["-"],
"warn-on-potentially-unavailable-enum-case">,
HelpText<"Downgrade potential unavailability of enum case to a warning">;

def report_errors_to_debugger : Flag<["-"], "report-errors-to-debugger">,
HelpText<"Deprecated, will be removed in future versions.">;

Expand Down
4 changes: 4 additions & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
arguments.push_back("-aarch64-use-tbi");
}

if (output.getPrimaryOutputType() == file_types::TY_SwiftModuleFile) {
arguments.push_back("-warn-on-potentially-unavailable-enum-case");
}

// Enable or disable ObjC interop appropriately for the platform
if (Triple.isOSDarwin()) {
arguments.push_back("-enable-objc-interop");
Expand Down
2 changes: 2 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
= A->getOption().matches(OPT_enable_conformance_availability_errors);
}

Opts.WarnOnPotentiallyUnavailableEnumCase |=
Args.hasArg(OPT_warn_on_potentially_unavailable_enum_case);
if (auto A = Args.getLastArg(OPT_enable_access_control,
OPT_disable_access_control)) {
Opts.EnableAccessControl
Expand Down
17 changes: 14 additions & 3 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3365,6 +3365,8 @@ Type TypeChecker::checkReferenceOwnershipAttr(VarDecl *var, Type type,

Optional<Diag<>>
TypeChecker::diagnosticIfDeclCannotBePotentiallyUnavailable(const Decl *D) {
auto *DC = D->getDeclContext();

if (auto *VD = dyn_cast<VarDecl>(D)) {
if (!VD->hasStorage())
return None;
Expand All @@ -3377,14 +3379,23 @@ TypeChecker::diagnosticIfDeclCannotBePotentiallyUnavailable(const Decl *D) {

// Globals and statics are lazily initialized, so they are safe
// for potential unavailability.
if (!VD->isStatic() && !VD->getDeclContext()->isModuleScopeContext())
if (!VD->isStatic() && !DC->isModuleScopeContext())
return diag::availability_stored_property_no_potential;

} else if (auto *EED = dyn_cast<EnumElementDecl>(D)) {
// An enum element with an associated value cannot be potentially
// unavailable.
if (EED->hasAssociatedValues())
return diag::availability_enum_element_no_potential;
if (EED->hasAssociatedValues()) {
auto &ctx = DC->getASTContext();
auto *SF = DC->getParentSourceFile();

if (SF->Kind == SourceFileKind::Interface ||
ctx.LangOpts.WarnOnPotentiallyUnavailableEnumCase) {
return diag::availability_enum_element_no_potential_warn;
} else {
return diag::availability_enum_element_no_potential;
}
}
}

return None;
Expand Down
4 changes: 4 additions & 0 deletions test/Sema/Inputs/availability_enum_case_other.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public enum Horse {
@available(macOS 100, *)
case kevin(Int)
}
19 changes: 19 additions & 0 deletions test/Sema/availability_enum_case.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %empty-directory(%t)

// RUN: %target-build-swift -emit-module %S/Inputs/availability_enum_case_other.swift -emit-module-interface-path %t/availability_enum_case_other.swiftinterface -swift-version 5 -enable-library-evolution
// RUN: %target-typecheck-verify-swift -I %t

// RUN: %target-build-swift -emit-module %S/Inputs/availability_enum_case_other.swift -emit-module-interface-path %t/availability_enum_case_other.swiftinterface -swift-version 5 -enable-library-evolution -whole-module-optimization
// RUN: %target-typecheck-verify-swift -I %t

// REQUIRES: OS=macosx

import availability_enum_case_other

func ride(horse: Horse) {
// expected-note@-1 {{add @available attribute to enclosing global function}}

_ = Horse.kevin
// expected-error@-1 {{'kevin' is only available in macOS 100 or newer}}
// expected-note@-2 {{add 'if #available' version check}}
}