Skip to content

Commit 5cdcb5d

Browse files
committed
AST: Introduce Decl::getNoAsyncAttr().
1 parent 5143382 commit 5cdcb5d

File tree

5 files changed

+41
-48
lines changed

5 files changed

+41
-48
lines changed

include/swift/AST/Attr.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2931,11 +2931,6 @@ class DeclAttributes {
29312931
findMostSpecificActivePlatform(const ASTContext &ctx,
29322932
bool ignoreAppExtensions = false) const;
29332933

2934-
/// Returns the first @available attribute that indicates
2935-
/// a declaration is unavailable from asynchronous contexts, or null
2936-
/// otherwise.
2937-
const AvailableAttr *getNoAsync(const ASTContext &ctx) const;
2938-
29392934
/// Returns the `@backDeployed` attribute that is active for the current
29402935
/// platform.
29412936
const BackDeployedAttr *getBackDeployed(const ASTContext &ctx,

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
14021402
/// will be deprecated in the future, or `nullptr` otherwise.
14031403
const AvailableAttr *getSoftDeprecatedAttr() const;
14041404

1405+
/// Returns the first @available attribute that indicates this decl is
1406+
/// unavailable from asynchronous contexts, or `nullptr` otherwise.
1407+
const AvailableAttr *getNoAsyncAttr() const;
1408+
14051409
/// Returns true if the decl has been marked unavailable in the Swift language
14061410
/// version that is currently active.
14071411
bool isUnavailableInCurrentSwiftVersion() const;

lib/AST/Attr.cpp

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -404,48 +404,6 @@ DeclAttributes::findMostSpecificActivePlatform(const ASTContext &ctx,
404404
return bestAttr;
405405
}
406406

407-
const AvailableAttr *DeclAttributes::getNoAsync(const ASTContext &ctx) const {
408-
const AvailableAttr *bestAttr = nullptr;
409-
for (const DeclAttribute *attr : *this) {
410-
if (const AvailableAttr *avAttr = dyn_cast<AvailableAttr>(attr)) {
411-
if (avAttr->isInvalid())
412-
continue;
413-
414-
if (avAttr->getPlatformAgnosticAvailability() ==
415-
PlatformAgnosticAvailabilityKind::NoAsync) {
416-
// An API may only be unavailable on specific platforms.
417-
// If it doesn't have a platform associated with it, then it's
418-
// unavailable for all platforms, so we should include it. If it does
419-
// have a platform and we are not that platform, then it doesn't apply
420-
// to us.
421-
const bool isGoodForPlatform =
422-
(avAttr->hasPlatform() && avAttr->isActivePlatform(ctx)) ||
423-
!avAttr->hasPlatform();
424-
425-
if (!isGoodForPlatform)
426-
continue;
427-
428-
if (!bestAttr) {
429-
// If there is no best attr selected
430-
// and the attr either has an active platform, or doesn't have one at
431-
// all, select it.
432-
bestAttr = avAttr;
433-
} else if (bestAttr && avAttr->hasPlatform() &&
434-
bestAttr->hasPlatform() &&
435-
inheritsAvailabilityFromPlatform(avAttr->getPlatform(),
436-
bestAttr->getPlatform())) {
437-
// if they both have a viable platform, use the better one
438-
bestAttr = avAttr;
439-
} else if (avAttr->hasPlatform() && !bestAttr->hasPlatform()) {
440-
// Use the one more specific
441-
bestAttr = avAttr;
442-
}
443-
}
444-
}
445-
}
446-
return bestAttr;
447-
}
448-
449407
const BackDeployedAttr *
450408
DeclAttributes::getBackDeployed(const ASTContext &ctx,
451409
bool forTargetVariant) const {

lib/AST/Availability.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,42 @@ const AvailableAttr *Decl::getSoftDeprecatedAttr() const {
554554
return result;
555555
}
556556

557+
const AvailableAttr *Decl::getNoAsyncAttr() const {
558+
auto &ctx = getASTContext();
559+
const AvailableAttr *bestAttr = nullptr;
560+
561+
for (auto attr :
562+
getAttrs().getAttributes<AvailableAttr, /*AllowInvalid=*/false>()) {
563+
564+
if (attr->getPlatformAgnosticAvailability() !=
565+
PlatformAgnosticAvailabilityKind::NoAsync)
566+
continue;
567+
568+
if (attr->hasPlatform() && !attr->isActivePlatform(ctx))
569+
continue;
570+
571+
if (!bestAttr) {
572+
bestAttr = attr;
573+
continue;
574+
}
575+
576+
if (!bestAttr) {
577+
// If there is no best attr selected and the attr either has an active
578+
// platform, or doesn't have one at all, select it.
579+
bestAttr = attr;
580+
} else if (bestAttr && attr->hasPlatform() && bestAttr->hasPlatform() &&
581+
inheritsAvailabilityFromPlatform(attr->getPlatform(),
582+
bestAttr->getPlatform())) {
583+
// if they both have a viable platform, use the better one
584+
bestAttr = attr;
585+
} else if (attr->hasPlatform() && !bestAttr->hasPlatform()) {
586+
// Use the one more specific
587+
bestAttr = attr;
588+
}
589+
}
590+
return bestAttr;
591+
}
592+
557593
bool Decl::isUnavailableInCurrentSwiftVersion() const {
558594
llvm::VersionTuple vers = getASTContext().LangOpts.EffectiveLanguageVersion;
559595
for (auto attr :

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4110,7 +4110,7 @@ diagnoseDeclAsyncAvailability(const ValueDecl *D, SourceRange R,
41104110
}
41114111

41124112
// @available(noasync) spelling
4113-
if (const AvailableAttr *attr = D->getAttrs().getNoAsync(ctx)) {
4113+
if (const AvailableAttr *attr = D->getNoAsyncAttr()) {
41144114
SourceLoc diagLoc = call ? call->getLoc() : R.Start;
41154115
auto diag = ctx.Diags.diagnose(diagLoc, diag::async_unavailable_decl,
41164116
D, attr->Message);

0 commit comments

Comments
 (0)