Skip to content

Commit a67f470

Browse files
committed
Replace @completionHandlerAsync with @available(*, renamed:)
Instead of a new attribute `@completionHandlerAsync`, allow the use of the existing `renamed` parameter of `@available` to specify the asynchronous alternative of a synchronous function. No errors will be output from invalid names as `@completionHandlerAsync` had, but if a function is correctly matched then it will be used to output warnings when using the synchronous function in an asynchronous context (as before). Resolves rdar://80612731
1 parent 0be6a4d commit a67f470

31 files changed

+649
-816
lines changed

include/swift/AST/Attr.def

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -625,11 +625,7 @@ SIMPLE_DECL_ATTR(reasync, AtReasync,
625625
ABIBreakingToAdd | ABIBreakingToRemove | APIBreakingToAdd | APIBreakingToRemove,
626626
110)
627627

628-
DECL_ATTR(completionHandlerAsync, CompletionHandlerAsync,
629-
OnAbstractFunction | ConcurrencyOnly | LongAttribute |
630-
ABIStableToAdd | ABIStableToRemove |
631-
APIStableToAdd | APIStableToRemove,
632-
111)
628+
// 111 was an experimental @completionHandlerAsync and is now unused
633629

634630
CONTEXTUAL_SIMPLE_DECL_ATTR(nonisolated, Nonisolated,
635631
DeclModifier | OnFunc | OnConstructor | OnVar | OnSubscript |

include/swift/AST/Attr.h

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ class AvailableAttr : public DeclAttribute {
626626

627627
AvailableAttr(SourceLoc AtLoc, SourceRange Range,
628628
PlatformKind Platform,
629-
StringRef Message, StringRef Rename,
629+
StringRef Message, StringRef Rename, ValueDecl *RenameDecl,
630630
const llvm::VersionTuple &Introduced,
631631
SourceRange IntroducedRange,
632632
const llvm::VersionTuple &Deprecated,
@@ -636,7 +636,7 @@ class AvailableAttr : public DeclAttribute {
636636
PlatformAgnosticAvailabilityKind PlatformAgnostic,
637637
bool Implicit)
638638
: DeclAttribute(DAK_Available, AtLoc, Range, Implicit),
639-
Message(Message), Rename(Rename),
639+
Message(Message), Rename(Rename), RenameDecl(RenameDecl),
640640
INIT_VER_TUPLE(Introduced), IntroducedRange(IntroducedRange),
641641
INIT_VER_TUPLE(Deprecated), DeprecatedRange(DeprecatedRange),
642642
INIT_VER_TUPLE(Obsoleted), ObsoletedRange(ObsoletedRange),
@@ -657,6 +657,12 @@ class AvailableAttr : public DeclAttribute {
657657
/// the `NS_SWIFT_NAME` annotation in Objective-C.
658658
const StringRef Rename;
659659

660+
/// The declaration referred to by \c Rename. Note that this is only set for
661+
/// deserialized attributes or inferred attributes from ObjectiveC code.
662+
/// \c ValueDecl::getRenamedDecl should be used to find the declaration
663+
/// corresponding to \c Rename.
664+
ValueDecl *RenameDecl;
665+
660666
/// Indicates when the symbol was introduced.
661667
const Optional<llvm::VersionTuple> Introduced;
662668

@@ -743,6 +749,11 @@ class AvailableAttr : public DeclAttribute {
743749
llvm::VersionTuple Obsoleted
744750
= llvm::VersionTuple());
745751

752+
/// Create an AvailableAttr that indicates the given \p AsyncFunc should be
753+
/// preferentially used in async contexts
754+
static AvailableAttr *createForAlternative(ASTContext &C,
755+
AbstractFunctionDecl *AsyncFunc);
756+
746757
AvailableAttr *clone(ASTContext &C, bool implicit) const;
747758

748759
static bool classof(const DeclAttribute *DA) {
@@ -2041,57 +2052,6 @@ class TransposeAttr final
20412052
}
20422053
};
20432054

2044-
/// The `@completionHandlerAsync` attribute marks a function as having an async
2045-
/// alternative, optionally providing a name (for cases when the alternative
2046-
/// has a different name).
2047-
class CompletionHandlerAsyncAttr final : public DeclAttribute {
2048-
public:
2049-
/// Reference to the async alternative function. Only set for deserialized
2050-
/// attributes or inferred attributes from ObjectiveC code.
2051-
AbstractFunctionDecl *AsyncFunctionDecl;
2052-
2053-
/// DeclName of the async function in the attribute. Only set from actual
2054-
/// Swift code, deserialization/ObjectiveC imports will set the decl instead.
2055-
const DeclNameRef AsyncFunctionName;
2056-
2057-
/// Source location of the async function name in the attribute
2058-
const SourceLoc AsyncFunctionNameLoc;
2059-
2060-
/// The index of the completion handler
2061-
const size_t CompletionHandlerIndex;
2062-
2063-
/// Source location of the completion handler index passed to the index
2064-
const SourceLoc CompletionHandlerIndexLoc;
2065-
2066-
CompletionHandlerAsyncAttr(DeclNameRef asyncFunctionName,
2067-
SourceLoc asyncFunctionNameLoc,
2068-
size_t completionHandlerIndex,
2069-
SourceLoc completionHandlerIndexLoc,
2070-
SourceLoc atLoc, SourceRange range)
2071-
: DeclAttribute(DAK_CompletionHandlerAsync, atLoc, range,
2072-
/*implicit*/ false),
2073-
AsyncFunctionDecl(nullptr),
2074-
AsyncFunctionName(asyncFunctionName),
2075-
AsyncFunctionNameLoc(asyncFunctionNameLoc),
2076-
CompletionHandlerIndex(completionHandlerIndex),
2077-
CompletionHandlerIndexLoc(completionHandlerIndexLoc) {}
2078-
2079-
CompletionHandlerAsyncAttr(AbstractFunctionDecl &asyncFunctionDecl,
2080-
size_t completionHandlerIndex,
2081-
SourceLoc completionHandlerIndexLoc,
2082-
SourceLoc atLoc, SourceRange range,
2083-
bool implicit)
2084-
: DeclAttribute(DAK_CompletionHandlerAsync, atLoc, range,
2085-
implicit),
2086-
AsyncFunctionDecl(&asyncFunctionDecl) ,
2087-
CompletionHandlerIndex(completionHandlerIndex),
2088-
CompletionHandlerIndexLoc(completionHandlerIndexLoc) {}
2089-
2090-
static bool classof(const DeclAttribute *DA) {
2091-
return DA->getKind() == DAK_CompletionHandlerAsync;
2092-
}
2093-
};
2094-
20952055
/// Attributes that may be applied to declarations.
20962056
class DeclAttributes {
20972057
/// Linked list of declaration attributes.

include/swift/AST/Decl.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6168,9 +6168,22 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
61686168
/// constructor.
61696169
bool hasDynamicSelfResult() const;
61706170

6171-
6171+
/// The async function marked as the alternative to this function, if any.
61726172
AbstractFunctionDecl *getAsyncAlternative() const;
61736173

6174+
/// If \p asyncAlternative is set, then compare its parameters to this
6175+
/// (presumed synchronous) function's parameters to find the index of the
6176+
/// completion handler parameter. This should be the the only missing
6177+
/// parameter in \p asyncAlternative, ignoring defaulted parameters if they
6178+
/// have the same label. It must have a void-returning function type and be
6179+
/// attributed with @escaping but not @autoclosure.
6180+
///
6181+
/// Returns the last index of the parameter that looks like a completion
6182+
/// handler if \p asyncAlternative is not set (with the same conditions on
6183+
/// its type as above).
6184+
Optional<unsigned> findPotentialCompletionHandlerParam(
6185+
AbstractFunctionDecl *asyncAlternative = nullptr) const;
6186+
61746187
/// Determine whether this function is implicitly known to have its
61756188
/// parameters of function type be @_unsafeSendable.
61766189
///

include/swift/AST/DiagnosticsParse.def

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,11 +1690,6 @@ ERROR(sil_inst_autodiff_invalid_witness_generic_signature,PointsToFirstBadToken,
16901690
"parameters as original function generic signature '%1'",
16911691
(StringRef, StringRef))
16921692

1693-
// completionHandlerAsync
1694-
ERROR(attr_completion_handler_async_invalid_name, none,
1695-
"argument of '%0' attribute must be an identifier or full function name",
1696-
(StringRef))
1697-
16981693
//------------------------------------------------------------------------------
16991694
// MARK: Generics parsing diagnostics
17001695
//------------------------------------------------------------------------------

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3376,32 +3376,6 @@ ERROR(diff_params_clause_param_not_differentiable,none,
33763376
"'Differentiable', but %0 does not conform to 'Differentiable'", (Type))
33773377

33783378
// completionHanderAsync attribute
3379-
ERROR(attr_completion_handler_async_handler_not_func,none,
3380-
"'%0' should be attached to a non-async completion-handler function",
3381-
(DeclAttribute))
3382-
3383-
NOTE(note_attr_function_declared_async,none,
3384-
"function declared async", ())
3385-
3386-
NOTE(note_attr_completion_function_must_return_void,none,
3387-
"completion handler must return 'Void'", ())
3388-
3389-
NOTE(note_attr_completion_handler_async_type_is_not_function,none,
3390-
"%0 is not a function type", (Type))
3391-
3392-
NOTE(note_attr_completion_handler_async_handler_attr_req,none,
3393-
"completion handler must%select{ not|}0 be '@%1'",
3394-
(bool, StringRef))
3395-
3396-
ERROR(attr_completion_handler_async_handler_out_of_range,none,
3397-
"completion handler index out of range of the function parameters", ())
3398-
3399-
ERROR(attr_completion_handler_async_ambiguous_function,none,
3400-
"ambiguous '%0' async function %1", (DeclAttribute, DeclNameRef))
3401-
3402-
ERROR(attr_completion_handler_async_no_suitable_function,none,
3403-
"no corresponding async function named %0", (DeclNameRef))
3404-
34053379
WARNING(warn_use_async_alternative,none,
34063380
"consider using asynchronous alternative function",())
34073381

include/swift/AST/TypeCheckRequests.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2969,23 +2969,6 @@ class ConditionalRequirementsRequest
29692969
bool isCached() const { return true; }
29702970
};
29712971

2972-
class AsyncAlternativeRequest
2973-
: public SimpleRequest<AsyncAlternativeRequest,
2974-
AbstractFunctionDecl *(AbstractFunctionDecl *),
2975-
RequestFlags::Cached> {
2976-
public:
2977-
using SimpleRequest::SimpleRequest;
2978-
2979-
private:
2980-
friend SimpleRequest;
2981-
2982-
AbstractFunctionDecl *evaluate(
2983-
Evaluator &evaluator, AbstractFunctionDecl *attachedFunctionDecl) const;
2984-
2985-
public:
2986-
bool isCached() const { return true; }
2987-
};
2988-
29892972
class RenamedDeclRequest
29902973
: public SimpleRequest<RenamedDeclRequest,
29912974
ValueDecl *(const ValueDecl *, const AvailableAttr *),

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,6 @@ SWIFT_REQUEST(TypeChecker, SynthesizeMainFunctionRequest,
329329
SWIFT_REQUEST(TypeChecker, GetImplicitSendableRequest,
330330
NormalProtocolConformance *(NominalTypeDecl *),
331331
Cached, NoLocationInfo)
332-
SWIFT_REQUEST(TypeChecker, AsyncAlternativeRequest,
333-
AbstractFunctionDecl *(AbstractFunctionDecl *),
334-
Cached, NoLocationInfo)
335332
SWIFT_REQUEST(TypeChecker, RenamedDeclRequest,
336-
ValueDecl *(const ValueDecl *),
337-
Cached, NoLocationInfo)
333+
ValueDecl *(const ValueDecl *),
334+
Cached, NoLocationInfo)

lib/AST/Attr.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,11 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
875875
if (Attr->Obsoleted)
876876
Printer << ", obsoleted: " << Attr->Obsoleted.getValue().getAsString();
877877

878-
if (!Attr->Rename.empty())
878+
if (!Attr->Rename.empty()) {
879879
Printer << ", renamed: \"" << Attr->Rename << "\"";
880+
} else if (Attr->RenameDecl) {
881+
Printer << ", renamed: \"" << Attr->RenameDecl->getName() << "\"";
882+
}
880883

881884
// If there's no message, but this is specifically an imported
882885
// "unavailable in Swift" attribute, synthesize a message to look good in
@@ -1085,20 +1088,6 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
10851088
#include "swift/AST/Attr.def"
10861089
llvm_unreachable("handled above");
10871090

1088-
case DAK_CompletionHandlerAsync: {
1089-
auto *attr = cast<CompletionHandlerAsyncAttr>(this);
1090-
Printer.printAttrName("@completionHandlerAsync");
1091-
Printer << "(\"";
1092-
if (attr->AsyncFunctionDecl) {
1093-
Printer << attr->AsyncFunctionDecl->getName();
1094-
} else {
1095-
Printer << attr->AsyncFunctionName;
1096-
}
1097-
Printer << "\", completionHandlerIndex: " <<
1098-
attr->CompletionHandlerIndex << ')';
1099-
break;
1100-
}
1101-
11021091
default:
11031092
assert(DeclAttribute::isDeclModifier(getKind()) &&
11041093
"handled above");
@@ -1243,8 +1232,6 @@ StringRef DeclAttribute::getAttrName() const {
12431232
return "derivative";
12441233
case DAK_Transpose:
12451234
return "transpose";
1246-
case DAK_CompletionHandlerAsync:
1247-
return "completionHandlerAsync";
12481235
}
12491236
llvm_unreachable("bad DeclAttrKind");
12501237
}
@@ -1466,21 +1453,32 @@ AvailableAttr::createPlatformAgnostic(ASTContext &C,
14661453
assert(!Obsoleted.empty());
14671454
}
14681455
return new (C) AvailableAttr(
1469-
SourceLoc(), SourceRange(), PlatformKind::none, Message, Rename,
1456+
SourceLoc(), SourceRange(), PlatformKind::none, Message, Rename, nullptr,
14701457
NoVersion, SourceRange(),
14711458
NoVersion, SourceRange(),
14721459
Obsoleted, SourceRange(),
14731460
Kind, /* isImplicit */ false);
14741461
}
14751462

1463+
AvailableAttr *AvailableAttr::createForAlternative(
1464+
ASTContext &C, AbstractFunctionDecl *AsyncFunc) {
1465+
llvm::VersionTuple NoVersion;
1466+
return new (C) AvailableAttr(
1467+
SourceLoc(), SourceRange(), PlatformKind::none, "", "", AsyncFunc,
1468+
NoVersion, SourceRange(),
1469+
NoVersion, SourceRange(),
1470+
NoVersion, SourceRange(),
1471+
PlatformAgnosticAvailabilityKind::None, /*Implicit=*/true);
1472+
}
1473+
14761474
bool AvailableAttr::isActivePlatform(const ASTContext &ctx) const {
14771475
return isPlatformActive(Platform, ctx.LangOpts);
14781476
}
14791477

14801478
AvailableAttr *AvailableAttr::clone(ASTContext &C, bool implicit) const {
14811479
return new (C) AvailableAttr(implicit ? SourceLoc() : AtLoc,
14821480
implicit ? SourceRange() : getRange(),
1483-
Platform, Message, Rename,
1481+
Platform, Message, Rename, RenameDecl,
14841482
Introduced ? *Introduced : llvm::VersionTuple(),
14851483
implicit ? SourceRange() : IntroducedRange,
14861484
Deprecated ? *Deprecated : llvm::VersionTuple(),

lib/AST/Availability.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ createAvailableAttr(PlatformKind Platform,
9999
return new (Context) AvailableAttr(
100100
SourceLoc(), SourceRange(), Platform,
101101
/*Message=*/StringRef(),
102-
/*Rename=*/StringRef(),
102+
/*Rename=*/StringRef(), /*RenameDecl=*/nullptr,
103103
Introduced, /*IntroducedRange=*/SourceRange(),
104104
Deprecated, /*DeprecatedRange=*/SourceRange(),
105105
Obsoleted, /*ObsoletedRange=*/SourceRange(),

0 commit comments

Comments
 (0)