Skip to content

Commit e6741d5

Browse files
committed
AST: Adopt ValueDecl::getRenamedDecl().
This unblocks removing `RenameDecl` from `AvailableAttr`.
1 parent 9d884bf commit e6741d5

File tree

4 files changed

+38
-32
lines changed

4 files changed

+38
-32
lines changed

lib/AST/Attr.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,8 @@ ParsedDeclAttrFilter::operator()(const DeclAttribute *Attr) const {
11131113
return Attr;
11141114
}
11151115

1116-
static void printAvailableAttr(const AvailableAttr *Attr, ASTPrinter &Printer,
1116+
static void printAvailableAttr(const Decl *D, const AvailableAttr *Attr,
1117+
ASTPrinter &Printer,
11171118
const PrintOptions &Options) {
11181119
if (Attr->isLanguageVersionSpecific())
11191120
Printer << "swift";
@@ -1138,17 +1139,19 @@ static void printAvailableAttr(const AvailableAttr *Attr, ASTPrinter &Printer,
11381139

11391140
if (!Attr->Rename.empty()) {
11401141
Printer << ", renamed: \"" << Attr->Rename << "\"";
1141-
} else if (Attr->RenameDecl) {
1142-
Printer << ", renamed: \"";
1143-
if (auto *Accessor = dyn_cast<AccessorDecl>(Attr->RenameDecl)) {
1144-
SmallString<32> Name;
1145-
llvm::raw_svector_ostream OS(Name);
1146-
Accessor->printUserFacingName(OS);
1147-
Printer << Name.str();
1148-
} else {
1149-
Printer << Attr->RenameDecl->getName();
1142+
} else if (auto *VD = dyn_cast<ValueDecl>(D)) {
1143+
if (auto *renamedDecl = VD->getRenamedDecl(Attr)) {
1144+
Printer << ", renamed: \"";
1145+
if (auto *Accessor = dyn_cast<AccessorDecl>(renamedDecl)) {
1146+
SmallString<32> Name;
1147+
llvm::raw_svector_ostream OS(Name);
1148+
Accessor->printUserFacingName(OS);
1149+
Printer << Name.str();
1150+
} else {
1151+
Printer << renamedDecl->getName();
1152+
}
1153+
Printer << "\"";
11501154
}
1151-
Printer << "\"";
11521155
}
11531156

11541157
// If there's no message, but this is specifically an imported
@@ -1365,7 +1368,7 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
13651368
Printer.printAttrName("@available");
13661369
}
13671370
Printer << "(";
1368-
printAvailableAttr(Attr, Printer, Options);
1371+
printAvailableAttr(D, Attr, Printer, Options);
13691372
Printer << ")";
13701373
break;
13711374
}
@@ -1464,7 +1467,7 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
14641467
Printer << "availability: ";
14651468
auto numAttrs = availAttrs.size();
14661469
if (numAttrs == 1) {
1467-
printAvailableAttr(availAttrs[0], Printer, Options);
1470+
printAvailableAttr(D, availAttrs[0], Printer, Options);
14681471
Printer << "; ";
14691472
} else {
14701473
SmallVector<const DeclAttribute *, 8> tmp(availAttrs.begin(),

lib/AST/Availability.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,9 @@ void AvailabilityInference::applyInferredAvailableAttrs(
235235

236236
if (Rename.empty() && !AvAttr->Rename.empty()) {
237237
Rename = AvAttr->Rename;
238-
RenameDecl = AvAttr->RenameDecl;
238+
if (auto *VD = dyn_cast<ValueDecl>(D)) {
239+
RenameDecl = VD->getRenamedDecl(AvAttr);
240+
}
239241
}
240242
}
241243

lib/AST/Decl.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9245,29 +9245,26 @@ AbstractFunctionDecl *AbstractFunctionDecl::getAsyncAlternative() const {
92459245
if (hasAsync())
92469246
return nullptr;
92479247

9248-
const AvailableAttr *avAttr = nullptr;
9248+
// Prefer the first availability attribute with no platform and a valid
9249+
// rename target, falling back to the first with a rename. Note that
9250+
// `getAttrs` is in reverse source order, so the last attribute is the
9251+
// first in source.
9252+
AbstractFunctionDecl *alternative = nullptr;
92499253
for (const auto *attr : getAttrs().getAttributes<AvailableAttr>()) {
9250-
// If there's an attribute with an already-resolved rename decl, use it
9251-
if (attr->RenameDecl) {
9252-
avAttr = attr;
9253-
break;
9254-
}
9254+
if (attr->isNoAsync())
9255+
continue;
92559256

9256-
// Otherwise prefer the first availability attribute with no platform and
9257-
// rename parameter, falling back to the first with a rename. Note that
9258-
// `getAttrs` is in reverse source order, so the last attribute is the
9259-
// first in source
9260-
if (!attr->Rename.empty() &&
9261-
(attr->getPlatform() == PlatformKind::none || !avAttr) &&
9262-
!attr->isNoAsync()) {
9263-
avAttr = attr;
9257+
if (attr->getPlatform() != PlatformKind::none && alternative != nullptr)
9258+
continue;
9259+
9260+
if (auto *renamedDecl = getRenamedDecl(attr)) {
9261+
if (auto *afd = dyn_cast<AbstractFunctionDecl>(renamedDecl)) {
9262+
if (afd->hasAsync())
9263+
alternative = afd;
9264+
}
92649265
}
92659266
}
92669267

9267-
auto *renamedDecl = getRenamedDecl(avAttr);
9268-
auto *alternative = dyn_cast_or_null<AbstractFunctionDecl>(renamedDecl);
9269-
if (!alternative || !alternative->hasAsync())
9270-
return nullptr;
92719268
return alternative;
92729269
}
92739270

test/attr/attr_availability_async_rename.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,14 @@ func defaultedParamsEnd4(newArg: Int, arg: Int = 0) async { }
8989
@available(macOS, deprecated: 12, renamed: "manyAttrsOld()")
9090
@available(*, deprecated)
9191
func manyAttrs(completionHandler: @escaping () -> Void) { }
92+
9293
// expected-note@+2 {{'manyAttrsNew()' declared here}}
9394
@available(SwiftStdlib 5.5, *)
9495
func manyAttrsNew() async { }
9596

97+
@available(SwiftStdlib 5.5, *)
98+
func manyAttrsNewOther() async { }
99+
96100
@available(macOS, introduced: 12, renamed: "platformOnlyNew()")
97101
func platformOnly(completionHandler: @escaping () -> Void) { }
98102
// expected-note@+2 {{'platformOnlyNew()' declared here}}

0 commit comments

Comments
 (0)