Skip to content

Commit bede9c8

Browse files
authored
Merge pull request #35343 from xedin/refactor-check-availability
[TypeChecker] NFC: Unify logic in `checkDeclarationAvailaibility` and…
2 parents 4337899 + fd4ce3b commit bede9c8

File tree

3 files changed

+42
-44
lines changed

3 files changed

+42
-44
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5084,42 +5084,20 @@ void ConstraintSystem::diagnoseFailureFor(SolutionApplicationTarget target) {
50845084

50855085
bool ConstraintSystem::isDeclUnavailable(const Decl *D,
50865086
ConstraintLocator *locator) const {
5087-
auto &ctx = getASTContext();
5088-
50895087
// First check whether this declaration is universally unavailable.
5090-
if (D->getAttrs().isUnavailable(ctx))
5088+
if (D->getAttrs().isUnavailable(getASTContext()))
50915089
return true;
50925090

5093-
if (ctx.LangOpts.DisableAvailabilityChecking)
5094-
return false;
5095-
5096-
if (!DC->getParentSourceFile()) {
5097-
// We only check availability if this reference is in a source file; we do
5098-
// not check in other kinds of FileUnits.
5099-
return false;
5100-
}
5101-
5102-
AvailabilityContext safeRangeUnderApprox{
5103-
AvailabilityInference::availableRange(D, ctx)};
5104-
if (safeRangeUnderApprox.isAlwaysAvailable())
5105-
return false;
5106-
5107-
SourceLoc loc;
5108-
5109-
if (locator) {
5110-
if (auto anchor = locator->getAnchor())
5111-
loc = getLoc(anchor);
5112-
}
5091+
return TypeChecker::isDeclarationUnavailable(D, DC, [&] {
5092+
SourceLoc loc;
51135093

5114-
AvailabilityContext runningOSOverApprox =
5115-
TypeChecker::overApproximateAvailabilityAtLocation(loc, DC);
5094+
if (locator) {
5095+
if (auto anchor = locator->getAnchor())
5096+
loc = getLoc(anchor);
5097+
}
51165098

5117-
// The reference is safe if an over-approximation of the running OS
5118-
// versions is fully contained within an under-approximation
5119-
// of the versions on which the declaration is available. If this
5120-
// containment cannot be guaranteed, we say the reference is
5121-
// not available.
5122-
return !runningOSOverApprox.isContainedIn(safeRangeUnderApprox);
5099+
return TypeChecker::overApproximateAvailabilityAtLocation(loc, DC);
5100+
});
51235101
}
51245102

51255103
bool ConstraintSystem::isConformanceUnavailable(ProtocolConformanceRef conformance,

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -936,37 +936,51 @@ TypeChecker::overApproximateAvailabilityAtLocation(SourceLoc loc,
936936
return OverApproximateContext;
937937
}
938938

939-
Optional<UnavailabilityReason>
940-
TypeChecker::checkDeclarationAvailability(const Decl *D,
941-
const ExportContext &where) {
942-
auto *referenceDC = where.getDeclContext();
939+
bool TypeChecker::isDeclarationUnavailable(
940+
const Decl *D, const DeclContext *referenceDC,
941+
llvm::function_ref<AvailabilityContext()> getAvailabilityContext) {
943942
ASTContext &Context = referenceDC->getASTContext();
944943
if (Context.LangOpts.DisableAvailabilityChecking) {
945-
return None;
944+
return false;
946945
}
947946

948947
if (!referenceDC->getParentSourceFile()) {
949948
// We only check availability if this reference is in a source file; we do
950949
// not check in other kinds of FileUnits.
951-
return None;
950+
return false;
952951
}
953952

954-
AvailabilityContext runningOSOverApprox =
955-
where.getAvailabilityContext();
956-
957953
AvailabilityContext safeRangeUnderApprox{
958954
AvailabilityInference::availableRange(D, Context)};
959955

956+
if (safeRangeUnderApprox.isAlwaysAvailable())
957+
return false;
958+
959+
AvailabilityContext runningOSOverApprox = getAvailabilityContext();
960+
960961
// The reference is safe if an over-approximation of the running OS
961962
// versions is fully contained within an under-approximation
962963
// of the versions on which the declaration is available. If this
963964
// containment cannot be guaranteed, we say the reference is
964965
// not available.
965-
if (runningOSOverApprox.isContainedIn(safeRangeUnderApprox))
966-
return None;
966+
return !runningOSOverApprox.isContainedIn(safeRangeUnderApprox);
967+
}
967968

968-
VersionRange version = safeRangeUnderApprox.getOSVersion();
969-
return UnavailabilityReason::requiresVersionRange(version);
969+
Optional<UnavailabilityReason>
970+
TypeChecker::checkDeclarationAvailability(const Decl *D,
971+
const ExportContext &Where) {
972+
if (isDeclarationUnavailable(D, Where.getDeclContext(), [&Where] {
973+
return Where.getAvailabilityContext();
974+
})) {
975+
auto &Context = Where.getDeclContext()->getASTContext();
976+
AvailabilityContext safeRangeUnderApprox{
977+
AvailabilityInference::availableRange(D, Context)};
978+
979+
VersionRange version = safeRangeUnderApprox.getOSVersion();
980+
return UnavailabilityReason::requiresVersionRange(version);
981+
}
982+
983+
return None;
970984
}
971985

972986
Optional<UnavailabilityReason>

lib/Sema/TypeChecker.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,12 @@ TypeRefinementContext *getOrBuildTypeRefinementContext(SourceFile *SF);
998998
Optional<Diag<>>
999999
diagnosticIfDeclCannotBePotentiallyUnavailable(const Decl *D);
10001000

1001+
/// Same as \c checkDeclarationAvailability but doesn't give a reason for
1002+
/// unavailability.
1003+
bool isDeclarationUnavailable(
1004+
const Decl *D, const DeclContext *referenceDC,
1005+
llvm::function_ref<AvailabilityContext()> getAvailabilityContext);
1006+
10011007
/// Checks whether a declaration should be considered unavailable when
10021008
/// referred to at the given location and, if so, returns the reason why the
10031009
/// declaration is unavailable. Returns None is the declaration is

0 commit comments

Comments
 (0)