Skip to content

Sema: Improve availability diagnostics when -target-min-inlining-version min is specified #58963

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
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
12 changes: 9 additions & 3 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -5547,9 +5547,15 @@ ERROR(availability_decl_only_version_newer, none,
"%0 is only available in %1 %2 or newer",
(DeclName, StringRef, llvm::VersionTuple))

WARNING(availability_decl_only_version_newer_warn, none,
"%0 is only available in %1 %2 or newer",
(DeclName, StringRef, llvm::VersionTuple))
ERROR(availability_decl_only_version_newer_for_clients, none,
"%0 is only available in %1 %2 or newer; clients of %3 may have a lower"
" deployment target",
(DeclName, StringRef, llvm::VersionTuple, ModuleDecl *))

WARNING(availability_decl_only_version_newer_for_clients_warn, none,
"%0 is only available in %1 %2 or newer; clients of %3 may have a lower"
" deployment target",
(DeclName, StringRef, llvm::VersionTuple, ModuleDecl *))

ERROR(availability_opaque_types_only_version_newer, none,
"'some' return types are only available in %0 %1 or newer",
Expand Down
46 changes: 34 additions & 12 deletions lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1967,36 +1967,58 @@ void TypeChecker::checkConcurrencyAvailability(SourceRange ReferenceRange,
}
}

/// Returns the diagnostic to emit for the potentially unavailable decl and sets
/// \p IsError accordingly.
static Diagnostic getPotentialUnavailabilityDiagnostic(
const ValueDecl *D, const DeclContext *ReferenceDC,
const UnavailabilityReason &Reason, bool WarnBeforeDeploymentTarget,
bool &IsError) {
ASTContext &Context = ReferenceDC->getASTContext();
auto Platform = prettyPlatformString(targetPlatform(Context.LangOpts));
auto Version = Reason.getRequiredOSVersionRange().getLowerEndpoint();

if (Version <= AvailabilityContext::forDeploymentTarget(Context)
.getOSVersion()
.getLowerEndpoint()) {
// The required OS version is at or before the deployment target so this
// diagnostic should indicate that the decl could be unavailable to clients
// of the module containing the reference.
IsError = !WarnBeforeDeploymentTarget;

return Diagnostic(
IsError ? diag::availability_decl_only_version_newer_for_clients
: diag::availability_decl_only_version_newer_for_clients_warn,
D->getName(), Platform, Version, ReferenceDC->getParentModule());
}

IsError = true;
return Diagnostic(diag::availability_decl_only_version_newer, D->getName(),
Platform, Version);
}

bool TypeChecker::diagnosePotentialUnavailability(
const ValueDecl *D, SourceRange ReferenceRange,
const DeclContext *ReferenceDC,
const UnavailabilityReason &Reason,
bool WarnBeforeDeploymentTarget) {
ASTContext &Context = ReferenceDC->getASTContext();

bool AsError = true;
auto RequiredRange = Reason.getRequiredOSVersionRange();
bool IsError;
{
if (WarnBeforeDeploymentTarget &&
!RequiredRange.isContainedIn(
AvailabilityContext::forDeploymentTarget(Context).getOSVersion()))
AsError = false;

auto Diag = Context.Diags.diagnose(
ReferenceRange.Start,
AsError ? diag::availability_decl_only_version_newer
: diag::availability_decl_only_version_newer_warn,
D->getName(), prettyPlatformString(targetPlatform(Context.LangOpts)),
Reason.getRequiredOSVersionRange().getLowerEndpoint());
getPotentialUnavailabilityDiagnostic(
D, ReferenceDC, Reason, WarnBeforeDeploymentTarget, IsError));

// Direct a fixit to the error if an existing guard is nearly-correct
if (fixAvailabilityByNarrowingNearbyVersionCheck(
ReferenceRange, ReferenceDC, RequiredRange, Context, Diag))
return AsError;
return IsError;
}

fixAvailability(ReferenceRange, ReferenceDC, RequiredRange, Context);
return AsError;
return IsError;
}

void TypeChecker::diagnosePotentialAccessorUnavailability(
Expand Down
Loading