Skip to content

Commit d566903

Browse files
authored
Merge pull request #80043 from tshortli/availability-at-location-cleanup
Sema: Clean up some availability type checking APIs
2 parents da31340 + 1945c37 commit d566903

7 files changed

+29
-50
lines changed

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,6 @@ static bool shouldTreatDeclContextAsAsyncForDiagnostics(const DeclContext *DC) {
288288
return DC->isAsyncContext();
289289
}
290290

291-
AvailabilityRange TypeChecker::overApproximateAvailabilityAtLocation(
292-
SourceLoc loc, const DeclContext *DC,
293-
const AvailabilityScope **MostRefined) {
294-
return AvailabilityContext::forLocation(loc, DC, MostRefined)
295-
.getPlatformRange();
296-
}
297-
298291
/// A class that walks the AST to find the innermost (i.e., deepest) node that
299292
/// contains a target SourceRange and matches a particular criterion.
300293
/// This class finds the innermost nodes of interest by walking
@@ -689,8 +682,8 @@ static bool fixAvailabilityByNarrowingNearbyVersionCheck(
689682
return false;
690683

691684
const AvailabilityScope *scope = nullptr;
692-
(void)TypeChecker::overApproximateAvailabilityAtLocation(ReferenceRange.Start,
693-
ReferenceDC, &scope);
685+
(void)AvailabilityContext::forLocation(ReferenceRange.Start, ReferenceDC,
686+
&scope);
694687
if (!scope)
695688
return false;
696689

@@ -866,7 +859,7 @@ static void diagnosePotentialUnavailability(
866859
// FIXME: [availability] Should this take an AvailabilityContext instead of
867860
// AvailabilityRange?
868861
bool TypeChecker::checkAvailability(SourceRange ReferenceRange,
869-
AvailabilityRange RequiredAvailability,
862+
AvailabilityRange PlatformRange,
870863
const DeclContext *ReferenceDC,
871864
llvm::function_ref<InFlightDiagnostic(
872865
AvailabilityDomain, AvailabilityRange)>
@@ -880,24 +873,25 @@ bool TypeChecker::checkAvailability(SourceRange ReferenceRange,
880873
return false;
881874

882875
auto availabilityAtLocation =
883-
TypeChecker::overApproximateAvailabilityAtLocation(ReferenceRange.Start,
884-
ReferenceDC);
885-
if (!availabilityAtLocation.isContainedIn(RequiredAvailability)) {
876+
AvailabilityContext::forLocation(ReferenceRange.Start, ReferenceDC)
877+
.getPlatformRange();
878+
879+
if (!availabilityAtLocation.isContainedIn(PlatformRange)) {
886880
diagnosePotentialUnavailability(ReferenceRange, Diagnose, ReferenceDC,
887-
domain, RequiredAvailability);
881+
domain, PlatformRange);
888882
return true;
889883
}
890884

891885
return false;
892886
}
893887

894888
bool TypeChecker::checkAvailability(
895-
SourceRange ReferenceRange, AvailabilityRange RequiredAvailability,
889+
SourceRange ReferenceRange, AvailabilityRange PlatformRange,
896890
Diag<AvailabilityDomain, AvailabilityRange> Diag,
897891
const DeclContext *ReferenceDC) {
898892
auto &Diags = ReferenceDC->getASTContext().Diags;
899893
return TypeChecker::checkAvailability(
900-
ReferenceRange, RequiredAvailability, ReferenceDC,
894+
ReferenceRange, PlatformRange, ReferenceDC,
901895
[&](AvailabilityDomain domain, AvailabilityRange range) {
902896
return Diags.diagnose(ReferenceRange.Start, Diag, domain, range);
903897
});
@@ -1494,18 +1488,19 @@ static void diagnoseIfDeprecated(SourceRange ReferenceRange,
14941488
if (!Attr)
14951489
return;
14961490

1491+
auto Availability = Where.getAvailability();
1492+
14971493
// We match the behavior of clang to not report deprecation warnings
14981494
// inside declarations that are themselves deprecated on all deployment
14991495
// targets.
1500-
if (Where.isDeprecated()) {
1496+
if (Availability.isDeprecated()) {
15011497
return;
15021498
}
15031499

15041500
auto *ReferenceDC = Where.getDeclContext();
15051501
auto &Context = ReferenceDC->getASTContext();
15061502
if (!Context.LangOpts.DisableAvailabilityChecking) {
1507-
AvailabilityRange RunningOSVersions = Where.getAvailabilityRange();
1508-
if (RunningOSVersions.isKnownUnreachable()) {
1503+
if (Availability.getPlatformRange().isKnownUnreachable()) {
15091504
// Suppress a deprecation warning if the availability checking machinery
15101505
// thinks the reference program location will not execute on any
15111506
// deployment target for the current platform.
@@ -1573,18 +1568,19 @@ static bool diagnoseIfDeprecated(SourceLoc loc,
15731568
if (!attr)
15741569
return false;
15751570

1571+
auto availability = where.getAvailability();
1572+
15761573
// We match the behavior of clang to not report deprecation warnings
15771574
// inside declarations that are themselves deprecated on all deployment
15781575
// targets.
1579-
if (where.isDeprecated()) {
1576+
if (availability.isDeprecated()) {
15801577
return false;
15811578
}
15821579

15831580
auto *dc = where.getDeclContext();
15841581
auto &ctx = dc->getASTContext();
15851582
if (!ctx.LangOpts.DisableAvailabilityChecking) {
1586-
AvailabilityRange runningOSVersion = where.getAvailabilityRange();
1587-
if (runningOSVersion.isKnownUnreachable()) {
1583+
if (availability.getPlatformRange().isKnownUnreachable()) {
15881584
// Suppress a deprecation warning if the availability checking machinery
15891585
// thinks the reference program location will not execute on any
15901586
// deployment target for the current platform.

lib/Sema/TypeCheckAvailability.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,6 @@ class ExportContext {
164164

165165
AvailabilityContext getAvailability() const { return Availability; }
166166

167-
AvailabilityRange getAvailabilityRange() const {
168-
return Availability.getPlatformRange();
169-
}
170-
171167
/// If not 'None', the context has the inlinable function body restriction.
172168
FragileFunctionKind getFragileFunctionKind() const { return FragileKind; }
173169

@@ -188,10 +184,6 @@ class ExportContext {
188184
/// or declarations from `@_implementationOnly` imports.
189185
bool isExported() const { return Exported; }
190186

191-
/// If true, the context is part of a deprecated declaration and can
192-
/// reference other deprecated declarations without warning.
193-
bool isDeprecated() const { return Availability.isDeprecated(); }
194-
195187
/// If true, the context can only reference exported declarations, either
196188
/// because it is the signature context of an exported declaration, or
197189
/// because it is the function body context of an inlinable function.

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,8 @@ bool IsDefaultActorRequest::evaluate(
220220
auto customExecutorAvailability =
221221
ctx.getConcurrencyDistributedActorWithCustomExecutorAvailability();
222222

223-
auto actorAvailability = TypeChecker::overApproximateAvailabilityAtLocation(
224-
classDecl->getStartLoc(),
225-
classDecl);
223+
auto actorAvailability =
224+
AvailabilityContext::forDeclSignature(classDecl).getPlatformRange();
226225

227226
if (!actorAvailability.isContainedIn(customExecutorAvailability)) {
228227
// Any 'distributed actor' declared with availability lower than the
@@ -1510,8 +1509,7 @@ void swift::tryDiagnoseExecutorConformance(ASTContext &C,
15101509
AvailabilityRange requirementInfo =
15111510
AvailabilityInference::availableRange(moveOnlyEnqueueRequirement);
15121511
AvailabilityRange declInfo =
1513-
TypeChecker::overApproximateAvailabilityAtLocation(
1514-
nominal->getLoc(), dyn_cast<DeclContext>(nominal));
1512+
AvailabilityContext::forDeclSignature(nominal).getPlatformRange();
15151513
canRemoveOldDecls = declInfo.isContainedIn(requirementInfo);
15161514
}
15171515

lib/Sema/TypeCheckDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1732,7 +1732,7 @@ bool TypeChecker::isAvailabilitySafeForConformance(
17321732
requirementInfo.constrainWith(infoForConformingDecl);
17331733

17341734
AvailabilityRange infoForProtocolDecl =
1735-
overApproximateAvailabilityAtLocation(proto->getLoc(), proto);
1735+
AvailabilityContext::forDeclSignature(proto).getPlatformRange();
17361736

17371737
witnessInfo.constrainWith(infoForProtocolDecl);
17381738
requirementInfo.constrainWith(infoForProtocolDecl);

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,7 +1904,7 @@ RequirementCheck WitnessChecker::checkWitness(ValueDecl *requirement,
19041904
isDefaultWitness = isa<ProtocolDecl>(nominal);
19051905
if (isDefaultWitness && match.Witness->isDeprecated() &&
19061906
!requirement->isDeprecated()) {
1907-
auto conformanceContext = ExportContext::forConformance(DC, Proto);
1907+
auto conformanceContext = AvailabilityContext::forDeclSignature(DC->getInnermostDeclarationDeclContext());
19081908
if (!conformanceContext.isDeprecated()) {
19091909
return RequirementCheck(CheckKind::DefaultWitnessDeprecated);
19101910
}
@@ -5210,8 +5210,8 @@ static void ensureRequirementsAreSatisfied(ASTContext &ctx,
52105210
if (auto depMemberType = depTy->getAs<DependentMemberType>()) {
52115211
auto assocType = depMemberType->getAssocType();
52125212
availability.intersectWith(
5213-
TypeChecker::overApproximateAvailabilityAtLocation(
5214-
assocType->getLoc(), assocType->getDeclContext()));
5213+
AvailabilityContext::forDeclSignature(assocType)
5214+
.getPlatformRange());
52155215
}
52165216

52175217
diagnoseConformanceAvailability(

lib/Sema/TypeCheckStmt.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3299,7 +3299,8 @@ FuncDecl *TypeChecker::getForEachIteratorNextFunction(
32993299

33003300
// We can only call next(isolation:) if we are in an availability context
33013301
// that supports typed throws.
3302-
auto availability = overApproximateAvailabilityAtLocation(loc, dc);
3302+
auto availability =
3303+
AvailabilityContext::forLocation(loc, dc).getPlatformRange();
33033304
if (availability.isContainedIn(ctx.getTypedThrowsAvailability()))
33043305
return nextElement;
33053306

lib/Sema/TypeChecker.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,14 +1031,6 @@ bool isAvailabilitySafeForConformance(
10311031
const ValueDecl *witness, const DeclContext *dc,
10321032
AvailabilityRange &requiredAvailability);
10331033

1034-
/// Returns an over-approximation of the range of operating system versions
1035-
/// that could the passed-in location could be executing upon for
1036-
/// the target platform. If MostRefined != nullptr, set to the most-refined
1037-
/// scope found while approximating.
1038-
AvailabilityRange overApproximateAvailabilityAtLocation(
1039-
SourceLoc loc, const DeclContext *DC,
1040-
const AvailabilityScope **MostRefined = nullptr);
1041-
10421034
/// Returns a diagnostic indicating why the declaration cannot be annotated
10431035
/// with an @available() attribute indicating it is potentially unavailable
10441036
/// or None if this is allowed.
@@ -1055,7 +1047,7 @@ diagnosticIfDeclCannotBeUnavailable(const Decl *D, SemanticAvailableAttr attr);
10551047
/// platform are available at the given `SourceRange`. If not, `Diagnose` is
10561048
/// invoked.
10571049
bool checkAvailability(SourceRange ReferenceRange,
1058-
AvailabilityRange RequiredAvailability,
1050+
AvailabilityRange PlatformRange,
10591051
const DeclContext *ReferenceDC,
10601052
llvm::function_ref<InFlightDiagnostic(AvailabilityDomain,
10611053
AvailabilityRange)>
@@ -1065,7 +1057,7 @@ bool checkAvailability(SourceRange ReferenceRange,
10651057
/// platform are available at the given `SourceRange`. If not, `Diag` is
10661058
/// emitted.
10671059
bool checkAvailability(SourceRange ReferenceRange,
1068-
AvailabilityRange RequiredAvailability,
1060+
AvailabilityRange PlatformRange,
10691061
Diag<AvailabilityDomain, AvailabilityRange> Diag,
10701062
const DeclContext *ReferenceDC);
10711063

0 commit comments

Comments
 (0)