@@ -1458,7 +1458,7 @@ bool TypeChecker::isDeclarationUnavailable(
1458
1458
return !runningOSOverApprox.isContainedIn (safeRangeUnderApprox);
1459
1459
}
1460
1460
1461
- std::optional<UnavailabilityReason >
1461
+ std::optional<AvailabilityContext >
1462
1462
TypeChecker::checkDeclarationAvailability (const Decl *D,
1463
1463
const ExportContext &Where) {
1464
1464
// Skip computing potential unavailability if the declaration is explicitly
@@ -1471,17 +1471,13 @@ TypeChecker::checkDeclarationAvailability(const Decl *D,
1471
1471
return Where.getAvailabilityContext ();
1472
1472
})) {
1473
1473
auto &Context = Where.getDeclContext ()->getASTContext ();
1474
- AvailabilityContext safeRangeUnderApprox{
1475
- AvailabilityInference::availableRange (D, Context)};
1476
-
1477
- VersionRange version = safeRangeUnderApprox.getVersionRange ();
1478
- return UnavailabilityReason::requiresVersionRange (version);
1474
+ return AvailabilityInference::availableRange (D, Context);
1479
1475
}
1480
1476
1481
1477
return std::nullopt;
1482
1478
}
1483
1479
1484
- std::optional<UnavailabilityReason >
1480
+ std::optional<AvailabilityContext >
1485
1481
TypeChecker::checkConformanceAvailability (const RootProtocolConformance *conf,
1486
1482
const ExtensionDecl *ext,
1487
1483
const ExportContext &where) {
@@ -2069,17 +2065,16 @@ static void fixAvailability(SourceRange ReferenceRange,
2069
2065
2070
2066
void TypeChecker::diagnosePotentialUnavailability (
2071
2067
SourceRange ReferenceRange, Diag<StringRef, llvm::VersionTuple> Diag,
2072
- const DeclContext *ReferenceDC,
2073
- const UnavailabilityReason &Reason) {
2068
+ const DeclContext *ReferenceDC, const AvailabilityContext &Availability) {
2074
2069
ASTContext &Context = ReferenceDC->getASTContext ();
2075
2070
2076
- auto RequiredRange = Reason. getRequiredOSVersionRange ();
2071
+ auto RequiredRange = Availability. getVersionRange ();
2077
2072
{
2078
2073
auto Err =
2079
2074
Context.Diags .diagnose (
2080
2075
ReferenceRange.Start , Diag,
2081
2076
prettyPlatformString (targetPlatform (Context.LangOpts )),
2082
- Reason. getRequiredOSVersionRange () .getLowerEndpoint ());
2077
+ RequiredRange .getLowerEndpoint ());
2083
2078
2084
2079
// Direct a fixit to the error if an existing guard is nearly-correct
2085
2080
if (fixAvailabilityByNarrowingNearbyVersionCheck (
@@ -2090,20 +2085,19 @@ void TypeChecker::diagnosePotentialUnavailability(
2090
2085
}
2091
2086
2092
2087
bool TypeChecker::checkAvailability (SourceRange ReferenceRange,
2093
- AvailabilityContext Availability ,
2088
+ AvailabilityContext RequiredAvailability ,
2094
2089
Diag<StringRef, llvm::VersionTuple> Diag,
2095
2090
const DeclContext *ReferenceDC) {
2096
2091
ASTContext &ctx = ReferenceDC->getASTContext ();
2097
2092
if (ctx.LangOpts .DisableAvailabilityChecking )
2098
2093
return false ;
2099
2094
2100
- auto runningOS =
2101
- TypeChecker::overApproximateAvailabilityAtLocation (
2102
- ReferenceRange. Start , ReferenceDC);
2103
- if (!runningOS .isContainedIn (Availability )) {
2095
+ auto availabilityAtLocation =
2096
+ TypeChecker::overApproximateAvailabilityAtLocation (ReferenceRange. Start ,
2097
+ ReferenceDC);
2098
+ if (!availabilityAtLocation .isContainedIn (RequiredAvailability )) {
2104
2099
diagnosePotentialUnavailability (ReferenceRange, Diag, ReferenceDC,
2105
- UnavailabilityReason::requiresVersionRange (
2106
- Availability.getVersionRange ()));
2100
+ RequiredAvailability);
2107
2101
return true ;
2108
2102
}
2109
2103
@@ -2119,17 +2113,24 @@ void TypeChecker::checkConcurrencyAvailability(SourceRange ReferenceRange,
2119
2113
ReferenceDC);
2120
2114
}
2121
2115
2116
+ static bool
2117
+ requiresDeploymentTargetOrEarlier (const AvailabilityContext &availability,
2118
+ ASTContext &ctx) {
2119
+ auto deploymentTarget = AvailabilityContext::forDeploymentTarget (ctx);
2120
+ return deploymentTarget.isContainedIn (availability);
2121
+ }
2122
+
2122
2123
// / Returns the diagnostic to emit for the potentially unavailable decl and sets
2123
2124
// / \p IsError accordingly.
2124
2125
static Diagnostic getPotentialUnavailabilityDiagnostic (
2125
2126
const ValueDecl *D, const DeclContext *ReferenceDC,
2126
- const UnavailabilityReason &Reason , bool WarnBeforeDeploymentTarget,
2127
+ const AvailabilityContext &Availability , bool WarnBeforeDeploymentTarget,
2127
2128
bool &IsError) {
2128
2129
ASTContext &Context = ReferenceDC->getASTContext ();
2129
2130
auto Platform = prettyPlatformString (targetPlatform (Context.LangOpts ));
2130
- auto Version = Reason. getRequiredOSVersionRange ().getLowerEndpoint ();
2131
+ auto Version = Availability. getVersionRange ().getLowerEndpoint ();
2131
2132
2132
- if (Reason. requiresDeploymentTargetOrEarlier (Context)) {
2133
+ if (requiresDeploymentTargetOrEarlier (Availability, Context)) {
2133
2134
// The required OS version is at or before the deployment target so this
2134
2135
// diagnostic should indicate that the decl could be unavailable to clients
2135
2136
// of the module containing the reference.
@@ -2148,18 +2149,17 @@ static Diagnostic getPotentialUnavailabilityDiagnostic(
2148
2149
2149
2150
bool TypeChecker::diagnosePotentialUnavailability (
2150
2151
const ValueDecl *D, SourceRange ReferenceRange,
2151
- const DeclContext *ReferenceDC,
2152
- const UnavailabilityReason &Reason,
2152
+ const DeclContext *ReferenceDC, const AvailabilityContext &Availability,
2153
2153
bool WarnBeforeDeploymentTarget = false ) {
2154
2154
ASTContext &Context = ReferenceDC->getASTContext ();
2155
2155
2156
- auto RequiredRange = Reason. getRequiredOSVersionRange ();
2156
+ auto RequiredRange = Availability. getVersionRange ();
2157
2157
bool IsError;
2158
2158
{
2159
2159
auto Diag = Context.Diags .diagnose (
2160
2160
ReferenceRange.Start ,
2161
2161
getPotentialUnavailabilityDiagnostic (
2162
- D, ReferenceDC, Reason , WarnBeforeDeploymentTarget, IsError));
2162
+ D, ReferenceDC, Availability , WarnBeforeDeploymentTarget, IsError));
2163
2163
2164
2164
// Direct a fixit to the error if an existing guard is nearly-correct
2165
2165
if (fixAvailabilityByNarrowingNearbyVersionCheck (
@@ -2173,7 +2173,7 @@ bool TypeChecker::diagnosePotentialUnavailability(
2173
2173
2174
2174
void TypeChecker::diagnosePotentialAccessorUnavailability (
2175
2175
const AccessorDecl *Accessor, SourceRange ReferenceRange,
2176
- const DeclContext *ReferenceDC, const UnavailabilityReason &Reason ,
2176
+ const DeclContext *ReferenceDC, const AvailabilityContext &Availability ,
2177
2177
bool ForInout) {
2178
2178
ASTContext &Context = ReferenceDC->getASTContext ();
2179
2179
@@ -2182,13 +2182,13 @@ void TypeChecker::diagnosePotentialAccessorUnavailability(
2182
2182
auto &diag = ForInout ? diag::availability_inout_accessor_only_version_newer
2183
2183
: diag::availability_decl_only_version_newer;
2184
2184
2185
- auto RequiredRange = Reason. getRequiredOSVersionRange ();
2185
+ auto RequiredRange = Availability. getVersionRange ();
2186
2186
{
2187
2187
auto Err =
2188
2188
Context.Diags .diagnose (
2189
2189
ReferenceRange.Start , diag, Accessor,
2190
2190
prettyPlatformString (targetPlatform (Context.LangOpts )),
2191
- Reason. getRequiredOSVersionRange () .getLowerEndpoint ());
2191
+ RequiredRange .getLowerEndpoint ());
2192
2192
2193
2193
2194
2194
// Direct a fixit to the error if an existing guard is nearly-correct
@@ -2221,21 +2221,19 @@ behaviorLimitForExplicitUnavailability(
2221
2221
}
2222
2222
2223
2223
void TypeChecker::diagnosePotentialUnavailability (
2224
- const RootProtocolConformance *rootConf,
2225
- const ExtensionDecl *ext,
2226
- SourceLoc loc,
2227
- const DeclContext *dc,
2228
- const UnavailabilityReason &reason) {
2224
+ const RootProtocolConformance *rootConf, const ExtensionDecl *ext,
2225
+ SourceLoc loc, const DeclContext *dc,
2226
+ const AvailabilityContext &availability) {
2229
2227
ASTContext &ctx = dc->getASTContext ();
2230
2228
2231
- auto requiredRange = reason. getRequiredOSVersionRange ();
2229
+ auto requiredRange = availability. getVersionRange ();
2232
2230
{
2233
2231
auto type = rootConf->getType ();
2234
2232
auto proto = rootConf->getProtocol ()->getDeclaredInterfaceType ();
2235
2233
auto err = ctx.Diags .diagnose (
2236
2234
loc, diag::conformance_availability_only_version_newer, type, proto,
2237
2235
prettyPlatformString (targetPlatform (ctx.LangOpts )),
2238
- reason. getRequiredOSVersionRange () .getLowerEndpoint ());
2236
+ requiredRange .getLowerEndpoint ());
2239
2237
2240
2238
err.warnUntilSwiftVersion (6 );
2241
2239
err.limitBehavior (behaviorLimitForExplicitUnavailability (rootConf, dc));
@@ -4065,20 +4063,22 @@ bool swift::diagnoseDeclAvailability(const ValueDecl *D, SourceRange R,
4065
4063
if (!maybeUnavail.has_value ())
4066
4064
return false ;
4067
4065
4068
- auto unavailReason = maybeUnavail.value ();
4066
+ auto requiredAvailability = maybeUnavail.value ();
4069
4067
auto *DC = Where.getDeclContext ();
4068
+ auto &ctx = DC->getASTContext ();
4070
4069
if (Flags.contains (
4071
4070
DeclAvailabilityFlag::
4072
4071
AllowPotentiallyUnavailableAtOrBelowDeploymentTarget) &&
4073
- unavailReason. requiresDeploymentTargetOrEarlier (DC-> getASTContext () ))
4072
+ requiresDeploymentTargetOrEarlier (requiredAvailability, ctx ))
4074
4073
return false ;
4075
4074
4076
4075
if (accessor) {
4077
4076
bool forInout = Flags.contains (DeclAvailabilityFlag::ForInout);
4078
4077
TypeChecker::diagnosePotentialAccessorUnavailability (
4079
- accessor, R, DC, unavailReason , forInout);
4078
+ accessor, R, DC, requiredAvailability , forInout);
4080
4079
} else {
4081
- if (!TypeChecker::diagnosePotentialUnavailability (D, R, DC, unavailReason))
4080
+ if (!TypeChecker::diagnosePotentialUnavailability (D, R, DC,
4081
+ requiredAvailability))
4082
4082
return false ;
4083
4083
}
4084
4084
0 commit comments