Skip to content

Commit 0377ef4

Browse files
committed
Sema: Take a SourceLoc in getUnmetDeclAvailabilityRequirement().
Introduce a convenience overload for the common case where the AvailabilityContext must first be looked up by source location.
1 parent ca34f42 commit 0377ef4

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4682,9 +4682,7 @@ bool ConstraintSystem::isDeclUnavailable(const Decl *D,
46824682
loc = getLoc(anchor);
46834683
}
46844684

4685-
auto availabilityContext = TypeChecker::availabilityAtLocation(loc, DC);
4686-
return getUnmetDeclAvailabilityRequirement(D, DC, availabilityContext)
4687-
.has_value();
4685+
return getUnmetDeclAvailabilityRequirement(D, DC, loc).has_value();
46884686
}
46894687

46904688
bool ConstraintSystem::isConformanceUnavailable(ProtocolConformanceRef conformance,

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3127,6 +3127,15 @@ swift::getUnmetDeclAvailabilityRequirement(
31273127
return std::nullopt;
31283128
}
31293129

3130+
std::optional<UnmetAvailabilityRequirement>
3131+
swift::getUnmetDeclAvailabilityRequirement(const Decl *decl,
3132+
const DeclContext *referenceDC,
3133+
SourceLoc referenceLoc) {
3134+
return getUnmetDeclAvailabilityRequirement(
3135+
decl, referenceDC,
3136+
TypeChecker::availabilityAtLocation(referenceLoc, referenceDC));
3137+
}
3138+
31303139
/// Check if this is a subscript declaration inside String or
31313140
/// Substring that returns String, and if so return true.
31323141
bool isSubscriptReturningString(const ValueDecl *D, ASTContext &Context) {

lib/Sema/TypeCheckAvailability.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,13 @@ getUnmetDeclAvailabilityRequirement(const Decl *decl,
252252
const DeclContext *declContext,
253253
AvailabilityContext availabilityContext);
254254

255+
/// Checks whether a declaration should be considered unavailable when referred
256+
/// to at the given source location in the given decl context and, if so,
257+
/// returns a result that describes the unmet availability requirements.
258+
/// Returns `std::nullopt` if the declaration is available.
259+
std::optional<UnmetAvailabilityRequirement> getUnmetDeclAvailabilityRequirement(
260+
const Decl *decl, const DeclContext *referenceDC, SourceLoc referenceLoc);
261+
255262
/// Diagnose uses of the runtime support of the given type, such as
256263
/// type metadata and dynamic casting.
257264
///

lib/Sema/TypeCheckPattern.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ using namespace swift;
3636
///
3737
/// This requires the getter's body to have a certain syntactic form. It should
3838
/// be kept in sync with importEnumCaseAlias in the ClangImporter library.
39-
static EnumElementDecl *
40-
extractEnumElement(DeclContext *DC, SourceLoc UseLoc,
41-
const VarDecl *constant) {
42-
auto availabilityContext = TypeChecker::availabilityAtLocation(UseLoc, DC);
43-
if (auto requirement = getUnmetDeclAvailabilityRequirement(
44-
constant, DC, availabilityContext)) {
39+
static EnumElementDecl *extractEnumElement(DeclContext *DC, SourceLoc UseLoc,
40+
const VarDecl *constant) {
41+
if (auto requirement =
42+
getUnmetDeclAvailabilityRequirement(constant, DC, UseLoc)) {
4543
// Only diagnose explicit unavailability.
4644
if (!requirement->isConditionallySatisfiable())
4745
diagnoseDeclAvailability(constant, UseLoc, nullptr,

0 commit comments

Comments
 (0)