Skip to content

Commit 64f9d5b

Browse files
committed
AST: Remove Decl::getSemanticUnavailableAttr().
Also remove the underlying `SemanticUnavailableAttrRequest`, which used memory very inefficiently in order to cache a detailed answer to what was usually a much simpler question. The only remaining use of `Decl::getSemanticUnavailableAttr()` that actually needed to locate the semantic attribute making a declaration unavailable was in `TypeCheckAttr.cpp`. The implementation of the request could just be used directly in that one location. The other remaining callers only needed to know if the decl was unavailable or not, which there are simpler queries for. # Please enter the commit message for your changes. Lines starting
1 parent b2cc10c commit 64f9d5b

File tree

7 files changed

+32
-70
lines changed

7 files changed

+32
-70
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,19 +1442,6 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
14421442
std::optional<std::pair<const AvailableAttr *, const Decl *>>
14431443
getSemanticAvailableRangeAttr() const;
14441444

1445-
/// Retrieve the @available attribute that makes this declaration unavailable,
1446-
/// if any. If \p ignoreAppExtensions is true then attributes for app
1447-
/// extension platforms are ignored.
1448-
///
1449-
/// This attribute may come from an enclosing decl since availability is
1450-
/// inherited. The second member of the returned pair is the decl that owns
1451-
/// the attribute.
1452-
///
1453-
/// Note that this notion of unavailability is broader than that which is
1454-
/// checked by \c isUnavailable().
1455-
std::optional<std::pair<const AvailableAttr *, const Decl *>>
1456-
getSemanticUnavailableAttr(bool ignoreAppExtensions = false) const;
1457-
14581445
/// Returns true if the decl is effectively always unavailable in the current
14591446
/// compilation context. This query differs from \c isUnavailable() because it
14601447
/// takes the availability of parent declarations into account.

include/swift/AST/TypeCheckRequests.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4122,25 +4122,6 @@ class SemanticAvailableRangeAttrRequest
41224122
bool isCached() const { return true; }
41234123
};
41244124

