Skip to content

Commit 2ca2237

Browse files
committed
AST: Return a SemanticAvailableAttr from Decl::getNoAsyncAttr().
1 parent 0de339d commit 2ca2237

File tree

4 files changed

+25
-26
lines changed

4 files changed

+25
-26
lines changed

include/swift/AST/Attr.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3189,13 +3189,19 @@ class SemanticAvailableAttr final {
31893189
const AvailableAttr *getParsedAttr() const { return attr; }
31903190
const AvailabilityDomain getDomain() const { return domain; }
31913191

3192+
/// Returns the `message:` field of the attribute, or an empty string.
3193+
StringRef getMessage() const { return attr->Message; }
3194+
3195+
/// Returns the `rename:` field of the attribute, or an empty string.
3196+
StringRef getRename() const { return attr->Rename; }
3197+
31923198
/// Returns the platform kind that the attribute applies to, or
31933199
/// `PlatformKind::none` if the attribute is not platform specific.
31943200
bool isPlatformSpecific() const { return getDomain().isPlatform(); }
31953201

31963202
/// Returns the platform kind that the attribute applies to, or
31973203
/// `PlatformKind::none` if the attribute is not platform specific.
3198-
PlatformKind getPlatformKind() const { return getDomain().getPlatformKind(); }
3204+
PlatformKind getPlatform() const { return getDomain().getPlatformKind(); }
31993205

32003206
/// Whether this is a `noasync` attribute.
32013207
bool isNoAsync() const { return attr->isNoAsync(); }

include/swift/AST/Decl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,7 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
14281428

14291429
/// Returns the first @available attribute that indicates this decl is
14301430
/// unavailable from asynchronous contexts, or `nullptr` otherwise.
1431-
const AvailableAttr *getNoAsyncAttr() const;
1431+
std::optional<SemanticAvailableAttr> getNoAsyncAttr() const;
14321432

14331433
/// Returns true if the decl has been marked unavailable in the Swift language
14341434
/// version that is currently active.

lib/AST/Availability.cpp

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -581,33 +581,26 @@ const AvailableAttr *Decl::getSoftDeprecatedAttr() const {
581581
return result;
582582
}
583583

584-
const AvailableAttr *Decl::getNoAsyncAttr() const {
585-
const AvailableAttr *bestAttr = nullptr;
584+
std::optional<SemanticAvailableAttr> Decl::getNoAsyncAttr() const {
585+
std::optional<SemanticAvailableAttr> bestAttr;
586586

587-
for (auto semanticAttr :
588-
getSemanticAvailableAttrs(/*includingInactive=*/false)) {
589-
auto attr = semanticAttr.getParsedAttr();
590-
591-
if (!semanticAttr.isNoAsync())
587+
for (auto attr : getSemanticAvailableAttrs(/*includingInactive=*/false)) {
588+
if (!attr.isNoAsync())
592589
continue;
593590

594591
if (!bestAttr) {
595-
bestAttr = attr;
596-
continue;
597-
}
598-
599-
if (!bestAttr) {
600-
// If there is no best attr selected and the attr either has an active
592+
// If there is no best attr selected and the attr either has an active
601593
// platform, or doesn't have one at all, select it.
602-
bestAttr = attr;
603-
} else if (bestAttr && attr->hasPlatform() && bestAttr->hasPlatform() &&
604-
inheritsAvailabilityFromPlatform(attr->getPlatform(),
594+
bestAttr.emplace(attr);
595+
} else if (bestAttr && attr.isPlatformSpecific() &&
596+
bestAttr->isPlatformSpecific() &&
597+
inheritsAvailabilityFromPlatform(attr.getPlatform(),
605598
bestAttr->getPlatform())) {
606599
// if they both have a viable platform, use the better one
607-
bestAttr = attr;
608-
} else if (attr->hasPlatform() && !bestAttr->hasPlatform()) {
600+
bestAttr.emplace(attr);
601+
} else if (attr.isPlatformSpecific() && !bestAttr->isPlatformSpecific()) {
609602
// Use the one more specific
610-
bestAttr = attr;
603+
bestAttr.emplace(attr);
611604
}
612605
}
613606
return bestAttr;

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4201,14 +4201,14 @@ diagnoseDeclAsyncAvailability(const ValueDecl *D, SourceRange R,
42014201
}
42024202

42034203
// @available(noasync) spelling
4204-
if (const AvailableAttr *attr = D->getNoAsyncAttr()) {
4204+
if (auto attr = D->getNoAsyncAttr()) {
42054205
SourceLoc diagLoc = call ? call->getLoc() : R.Start;
4206-
auto diag = ctx.Diags.diagnose(diagLoc, diag::async_unavailable_decl,
4207-
D, attr->Message);
4206+
auto diag = ctx.Diags.diagnose(diagLoc, diag::async_unavailable_decl, D,
4207+
attr->getMessage());
42084208
diag.warnUntilSwiftVersion(6);
42094209

4210-
if (!attr->Rename.empty()) {
4211-
fixItAvailableAttrRename(diag, R, D, attr, call);
4210+
if (!attr->getRename().empty()) {
4211+
fixItAvailableAttrRename(diag, R, D, attr->getParsedAttr(), call);
42124212
}
42134213
return true;
42144214
}

0 commit comments

Comments
 (0)