Skip to content

AST/Sema: Remove unnecessary ASTContext parameters from availability APIs #77014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions include/swift/AST/Availability.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,17 +342,16 @@ class AvailabilityInference {
/// to ToDecl.
static void
applyInferredAvailableAttrs(Decl *ToDecl,
ArrayRef<const Decl *> InferredFromDecls,
ASTContext &Context);
ArrayRef<const Decl *> InferredFromDecls);

static AvailabilityRange inferForType(Type t);

/// Returns the context where a declaration is available
/// We assume a declaration without an annotation is always available.
static AvailabilityRange availableRange(const Decl *D, ASTContext &C);
static AvailabilityRange availableRange(const Decl *D);

/// Returns true is the declaration is `@_spi_available`.
static bool isAvailableAsSPI(const Decl *D, ASTContext &C);
static bool isAvailableAsSPI(const Decl *D);

/// Returns the availability context for a declaration with the given
/// @available attribute.
Expand All @@ -363,14 +362,13 @@ class AvailabilityInference {

/// Returns the attribute that should be used to determine the availability
/// range of the given declaration, or nullptr if there is none.
static const AvailableAttr *attrForAnnotatedAvailableRange(const Decl *D,
ASTContext &Ctx);
static const AvailableAttr *attrForAnnotatedAvailableRange(const Decl *D);

/// Returns the context for which the declaration
/// is annotated as available, or None if the declaration
/// has no availability annotation.
static std::optional<AvailabilityRange>
annotatedAvailableRange(const Decl *D, ASTContext &C);
annotatedAvailableRange(const Decl *D);

static AvailabilityRange
annotatedAvailableRangeForAttr(const SpecializeAttr *attr, ASTContext &ctx);
Expand Down
45 changes: 19 additions & 26 deletions lib/AST/Availability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ static AvailableAttr *createAvailableAttr(PlatformKind Platform,
}

void AvailabilityInference::applyInferredAvailableAttrs(
Decl *ToDecl, ArrayRef<const Decl *> InferredFromDecls,
ASTContext &Context) {
Decl *ToDecl, ArrayRef<const Decl *> InferredFromDecls) {
auto &Context = ToDecl->getASTContext();

// Let the new AvailabilityAttr inherit the message and rename.
// The first encountered message / rename will win; this matches the
Expand Down Expand Up @@ -370,8 +370,8 @@ bool AvailabilityInference::updateBeforePlatformForFallback(
}

const AvailableAttr *
AvailabilityInference::attrForAnnotatedAvailableRange(const Decl *D,
ASTContext &Ctx) {
AvailabilityInference::attrForAnnotatedAvailableRange(const Decl *D) {
ASTContext &Ctx = D->getASTContext();
const AvailableAttr *bestAvailAttr = nullptr;

D = abstractSyntaxDeclForAvailableAttribute(D);
Expand All @@ -395,8 +395,7 @@ AvailabilityInference::attrForAnnotatedAvailableRange(const Decl *D,
std::optional<AvailableAttrDeclPair>
SemanticAvailableRangeAttrRequest::evaluate(Evaluator &evaluator,
const Decl *decl) const {
if (auto attr = AvailabilityInference::attrForAnnotatedAvailableRange(
decl, decl->getASTContext()))
if (auto attr = AvailabilityInference::attrForAnnotatedAvailableRange(decl))
return std::make_pair(attr, decl);

if (auto *parent =
Expand All @@ -414,16 +413,16 @@ Decl::getSemanticAvailableRangeAttr() const {
}

std::optional<AvailabilityRange>
AvailabilityInference::annotatedAvailableRange(const Decl *D, ASTContext &Ctx) {
auto bestAvailAttr = attrForAnnotatedAvailableRange(D, Ctx);
AvailabilityInference::annotatedAvailableRange(const Decl *D) {
auto bestAvailAttr = attrForAnnotatedAvailableRange(D);
if (!bestAvailAttr)
return std::nullopt;

return availableRange(bestAvailAttr, Ctx);
return availableRange(bestAvailAttr, D->getASTContext());
}

bool Decl::isAvailableAsSPI() const {
return AvailabilityInference::isAvailableAsSPI(this, getASTContext());
return AvailabilityInference::isAvailableAsSPI(this);
}

std::optional<AvailableAttrDeclPair>
Expand Down Expand Up @@ -540,9 +539,8 @@ AvailabilityRange AvailabilityInference::annotatedAvailableRangeForAttr(
return AvailabilityRange::alwaysAvailable();
}

static const AvailableAttr *attrForAvailableRange(const Decl *D,
ASTContext &Ctx) {
if (auto attr = AvailabilityInference::attrForAnnotatedAvailableRange(D, Ctx))
static const AvailableAttr *attrForAvailableRange(const Decl *D) {
if (auto attr = AvailabilityInference::attrForAnnotatedAvailableRange(D))
return attr;

// Unlike other declarations, extensions can be used without referring to them
Expand All @@ -555,25 +553,23 @@ static const AvailableAttr *attrForAvailableRange(const Decl *D,

DeclContext *DC = D->getDeclContext();
if (auto *ED = dyn_cast<ExtensionDecl>(DC)) {
if (auto attr =
AvailabilityInference::attrForAnnotatedAvailableRange(ED, Ctx))
if (auto attr = AvailabilityInference::attrForAnnotatedAvailableRange(ED))
return attr;
}

return nullptr;
}

AvailabilityRange AvailabilityInference::availableRange(const Decl *D,
ASTContext &Ctx) {
if (auto attr = attrForAvailableRange(D, Ctx))
return availableRange(attr, Ctx);
AvailabilityRange AvailabilityInference::availableRange(const Decl *D) {
if (auto attr = attrForAvailableRange(D))
return availableRange(attr, D->getASTContext());

// Treat unannotated declarations as always available.
return AvailabilityRange::alwaysAvailable();
}

bool AvailabilityInference::isAvailableAsSPI(const Decl *D, ASTContext &Ctx) {
if (auto attr = attrForAvailableRange(D, Ctx))
bool AvailabilityInference::isAvailableAsSPI(const Decl *D) {
if (auto attr = attrForAvailableRange(D))
return attr->IsSPI;

return false;
Expand All @@ -598,15 +594,12 @@ namespace {
/// Infers the availability required to access a type.
class AvailabilityInferenceTypeWalker : public TypeWalker {
public:
ASTContext &AC;
AvailabilityRange AvailabilityInfo = AvailabilityRange::alwaysAvailable();

AvailabilityInferenceTypeWalker(ASTContext &AC) : AC(AC) {}

Action walkToTypePre(Type ty) override {
if (auto *nominalDecl = ty->getAnyNominal()) {
AvailabilityInfo.intersectWith(
AvailabilityInference::availableRange(nominalDecl, AC));
AvailabilityInference::availableRange(nominalDecl));
}

return Action::Continue;
Expand All @@ -615,7 +608,7 @@ class AvailabilityInferenceTypeWalker : public TypeWalker {
} // end anonymous namespace

AvailabilityRange AvailabilityInference::inferForType(Type t) {
AvailabilityInferenceTypeWalker walker(t->getASTContext());
AvailabilityInferenceTypeWalker walker;
t.walk(walker);
return walker.AvailabilityInfo;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1254,8 +1254,7 @@ AvailabilityRange Decl::getAvailabilityForLinkage() const {
if (auto backDeployVersion = getBackDeployedBeforeOSVersion(ctx))
return AvailabilityRange{VersionRange::allGTE(*backDeployVersion)};

auto containingContext =
AvailabilityInference::annotatedAvailableRange(this, getASTContext());
auto containingContext = AvailabilityInference::annotatedAvailableRange(this);
if (containingContext.has_value()) {
// If this entity comes from the concurrency module, adjust its
// availability for linkage purposes up to Swift 5.5, so that we use
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/DeclContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,7 @@ bool DeclContext::isAlwaysAvailableConformanceContext() const {
auto &ctx = getASTContext();

AvailabilityRange conformanceAvailability{
AvailabilityInference::availableRange(ext, ctx)};
AvailabilityInference::availableRange(ext)};

auto deploymentTarget = AvailabilityRange::forDeploymentTarget(ctx);

Expand Down
6 changes: 2 additions & 4 deletions lib/AST/TypeRefinementContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,8 @@ TypeRefinementContext::getExplicitAvailabilityRange() const {

case Reason::Decl: {
auto decl = Node.getAsDecl();
auto &ctx = decl->getASTContext();
if (auto attr =
AvailabilityInference::attrForAnnotatedAvailableRange(decl, ctx))
return AvailabilityInference::availableRange(attr, ctx);
if (auto attr = AvailabilityInference::attrForAnnotatedAvailableRange(decl))
return AvailabilityInference::availableRange(attr, decl->getASTContext());

return std::nullopt;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ static void inferProtocolMemberAvailability(ClangImporter::Implementation &impl,

const Decl *innermostDecl = dc->getInnermostDeclarationDeclContext();
AvailabilityRange containingDeclRange =
AvailabilityInference::availableRange(innermostDecl, C);
AvailabilityInference::availableRange(innermostDecl);

requiredRange.intersectWith(containingDeclRange);

Expand Down Expand Up @@ -6811,7 +6811,7 @@ bool SwiftDeclConverter::existingConstructorIsWorse(
// other?
llvm::VersionTuple introduced = findLatestIntroduction(objcMethod);
AvailabilityRange existingAvailability =
AvailabilityInference::availableRange(existingCtor, Impl.SwiftContext);
AvailabilityInference::availableRange(existingCtor);
assert(!existingAvailability.isKnownUnreachable());

if (existingAvailability.isAlwaysAvailable()) {
Expand Down Expand Up @@ -9119,7 +9119,7 @@ ClangImporter::Implementation::importMirroredDecl(const clang::NamedDecl *decl,
if (proto->getAttrs().hasAttribute<AvailableAttr>()) {
if (!result->getAttrs().hasAttribute<AvailableAttr>()) {
AvailabilityRange protoRange =
AvailabilityInference::availableRange(proto, SwiftContext);
AvailabilityInference::availableRange(proto);
applyAvailableAttribute(result, protoRange, SwiftContext);
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/SILGen/SILGenConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ static bool isCheckExpectedExecutorIntrinsicAvailable(SILGenModule &SGM) {
if (!C.LangOpts.DisableAvailabilityChecking) {
auto deploymentAvailability = AvailabilityRange::forDeploymentTarget(C);
auto declAvailability =
AvailabilityInference::availableRange(checkExecutor, C);
AvailabilityInference::availableRange(checkExecutor);
return deploymentAvailability.isContainedIn(declAvailability);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/Sema/AssociatedTypeInference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,15 @@ static void recordTypeWitness(NormalProtocolConformance *conformance,
// Only constrain the availability of the typealias by the availability of
// the associated type if the associated type is less available than its
// protocol. This is required for source compatibility.
auto protoAvailability = AvailabilityInference::availableRange(proto, ctx);
auto protoAvailability = AvailabilityInference::availableRange(proto);
auto assocTypeAvailability =
AvailabilityInference::availableRange(assocType, ctx);
AvailabilityInference::availableRange(assocType);
if (protoAvailability.isSupersetOf(assocTypeAvailability)) {
availabilitySources.push_back(assocType);
}

AvailabilityInference::applyInferredAvailableAttrs(
aliasDecl, availabilitySources, ctx);
AvailabilityInference::applyInferredAvailableAttrs(aliasDecl,
availabilitySources);

if (nominal == dc) {
nominal->addMember(aliasDecl);
Expand Down
3 changes: 1 addition & 2 deletions lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,7 @@ configureInheritedDesignatedInitAttributes(ClassDecl *classDecl,
if (auto *parentDecl = classDecl->getInnermostDeclWithAvailability()) {
asAvailableAs.push_back(parentDecl);
}
AvailabilityInference::applyInferredAvailableAttrs(
ctor, asAvailableAs, ctx);
AvailabilityInference::applyInferredAvailableAttrs(ctor, asAvailableAs);
}

// Wire up the overrides.
Expand Down
3 changes: 1 addition & 2 deletions lib/Sema/DerivedConformanceActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ static ValueDecl *deriveActor_unownedExecutor(DerivedConformance &derived) {
if (auto enclosingDecl = property->getInnermostDeclWithAvailability())
asAvailableAs.push_back(enclosingDecl);

AvailabilityInference::applyInferredAvailableAttrs(
property, asAvailableAs, ctx);
AvailabilityInference::applyInferredAvailableAttrs(property, asAvailableAs);

auto getter = derived.addGetterToReadOnlyDerivedProperty(property);
getter->setBodySynthesizer(deriveBodyActor_unownedExecutor);
Expand Down
3 changes: 1 addition & 2 deletions lib/Sema/DerivedConformanceDistributedActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -809,8 +809,7 @@ static ValueDecl *deriveDistributedActor_unownedExecutor(DerivedConformance &der
if (auto enclosingDecl = property->getInnermostDeclWithAvailability())
asAvailableAs.push_back(enclosingDecl);

AvailabilityInference::applyInferredAvailableAttrs(
property, asAvailableAs, ctx);
AvailabilityInference::applyInferredAvailableAttrs(property, asAvailableAs);

auto getter = derived.addGetterToReadOnlyDerivedProperty(property);
getter->setBodySynthesizer(deriveBodyDistributedActor_unownedExecutor);
Expand Down
11 changes: 5 additions & 6 deletions lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ class TypeRefinementContextBuilder : private ASTWalker {

// Declarations with an explicit availability attribute always get a TRC.
AvailabilityRange DeclaredAvailability =
swift::AvailabilityInference::availableRange(D, Context);
swift::AvailabilityInference::availableRange(D);
if (!DeclaredAvailability.isAlwaysAvailable()) {
return TypeRefinementContext::createForDecl(
Context, D, getCurrentTRC(),
Expand Down Expand Up @@ -1431,7 +1431,7 @@ AvailabilityRange TypeChecker::overApproximateAvailabilityAtLocation(
loc = D->getLoc();

std::optional<AvailabilityRange> Info =
AvailabilityInference::annotatedAvailableRange(D, Context);
AvailabilityInference::annotatedAvailableRange(D);

if (Info.has_value()) {
OverApproximateContext.constrainWith(Info.value());
Expand Down Expand Up @@ -1472,7 +1472,7 @@ bool TypeChecker::isDeclarationUnavailable(
}

AvailabilityRange safeRangeUnderApprox{
AvailabilityInference::availableRange(D, Context)};
AvailabilityInference::availableRange(D)};

if (safeRangeUnderApprox.isAlwaysAvailable())
return false;
Expand All @@ -1499,8 +1499,7 @@ TypeChecker::checkDeclarationAvailability(const Decl *D,
if (isDeclarationUnavailable(D, Where.getDeclContext(), [&Where] {
return Where.getAvailabilityRange();
})) {
auto &Context = Where.getDeclContext()->getASTContext();
return AvailabilityInference::availableRange(D, Context);
return AvailabilityInference::availableRange(D);
}

return std::nullopt;
Expand Down Expand Up @@ -4639,7 +4638,7 @@ static bool declNeedsExplicitAvailability(const Decl *decl) {
return false;

// Warn on decls without an introduction version.
auto safeRangeUnderApprox = AvailabilityInference::availableRange(decl, ctx);
auto safeRangeUnderApprox = AvailabilityInference::availableRange(decl);
return safeRangeUnderApprox.isAlwaysAvailable();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1491,7 +1491,7 @@ void swift::tryDiagnoseExecutorConformance(ASTContext &C,
} else {
// Check if the availability of nominal is high enough to be using the ExecutorJob version
AvailabilityRange requirementInfo =
AvailabilityInference::availableRange(moveOnlyEnqueueRequirement, C);
AvailabilityInference::availableRange(moveOnlyEnqueueRequirement);
AvailabilityRange declInfo =
TypeChecker::overApproximateAvailabilityAtLocation(
nominal->getLoc(), dyn_cast<DeclContext>(nominal));
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1696,8 +1696,8 @@ bool TypeChecker::isAvailabilitySafeForConformance(
// range with both the conforming type's available range and the protocol
// declaration's available range.
AvailabilityRange witnessInfo =
AvailabilityInference::availableRange(witness, Context);
requirementInfo = AvailabilityInference::availableRange(requirement, Context);
AvailabilityInference::availableRange(witness);
requirementInfo = AvailabilityInference::availableRange(requirement);

AvailabilityRange infoForConformingDecl =
overApproximateAvailabilityAtLocation(dc->getAsDecl()->getLoc(), dc);
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckDeclObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ static bool checkObjCClassStubAvailability(ASTContext &ctx, const Decl *decl) {
if (deploymentTarget.isContainedIn(stubAvailability))
return true;

auto declAvailability = AvailabilityInference::availableRange(decl, ctx);
auto declAvailability = AvailabilityInference::availableRange(decl);
return declAvailability.isContainedIn(stubAvailability);
}

Expand Down
8 changes: 3 additions & 5 deletions lib/Sema/TypeCheckDeclOverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1800,22 +1800,20 @@ OverrideRequiresKeyword swift::overrideRequiresKeyword(ValueDecl *overridden) {
/// makes it a safe override, given the availability of the base declaration.
static bool isAvailabilitySafeForOverride(ValueDecl *override,
ValueDecl *base) {
ASTContext &ctx = override->getASTContext();

// API availability ranges are contravariant: make sure the version range
// of an overridden declaration is fully contained in the range of the
// overriding declaration.
AvailabilityRange overrideInfo =
AvailabilityInference::availableRange(override, ctx);
AvailabilityRange baseInfo = AvailabilityInference::availableRange(base, ctx);
AvailabilityInference::availableRange(override);
AvailabilityRange baseInfo = AvailabilityInference::availableRange(base);

if (baseInfo.isContainedIn(overrideInfo))
return true;

// Allow overrides that are not as available as the base decl as long as the
// override is as available as its context.
auto overrideTypeAvailability = AvailabilityInference::availableRange(
override->getDeclContext()->getSelfNominalTypeDecl(), ctx);
override->getDeclContext()->getSelfNominalTypeDecl());

return overrideTypeAvailability.isContainedIn(overrideInfo);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3085,8 +3085,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
auto module = AT->getDeclContext()->getParentModule();
if (!defaultType &&
module->getResilienceStrategy() == ResilienceStrategy::Resilient &&
AvailabilityInference::availableRange(proto, Ctx)
.isSupersetOf(AvailabilityInference::availableRange(AT, Ctx))) {
AvailabilityInference::availableRange(proto).isSupersetOf(
AvailabilityInference::availableRange(AT))) {
AT->diagnose(
diag::resilient_associated_type_less_available_requires_default, AT);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6041,7 +6041,7 @@ static void inferStaticInitializeObjCMetadata(ClassDecl *classDecl) {
// a new-enough OS.
if (auto sourceFile = classDecl->getParentSourceFile()) {
AvailabilityRange safeRangeUnderApprox{
AvailabilityInference::availableRange(classDecl, ctx)};
AvailabilityInference::availableRange(classDecl)};
AvailabilityRange runningOSOverApprox =
AvailabilityRange::forDeploymentTarget(ctx);

Expand Down
Loading