Skip to content

Commit 29c1a42

Browse files
authored
Merge pull request #63099 from tshortli/consolidate-availability-from-attr
AST: Consolidate instantiation of `AvailabilityContext` from `AvailabilityAttr`
2 parents 22e5fc5 + 3024feb commit 29c1a42

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

include/swift/AST/Availability.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ class UnavailabilityReason {
224224
/// See #unionWith, #intersectWith, and #constrainWith.
225225
///
226226
/// [lattice]: http://mathworld.wolfram.com/Lattice.html
227+
///
228+
/// NOTE: Generally you should use the utilities on \c AvailabilityInference
229+
/// to create an \c AvailabilityContext, rather than creating one directly.
227230
class AvailabilityContext {
228231
VersionRange OSVersion;
229232
llvm::Optional<bool> SPI;
@@ -345,6 +348,13 @@ class AvailabilityInference {
345348
/// We assume a declaration without an annotation is always available.
346349
static AvailabilityContext availableRange(const Decl *D, ASTContext &C);
347350

351+
/// Returns the availability context for a declaration with the given
352+
/// @available attribute.
353+
///
354+
/// NOTE: The attribute must be active on the current platform.
355+
static AvailabilityContext availableRange(const AvailableAttr *attr,
356+
ASTContext &C);
357+
348358
/// Returns the attribute that should be used to determine the availability
349359
/// range of the given declaration, or nullptr if there is none.
350360
static const AvailableAttr *attrForAnnotatedAvailableRange(const Decl *D,

lib/AST/Availability.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,7 @@ AvailabilityInference::annotatedAvailableRange(const Decl *D, ASTContext &Ctx) {
229229
if (!bestAvailAttr)
230230
return None;
231231

232-
return AvailabilityContext{
233-
VersionRange::allGTE(bestAvailAttr->Introduced.value()),
234-
bestAvailAttr->IsSPI};
232+
return availableRange(bestAvailAttr, Ctx);
235233
}
236234

237235
bool Decl::isAvailableAsSPI() const {
@@ -283,11 +281,8 @@ AvailabilityInference::annotatedAvailableRangeForAttr(const SpecializeAttr* attr
283281
bestAvailAttr = availAttr;
284282
}
285283

286-
if (bestAvailAttr) {
287-
return AvailabilityContext{
288-
VersionRange::allGTE(bestAvailAttr->Introduced.value())
289-
};
290-
}
284+
if (bestAvailAttr)
285+
return availableRange(bestAvailAttr, ctx);
291286

292287
return AvailabilityContext::alwaysAvailable();
293288
}
@@ -320,6 +315,14 @@ AvailabilityContext AvailabilityInference::availableRange(const Decl *D,
320315
return AvailabilityContext::alwaysAvailable();
321316
}
322317

318+
AvailabilityContext
319+
AvailabilityInference::availableRange(const AvailableAttr *attr,
320+
ASTContext &Ctx) {
321+
assert(attr->isActivePlatform(Ctx));
322+
return AvailabilityContext{VersionRange::allGTE(attr->Introduced.value()),
323+
attr->IsSPI};
324+
}
325+
323326
namespace {
324327
/// Infers the availability required to access a type.
325328
class AvailabilityInferenceTypeWalker : public TypeWalker {

lib/Sema/TypeCheckAttr.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,8 +1885,8 @@ void AttributeChecker::visitAvailableAttr(AvailableAttr *attr) {
18851885
// is fully contained within that declaration's range. If there is no such
18861886
// enclosing declaration, then there is nothing to check.
18871887
Optional<AvailabilityContext> EnclosingAnnotatedRange;
1888-
AvailabilityContext AttrRange{
1889-
VersionRange::allGTE(attr->Introduced.value())};
1888+
AvailabilityContext AttrRange =
1889+
AvailabilityInference::availableRange(attr, Ctx);
18901890

18911891
if (auto *parent = getEnclosingDeclForDecl(D)) {
18921892
if (auto enclosingUnavailable = parent->getSemanticUnavailableAttr()) {
@@ -1904,7 +1904,7 @@ void AttributeChecker::visitAvailableAttr(AvailableAttr *attr) {
19041904
const AvailableAttr *enclosingAttr = enclosingAvailable.value().first;
19051905
const Decl *enclosingDecl = enclosingAvailable.value().second;
19061906
EnclosingAnnotatedRange.emplace(
1907-
VersionRange::allGTE(enclosingAttr->Introduced.value()));
1907+
AvailabilityInference::availableRange(enclosingAttr, Ctx));
19081908
if (!AttrRange.isContainedIn(*EnclosingAnnotatedRange)) {
19091909
// Members of extensions of nominal types with available ranges were
19101910
// not diagnosed previously, so only emit a warning in that case.

0 commit comments

Comments
 (0)