Skip to content

Commit e7e9b57

Browse files
committedJul 28, 2021
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 ebd820c commit e7e9b57

31 files changed

+649
-816
lines changed
 

‎include/swift/AST/Attr.def

+1-5
Original file line numberDiff line numberDiff line change
@@ -621,11 +621,7 @@ SIMPLE_DECL_ATTR(reasync, AtReasync,
621621
ABIBreakingToAdd | ABIBreakingToRemove | APIBreakingToAdd | APIBreakingToRemove,
622622
110)
623623

624-
DECL_ATTR(completionHandlerAsync, CompletionHandlerAsync,
625-
OnAbstractFunction | ConcurrencyOnly | LongAttribute |
626-
ABIStableToAdd | ABIStableToRemove |
627-
APIStableToAdd | APIStableToRemove,
628-
111)
624+
// 111 was an experimental @completionHandlerAsync and is now unused
629625

630626
CONTEXTUAL_SIMPLE_DECL_ATTR(nonisolated, Nonisolated,
631627
DeclModifier | OnFunc | OnConstructor | OnVar | OnSubscript |

‎include/swift/AST/Attr.h

+13-53
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ class AvailableAttr : public DeclAttribute {
629629

630630
AvailableAttr(SourceLoc AtLoc, SourceRange Range,
631631
PlatformKind Platform,
632-
StringRef Message, StringRef Rename,
632+
StringRef Message, StringRef Rename, ValueDecl *RenameDecl,
633633
const llvm::VersionTuple &Introduced,
634634
SourceRange IntroducedRange,
635635
const llvm::VersionTuple &Deprecated,
@@ -639,7 +639,7 @@ class AvailableAttr : public DeclAttribute {
639639
PlatformAgnosticAvailabilityKind PlatformAgnostic,
640640
bool Implicit)
641641
: DeclAttribute(DAK_Available, AtLoc, Range, Implicit),
642-
Message(Message), Rename(Rename),
642+
Message(Message), Rename(Rename), RenameDecl(RenameDecl),
643643
INIT_VER_TUPLE(Introduced), IntroducedRange(IntroducedRange),
644644
INIT_VER_TUPLE(Deprecated), DeprecatedRange(DeprecatedRange),
645645
INIT_VER_TUPLE(Obsoleted), ObsoletedRange(ObsoletedRange),
@@ -660,6 +660,12 @@ class AvailableAttr : public DeclAttribute {
660660
/// the `NS_SWIFT_NAME` annotation in Objective-C.
661661
const StringRef Rename;
662662

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

@@ -746,6 +752,11 @@ class AvailableAttr : public DeclAttribute {
746752
llvm::VersionTuple Obsoleted
747753
= llvm::VersionTuple());
748754

755+
/// Create an AvailableAttr that indicates the given \p AsyncFunc should be
756+
/// preferentially used in async contexts
757+
static AvailableAttr *createForAlternative(ASTContext &C,
758+
AbstractFunctionDecl *AsyncFunc);
759+
749760
AvailableAttr *clone(ASTContext &C, bool implicit) const;
750761

751762
static bool classof(const DeclAttribute *DA) {
@@ -2021,57 +2032,6 @@ class TransposeAttr final
20212032
}
20222033
};
20232034

2024-
/// The `@completionHandlerAsync` attribute marks a function as having an async
2025-
/// alternative, optionally providing a name (for cases when the alternative
2026-
/// has a different name).
2027-
class CompletionHandlerAsyncAttr final : public DeclAttribute {
2028-
public:
2029-
/// Reference to the async alternative function. Only set for deserialized
2030-
/// attributes or inferred attributes from ObjectiveC code.
2031-
AbstractFunctionDecl *AsyncFunctionDecl;
2032-
2033-
/// DeclName of the async function in the attribute. Only set from actual
2034-
/// Swift code, deserialization/ObjectiveC imports will set the decl instead.
2035-
const DeclNameRef AsyncFunctionName;
2036-
2037-
/// Source location of the async function name in the attribute
2038-
const SourceLoc AsyncFunctionNameLoc;
2039-
2040-
/// The index of the completion handler
2041-
const size_t CompletionHandlerIndex;
2042-
2043-
/// Source location of the completion handler index passed to the index
2044-
const SourceLoc CompletionHandlerIndexLoc;
2045-
2046-
CompletionHandlerAsyncAttr(DeclNameRef asyncFunctionName,
2047-
SourceLoc asyncFunctionNameLoc,
2048-
size_t completionHandlerIndex,
2049-
SourceLoc completionHandlerIndexLoc,
2050-
SourceLoc atLoc, SourceRange range)
2051-
: DeclAttribute(DAK_CompletionHandlerAsync, atLoc, range,
2052-
/*implicit*/ false),
2053-
AsyncFunctionDecl(nullptr),
2054-
AsyncFunctionName(asyncFunctionName),
2055-
AsyncFunctionNameLoc(asyncFunctionNameLoc),
2056-
CompletionHandlerIndex(completionHandlerIndex),
2057-
CompletionHandlerIndexLoc(completionHandlerIndexLoc) {}
2058-
2059-
CompletionHandlerAsyncAttr(AbstractFunctionDecl &asyncFunctionDecl,
2060-
size_t completionHandlerIndex,
2061-
SourceLoc completionHandlerIndexLoc,
2062-
SourceLoc atLoc, SourceRange range,
2063-
bool implicit)
2064-
: DeclAttribute(DAK_CompletionHandlerAsync, atLoc, range,
2065-
implicit),
2066-
AsyncFunctionDecl(&asyncFunctionDecl) ,
2067-
CompletionHandlerIndex(completionHandlerIndex),
2068-
CompletionHandlerIndexLoc(completionHandlerIndexLoc) {}
2069-
2070-
static bool classof(const DeclAttribute *DA) {
2071-
return DA->getKind() == DAK_CompletionHandlerAsync;
2072-
}
2073-
};
2074-
20752035
/// Attributes that may be applied to declarations.
20762036
class DeclAttributes {
20772037
/// Linked list of declaration attributes.

‎include/swift/AST/Decl.h

+14-1
Original file line numberDiff line numberDiff line change
@@ -6196,9 +6196,22 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
61966196
/// constructor.
61976197
bool hasDynamicSelfResult() const;
61986198

6199-
6199+
/// The async function marked as the alternative to this function, if any.
62006200
AbstractFunctionDecl *getAsyncAlternative() const;
62016201

6202+
/// If \p asyncAlternative is set, then compare its parameters to this
6203+
/// (presumed synchronous) function's parameters to find the index of the
6204+
/// completion handler parameter. This should be the the only missing
6205+
/// parameter in \p asyncAlternative, ignoring defaulted parameters if they
6206+
/// have the same label. It must have a void-returning function type and be
6207+
/// attributed with @escaping but not @autoclosure.
6208+
///
6209+
/// Returns the last index of the parameter that looks like a completion
6210+
/// handler if \p asyncAlternative is not set (with the same conditions on
6211+
/// its type as above).
6212+
Optional<unsigned> findPotentialCompletionHandlerParam(
6213+
AbstractFunctionDecl *asyncAlternative = nullptr) const;
6214+
62026215
/// Determine whether this function is implicitly known to have its
62036216
/// parameters of function type be @_unsafeSendable.
62046217
///

‎include/swift/AST/DiagnosticsParse.def

-5
Original file line numberDiff line numberDiff line change
@@ -1703,11 +1703,6 @@ ERROR(sil_inst_autodiff_invalid_witness_generic_signature,PointsToFirstBadToken,
17031703
"parameters as original function generic signature '%1'",
17041704
(StringRef, StringRef))
17051705

1706-
// completionHandlerAsync
1707-
ERROR(attr_completion_handler_async_invalid_name, none,
1708-
"argument of '%0' attribute must be an identifier or full function name",
1709-
(StringRef))
1710-
17111706
//------------------------------------------------------------------------------
17121707
// MARK: Generics parsing diagnostics
17131708
//------------------------------------------------------------------------------

‎include/swift/AST/DiagnosticsSema.def

-26
Original file line numberDiff line numberDiff line change
@@ -3393,32 +3393,6 @@ ERROR(diff_params_clause_param_not_differentiable,none,
33933393
"'Differentiable', but %0 does not conform to 'Differentiable'", (Type))
33943394

33953395
// completionHanderAsync attribute
3396-
ERROR(attr_completion_handler_async_handler_not_func,none,
3397-
"'%0' should be attached to a non-async completion-handler function",
3398-
(DeclAttribute))
3399-
3400-
NOTE(note_attr_function_declared_async,none,
3401-
"function declared async", ())
3402-
3403-
NOTE(note_attr_completion_function_must_return_void,none,
3404-
"completion handler must return 'Void'", ())
3405-
3406-
NOTE(note_attr_completion_handler_async_type_is_not_function,none,
3407-
"%0 is not a function type", (Type))
3408-
3409-
NOTE(note_attr_completion_handler_async_handler_attr_req,none,
3410-
"completion handler must%select{ not|}0 be '@%1'",
3411-
(bool, StringRef))
3412-
3413-
ERROR(attr_completion_handler_async_handler_out_of_range,none,
3414-
"completion handler index out of range of the function parameters", ())
3415-
3416-
ERROR(attr_completion_handler_async_ambiguous_function,none,
3417-
"ambiguous '%0' async function %1", (DeclAttribute, DeclNameRef))
3418-
3419-
ERROR(attr_completion_handler_async_no_suitable_function,none,
3420-
"no corresponding async function named %0", (DeclNameRef))
3421-
34223396
WARNING(warn_use_async_alternative,none,
34233397
"consider using asynchronous alternative function",())
34243398

‎include/swift/AST/TypeCheckRequests.h

-17
Original file line numberDiff line numberDiff line change
@@ -2986,23 +2986,6 @@ class ConditionalRequirementsRequest
29862986
bool isCached() const { return true; }
29872987
};
29882988

2989-
class AsyncAlternativeRequest
2990-
: public SimpleRequest<AsyncAlternativeRequest,
2991-
AbstractFunctionDecl *(AbstractFunctionDecl *),
2992-
RequestFlags::Cached> {
2993-
public:
2994-
using SimpleRequest::SimpleRequest;
2995-
2996-
private:
2997-
friend SimpleRequest;
2998-
2999-
AbstractFunctionDecl *evaluate(
3000-
Evaluator &evaluator, AbstractFunctionDecl *attachedFunctionDecl) const;
3001-
3002-
public:
3003-
bool isCached() const { return true; }
3004-
};
3005-
30062989
class RenamedDeclRequest
30072990
: public SimpleRequest<RenamedDeclRequest,
30082991
ValueDecl *(const ValueDecl *, const AvailableAttr *),

‎include/swift/AST/TypeCheckerTypeIDZone.def

+2-5
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,6 @@ SWIFT_REQUEST(TypeChecker, SynthesizeMainFunctionRequest,
331331
SWIFT_REQUEST(TypeChecker, GetImplicitSendableRequest,
332332
NormalProtocolConformance *(NominalTypeDecl *),
333333
Cached, NoLocationInfo)
334-
SWIFT_REQUEST(TypeChecker, AsyncAlternativeRequest,
335-
AbstractFunctionDecl *(AbstractFunctionDecl *),
336-
Cached, NoLocationInfo)
337334
SWIFT_REQUEST(TypeChecker, RenamedDeclRequest,
338-
ValueDecl *(const ValueDecl *),
339-
Cached, NoLocationInfo)
335+
ValueDecl *(const ValueDecl *),
336+
Cached, NoLocationInfo)

‎lib/AST/Attr.cpp

+17-19
Original file line numberDiff line numberDiff line change
@@ -881,8 +881,11 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
881881
if (Attr->Obsoleted)
882882
Printer << ", obsoleted: " << Attr->Obsoleted.getValue().getAsString();
883883

884-
if (!Attr->Rename.empty())
884+
if (!Attr->Rename.empty()) {
885885
Printer << ", renamed: \"" << Attr->Rename << "\"";
886+
} else if (Attr->RenameDecl) {
887+
Printer << ", renamed: \"" << Attr->RenameDecl->getName() << "\"";
888+
}
886889

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

1091-
case DAK_CompletionHandlerAsync: {
1092-
auto *attr = cast<CompletionHandlerAsyncAttr>(this);
1093-
Printer.printAttrName("@completionHandlerAsync");
1094-
Printer << "(\"";
1095-
if (attr->AsyncFunctionDecl) {
1096-
Printer << attr->AsyncFunctionDecl->getName();
1097-
} else {
1098-
Printer << attr->AsyncFunctionName;
1099-
}
1100-
Printer << "\", completionHandlerIndex: " <<
1101-
attr->CompletionHandlerIndex << ')';
1102-
break;
1103-
}
1104-
11051094
default:
11061095
assert(DeclAttribute::isDeclModifier(getKind()) &&
11071096
"handled above");
@@ -1237,8 +1226,6 @@ StringRef DeclAttribute::getAttrName() const {
12371226
return "derivative";
12381227
case DAK_Transpose:
12391228
return "transpose";
1240-
case DAK_CompletionHandlerAsync:
1241-
return "completionHandlerAsync";
12421229
}
12431230
llvm_unreachable("bad DeclAttrKind");
12441231
}
@@ -1460,21 +1447,32 @@ AvailableAttr::createPlatformAgnostic(ASTContext &C,
14601447
assert(!Obsoleted.empty());
14611448
}
14621449
return new (C) AvailableAttr(
1463-
SourceLoc(), SourceRange(), PlatformKind::none, Message, Rename,
1450+
SourceLoc(), SourceRange(), PlatformKind::none, Message, Rename, nullptr,
14641451
NoVersion, SourceRange(),
14651452
NoVersion, SourceRange(),
14661453
Obsoleted, SourceRange(),
14671454
Kind, /* isImplicit */ false);
14681455
}
14691456

1457+
AvailableAttr *AvailableAttr::createForAlternative(
1458+
ASTContext &C, AbstractFunctionDecl *AsyncFunc) {
1459+
llvm::VersionTuple NoVersion;
1460+
return new (C) AvailableAttr(
1461+
SourceLoc(), SourceRange(), PlatformKind::none, "", "", AsyncFunc,
1462+
NoVersion, SourceRange(),
1463+
NoVersion, SourceRange(),
1464+
NoVersion, SourceRange(),
1465+
PlatformAgnosticAvailabilityKind::None, /*Implicit=*/true);
1466+
}
1467+
14701468
bool AvailableAttr::isActivePlatform(const ASTContext &ctx) const {
14711469
return isPlatformActive(Platform, ctx.LangOpts);
14721470
}
14731471

14741472
AvailableAttr *AvailableAttr::clone(ASTContext &C, bool implicit) const {
14751473
return new (C) AvailableAttr(implicit ? SourceLoc() : AtLoc,
14761474
implicit ? SourceRange() : getRange(),
1477-
Platform, Message, Rename,
1475+
Platform, Message, Rename, RenameDecl,
14781476
Introduced ? *Introduced : llvm::VersionTuple(),
14791477
implicit ? SourceRange() : IntroducedRange,
14801478
Deprecated ? *Deprecated : llvm::VersionTuple(),

‎lib/AST/Availability.cpp

+1-1
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)