Skip to content

Commit fd4ce3b

Browse files
committed
[TypeChecker] NFC: Unify logic in checkDeclarationAvailaibility and ConstraintSystem::isDeclUnavailable
1 parent fc05d4a commit fd4ce3b

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
@@ -5085,42 +5085,20 @@ void ConstraintSystem::diagnoseFailureFor(SolutionApplicationTarget target) {
50855085

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

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

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

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

51265104
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)