Skip to content

Commit e097686

Browse files
authored
Merge pull request #38832 from apple/cherry-async-alternative-name
[5.5][Refactoring] Use @available(*, renamed:) to link async alternative
2 parents 41a818a + b0dc998 commit e097686

File tree

67 files changed

+1746
-1287
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1746
-1287
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: 18 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+
const AbstractFunctionDecl *asyncAlternative = nullptr) const;
6186+
61746187
/// Determine whether this function is implicitly known to have its
61756188
/// parameters of function type be @_unsafeSendable.
61766189
///
@@ -6563,6 +6576,10 @@ class AccessorDecl final : public FuncDecl {
65636576
Bits.AccessorDecl.IsTransparentComputed = 1;
65646577
}
65656578

6579+
/// A representation of the name to be displayed to users. \c getNameStr
6580+
/// for anything other than a getter or setter.
6581+
void printUserFacingName(llvm::raw_ostream &out) const;
6582+
65666583
static bool classof(const Decl *D) {
65676584
return D->getKind() == DeclKind::Accessor;
65686585
}

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: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ NOTE(decl_declared_here,none,
3030
"%0 declared here", (DeclName))
3131
NOTE(kind_declared_here,none,
3232
"%0 declared here", (DescriptiveDeclKind))
33+
NOTE(descriptive_decl_declared_here,none,
34+
"'%0' declared here", (StringRef))
3335
NOTE(implicit_member_declared_here,none,
3436
"%1 '%0' is implicitly declared", (StringRef, StringRef))
3537
NOTE(extended_type_declared_here,none,
@@ -3376,32 +3378,6 @@ ERROR(diff_params_clause_param_not_differentiable,none,
33763378
"'Differentiable', but %0 does not conform to 'Differentiable'", (Type))
33773379

33783380
// 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-
34053381
WARNING(warn_use_async_alternative,none,
34063382
"consider using asynchronous alternative function",())
34073383

include/swift/AST/Module.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,6 @@ class ModuleDecl : public DeclContext, public TypeDecl {
758758
void collectBasicSourceFileInfo(
759759
llvm::function_ref<void(const BasicSourceFileInfo &)> callback) const;
760760

761-
public:
762761
/// Retrieve a fingerprint value that summarizes the contents of this module.
763762
///
764763
/// This interface hash a of a module is guaranteed to change if the interface
@@ -771,6 +770,15 @@ class ModuleDecl : public DeclContext, public TypeDecl {
771770
/// contents have been made.
772771
Fingerprint getFingerprint() const;
773772

773+
/// Returns an approximation of whether the given module could be
774+
/// redistributed and consumed by external clients.
775+
///
776+
/// FIXME: The scope of this computation should be limited entirely to
777+
/// RenamedDeclRequest. Unfortunately, it has been co-opted to support the
778+
/// \c SerializeOptionsForDebugging hack. Once this information can be
779+
/// transferred from module files to the dSYMs, remove this.
780+
bool isExternallyConsumed() const;
781+
774782
SourceRange getSourceRange() const { return SourceRange(); }
775783

776784
static bool classof(const DeclContext *DC) {

include/swift/AST/TypeCheckRequests.h

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

2972-
class AsyncAlternativeRequest
2973-
: public SimpleRequest<AsyncAlternativeRequest,
2974-
AbstractFunctionDecl *(AbstractFunctionDecl *),
2972+
class RenamedDeclRequest
2973+
: public SimpleRequest<RenamedDeclRequest,
2974+
ValueDecl *(const ValueDecl *, const AvailableAttr *),
29752975
RequestFlags::Cached> {
29762976
public:
29772977
using SimpleRequest::SimpleRequest;
29782978

29792979
private:
29802980
friend SimpleRequest;
29812981

2982-
AbstractFunctionDecl *evaluate(
2983-
Evaluator &evaluator, AbstractFunctionDecl *attachedFunctionDecl) const;
2982+
ValueDecl *evaluate(Evaluator &evaluator, const ValueDecl *attached,
2983+
const AvailableAttr *attr) const;
29842984

29852985
public:
29862986
bool isCached() const { return true; }

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +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 *),
332+
SWIFT_REQUEST(TypeChecker, RenamedDeclRequest,
333+
ValueDecl *(const ValueDecl *),
334334
Cached, NoLocationInfo)

include/swift/Frontend/Frontend.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -400,15 +400,6 @@ class CompilerInvocation {
400400
SerializationOptions
401401
computeSerializationOptions(const SupplementaryOutputPaths &outs,
402402
const ModuleDecl *module) const;
403-
404-
/// Returns an approximation of whether the given module could be
405-
/// redistributed and consumed by external clients.
406-
///
407-
/// FIXME: The scope of this computation should be limited entirely to
408-
/// PrintAsObjC. Unfortunately, it has been co-opted to support the
409-
/// \c SerializeOptionsForDebugging hack. Once this information can be
410-
/// transferred from module files to the dSYMs, remove this.
411-
bool isModuleExternallyConsumed(const ModuleDecl *mod) const;
412403
};
413404

414405
/// A class which manages the state and execution of the compiler.

include/swift/IDE/Utils.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,10 @@ ClangNode extensionGetClangNode(const ExtensionDecl *ext);
577577

578578
/// Utility for finding the referenced declaration from a call, which might
579579
/// include a second level of function application for a 'self.' expression,
580-
/// or a curry thunk, etc.
581-
std::pair<Type, ConcreteDeclRef> getReferencedDecl(Expr *expr);
580+
/// or a curry thunk, etc. If \p semantic is true then the underlying semantic
581+
/// expression of \p expr is used.
582+
std::pair<Type, ConcreteDeclRef> getReferencedDecl(Expr *expr,
583+
bool semantic = true);
582584

583585
/// Whether the last expression in \p ExprStack is being called.
584586
bool isBeingCalled(ArrayRef<Expr*> ExprStack);

include/swift/PrintAsObjC/PrintAsObjC.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ namespace swift {
2525
/// header.
2626
///
2727
/// Returns true on error.
28-
bool printAsObjC(raw_ostream &out, ModuleDecl *M, StringRef bridgingHeader,
29-
AccessLevel minRequiredAccess);
28+
bool printAsObjC(raw_ostream &out, ModuleDecl *M, StringRef bridgingHeader);
3029
}
3130

3231
#endif

lib/AST/Attr.cpp

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,20 @@ 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: \"";
882+
if (auto *Accessor = dyn_cast<AccessorDecl>(Attr->RenameDecl)) {
883+
SmallString<32> Name;
884+
llvm::raw_svector_ostream OS(Name);
885+
Accessor->printUserFacingName(OS);
886+
Printer << Name.str();
887+
} else {
888+
Printer << Attr->RenameDecl->getName();
889+
}
890+
Printer << "\"";
891+
}
880892

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

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-
11021100
default:
11031101
assert(DeclAttribute::isDeclModifier(getKind()) &&
11041102
"handled above");
@@ -1243,8 +1241,6 @@ StringRef DeclAttribute::getAttrName() const {
12431241
return "derivative";
12441242
case DAK_Transpose:
12451243
return "transpose";
1246-
case DAK_CompletionHandlerAsync:
1247-
return "completionHandlerAsync";
12481244
}
12491245
llvm_unreachable("bad DeclAttrKind");
12501246
}
@@ -1466,21 +1462,32 @@ AvailableAttr::createPlatformAgnostic(ASTContext &C,
14661462
assert(!Obsoleted.empty());
14671463
}
14681464
return new (C) AvailableAttr(
1469-
SourceLoc(), SourceRange(), PlatformKind::none, Message, Rename,
1465+
SourceLoc(), SourceRange(), PlatformKind::none, Message, Rename, nullptr,
14701466
NoVersion, SourceRange(),
14711467
NoVersion, SourceRange(),
14721468
Obsoleted, SourceRange(),
14731469
Kind, /* isImplicit */ false);
14741470
}
14751471

1472+
AvailableAttr *AvailableAttr::createForAlternative(
1473+
ASTContext &C, AbstractFunctionDecl *AsyncFunc) {
1474+
llvm::VersionTuple NoVersion;
1475+
return new (C) AvailableAttr(
1476+
SourceLoc(), SourceRange(), PlatformKind::none, "", "", AsyncFunc,
1477+
NoVersion, SourceRange(),
1478+
NoVersion, SourceRange(),
1479+
NoVersion, SourceRange(),
1480+
PlatformAgnosticAvailabilityKind::None, /*Implicit=*/true);
1481+
}
1482+
14761483
bool AvailableAttr::isActivePlatform(const ASTContext &ctx) const {
14771484
return isPlatformActive(Platform, ctx.LangOpts);
14781485
}
14791486

14801487
AvailableAttr *AvailableAttr::clone(ASTContext &C, bool implicit) const {
14811488
return new (C) AvailableAttr(implicit ? SourceLoc() : AtLoc,
14821489
implicit ? SourceRange() : getRange(),
1483-
Platform, Message, Rename,
1490+
Platform, Message, Rename, RenameDecl,
14841491
Introduced ? *Introduced : llvm::VersionTuple(),
14851492
implicit ? SourceRange() : IntroducedRange,
14861493
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)