4125-
class SemanticUnavailableAttrRequest
4126-
: public SimpleRequest<SemanticUnavailableAttrRequest,
4127-
std::optional<AvailableAttrDeclPair>(
4128-
const Decl *decl, bool ignoreAppExtensions),
4129-
RequestFlags::Cached> {
4130-
public:
4131-
using SimpleRequest::SimpleRequest;
4132-
4133-
private:
4134-
friend SimpleRequest;
4135-
4136-
std::optional<AvailableAttrDeclPair>
4137-
evaluate(Evaluator &evaluator, const Decl *decl,
4138-
bool ignoreAppExtensions) const;
4139-
4140-
public:
4141-
bool isCached() const { return true; }
4142-
};
4143-
41444125
enum class SemanticDeclAvailability : uint8_t {
41454126
/// The decl is potentially available in some contexts and/or under certain
41464127
/// deployment conditions.

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,6 @@ SWIFT_REQUEST(TypeChecker, RenamedDeclRequest,
472472
SWIFT_REQUEST(TypeChecker, SemanticAvailableRangeAttrRequest,
473473
Optional<AvailableAttrDeclPair>(const Decl *),
474474
Cached, NoLocationInfo)
475-
SWIFT_REQUEST(TypeChecker, SemanticUnavailableAttrRequest,
476-
Optional<AvailableAttrDeclPair>(const Decl *),
477-
Cached, NoLocationInfo)
478475
SWIFT_REQUEST(TypeChecker, SemanticDeclAvailabilityRequest,
479476
SemanticDeclAvailability(const Decl *),
480477
Cached, NoLocationInfo)

lib/AST/Availability.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -689,28 +689,6 @@ const AvailableAttr *Decl::getUnavailableAttr(bool ignoreAppExtensions) const {
689689
return nullptr;
690690
}
691691

692-
std::optional<AvailableAttrDeclPair>
693-
SemanticUnavailableAttrRequest::evaluate(Evaluator &evaluator, const Decl *decl,
694-
bool ignoreAppExtensions) const {
695-
// Directly marked unavailable.
696-
if (auto attr = decl->getUnavailableAttr(ignoreAppExtensions))
697-
return std::make_pair(attr, decl);
698-
699-
if (auto *parent =
700-
AvailabilityInference::parentDeclForInferredAvailability(decl))
701-
return parent->getSemanticUnavailableAttr(ignoreAppExtensions);
702-
703-
return std::nullopt;
704-
}
705-
706-
std::optional<AvailableAttrDeclPair>
707-
Decl::getSemanticUnavailableAttr(bool ignoreAppExtensions) const {
708-
auto &eval = getASTContext().evaluator;
709-
return evaluateOrDefault(
710-
eval, SemanticUnavailableAttrRequest{this, ignoreAppExtensions},
711-
std::nullopt);
712-
}
713-
714692
static bool isDeclCompletelyUnavailable(const Decl *decl) {
715693
// Don't trust unavailability on declarations from clang modules.
716694
if (isa<ClangModuleUnit>(decl->getDeclContext()->getModuleScopeContext()))

lib/AST/Decl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,8 +1299,7 @@ bool Decl::isAlwaysWeakImported() const {
12991299

13001300
// FIXME: Weak linking on Windows is not yet supported
13011301
// https://github.com/apple/swift/issues/53303
1302-
if (getSemanticUnavailableAttr() &&
1303-
!getASTContext().LangOpts.Target.isOSWindows())
1302+
if (isUnavailable() && !getASTContext().LangOpts.Target.isOSWindows())
13041303
return true;
13051304

13061305
if (auto *accessor = dyn_cast<AccessorDecl>(this))

lib/Sema/TypeCheckAttr.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4795,10 +4795,12 @@ void AttributeChecker::checkBackDeployedAttrs(
47954795
if (Attr != ActiveAttr)
47964796
continue;
47974797

4798+
auto availability =
4799+
TypeChecker::availabilityAtLocation(D->getLoc(), D->getDeclContext());
4800+
47984801
// Unavailable decls cannot be back deployed.
4799-
if (auto unavailableAttrPair = VD->getSemanticUnavailableAttr()) {
4800-
auto unavailableAttr = unavailableAttrPair.value().first;
4801-
if (!inheritsAvailabilityFromPlatform(unavailableAttr->getPlatform(),
4802+
if (auto unavailablePlatform = availability.getUnavailablePlatformKind()) {
4803+
if (!inheritsAvailabilityFromPlatform(*unavailablePlatform,
48024804
Attr->Platform)) {
48034805
auto platformString = prettyPlatformString(Attr->Platform);
48044806
llvm::VersionTuple ignoredVersion;
@@ -4808,9 +4810,21 @@ void AttributeChecker::checkBackDeployedAttrs(
48084810

48094811
diagnose(AtLoc, diag::attr_has_no_effect_on_unavailable_decl, Attr, VD,
48104812
platformString);
4811-
diagnose(unavailableAttr->AtLoc, diag::availability_marked_unavailable,
4812-
VD)
4813-
.highlight(unavailableAttr->getRange());
4813+
4814+
// Find the attribute that makes the declaration unavailable.
4815+
const Decl *attrDecl = D;
4816+
do {
4817+
if (auto *unavailableAttr = attrDecl->getUnavailableAttr()) {
4818+
diagnose(unavailableAttr->AtLoc,
4819+
diag::availability_marked_unavailable, VD)
4820+
.highlight(unavailableAttr->getRange());
4821+
break;
4822+
}
4823+
4824+
attrDecl = AvailabilityInference::parentDeclForInferredAvailability(
4825+
attrDecl);
4826+
} while (attrDecl);
4827+
48144828
continue;
48154829
}
48164830
}

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -814,11 +814,17 @@ class AvailabilityScopeBuilder : private ASTWalker {
814814
if (D->getDeclContext()->isLocalContext())
815815
return false;
816816

817-
// As a convenience, SPI decls and explicitly unavailable decls are
818-
// constrained to the deployment target. There's not much benefit to
819-
// checking these declarations at a lower availability version floor since
820-
// neither can be used by API clients.
821-
if (D->isSPI() || D->getSemanticUnavailableAttr())
817+
// As a convenience, explicitly unavailable decls are constrained to the
818+
// deployment target. There's not much benefit to checking these decls at a
819+
// lower availability version floor since they can't be invoked by clients.
820+
if (getCurrentScope()->getAvailabilityContext().isUnavailable() ||
821+
D->isUnavailable())
822+
return true;
823+
824+
// To remain compatible with a lot of existing SPIs that are declared
825+
// without availability attributes, constrain them to the deployment target
826+
// too.
827+
if (D->isSPI())
822828
return true;
823829

824830
return !::isExported(D);

0 commit comments

Comments
 (0)