Skip to content

Commit 4757eeb

Browse files
committed
Sema: Expose a utility on ExportContext for checking unavailability.
Code that wants to determine whether a given declaration should be considered unavailable should use this new utility, which will take the unavailability of the context of use into consideration. NFC.
1 parent 11304b7 commit 4757eeb

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,18 @@ static bool isInsideCompatibleUnavailableDeclaration(
366366
inheritsAvailabilityFromPlatform(platform, *referencedPlatform));
367367
}
368368

369+
const AvailableAttr *
370+
ExportContext::shouldDiagnoseDeclAsUnavailable(const Decl *D) const {
371+
auto attr = AvailableAttr::isUnavailable(D);
372+
if (!attr)
373+
return nullptr;
374+
375+
if (isInsideCompatibleUnavailableDeclaration(D, *this, attr))
376+
return nullptr;
377+
378+
return attr;
379+
}
380+
369381
static bool shouldAllowReferenceToUnavailableInSwiftDeclaration(
370382
const Decl *D, const ExportContext &where) {
371383
auto *DC = where.getDeclContext();
@@ -2901,17 +2913,10 @@ class UnavailabilityDiagnosticInfo {
29012913
static std::optional<UnavailabilityDiagnosticInfo>
29022914
getExplicitUnavailabilityDiagnosticInfo(const Decl *decl,
29032915
const ExportContext &where) {
2904-
auto *attr = AvailableAttr::isUnavailable(decl);
2916+
auto *attr = where.shouldDiagnoseDeclAsUnavailable(decl);
29052917
if (!attr)
29062918
return std::nullopt;
29072919

2908-
// Calling unavailable code from within code with the same
2909-
// unavailability is OK -- the eventual caller can't call the
2910-
// enclosing code in the same situations it wouldn't be able to
2911-
// call this code.
2912-
if (isInsideCompatibleUnavailableDeclaration(decl, where, attr))
2913-
return std::nullopt;
2914-
29152920
ASTContext &ctx = decl->getASTContext();
29162921
StringRef platform = "";
29172922
StringRef versionedPlatform = "";

lib/Sema/TypeCheckAvailability.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ class ExportContext {
187187
/// Get the ExportabilityReason for diagnostics. If this is 'None', there
188188
/// are no restrictions on referencing unexported declarations.
189189
std::optional<ExportabilityReason> getExportabilityReason() const;
190+
191+
/// If \p decl is unconditionally unavailable in this context, and the context
192+
/// is not also unavailable in the same way, then this returns the specific
193+
/// `@available` attribute that makes the decl unavailable. Otherwise, returns
194+
/// nullptr.
195+
const AvailableAttr *shouldDiagnoseDeclAsUnavailable(const Decl *decl) const;
190196
};
191197

192198
/// Check if a declaration is exported as part of a module's external interface.

0 commit comments

Comments
 (0)