Skip to content

Simplify get async alternative #60985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6827,7 +6827,7 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
bool hasDynamicSelfResult() const;

/// The async function marked as the alternative to this function, if any.
AbstractFunctionDecl *getAsyncAlternative(bool isKnownObjC = false) const;
AbstractFunctionDecl *getAsyncAlternative() const;

/// If \p asyncAlternative is set, then compare its parameters to this
/// (presumed synchronous) function's parameters to find the index of the
Expand Down
5 changes: 2 additions & 3 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -3455,8 +3455,7 @@ class ConditionalRequirementsRequest

class RenamedDeclRequest
: public SimpleRequest<RenamedDeclRequest,
ValueDecl *(const ValueDecl *, const AvailableAttr *,
bool isKnownObjC),
ValueDecl *(const ValueDecl *, const AvailableAttr *),
RequestFlags::Cached> {
public:
using SimpleRequest::SimpleRequest;
Expand All @@ -3465,7 +3464,7 @@ class RenamedDeclRequest
friend SimpleRequest;

ValueDecl *evaluate(Evaluator &evaluator, const ValueDecl *attached,
const AvailableAttr *attr, bool isKnownObjC) const;
const AvailableAttr *attr) const;

public:
bool isCached() const { return true; }
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ SWIFT_REQUEST(TypeChecker, GetImplicitSendableRequest,
ProtocolConformance *(NominalTypeDecl *),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, RenamedDeclRequest,
ValueDecl *(const ValueDecl *, const AvailableAttr *, bool),
ValueDecl *(const ValueDecl *, const AvailableAttr *),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, ClosureEffectsRequest,
FunctionType::ExtInfo(ClosureExpr *),
Expand Down
6 changes: 2 additions & 4 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7666,8 +7666,7 @@ bool AbstractFunctionDecl::hasDynamicSelfResult() const {
return isa<ConstructorDecl>(this);
}

AbstractFunctionDecl *
AbstractFunctionDecl::getAsyncAlternative(bool isKnownObjC) const {
AbstractFunctionDecl *AbstractFunctionDecl::getAsyncAlternative() const {
// Async functions can't have async alternatives
if (hasAsync())
return nullptr;
Expand All @@ -7691,8 +7690,7 @@ AbstractFunctionDecl::getAsyncAlternative(bool isKnownObjC) const {
}

auto *renamedDecl = evaluateOrDefault(
getASTContext().evaluator, RenamedDeclRequest{this, avAttr, isKnownObjC},
nullptr);
getASTContext().evaluator, RenamedDeclRequest{this, avAttr}, nullptr);
auto *alternative = dyn_cast_or_null<AbstractFunctionDecl>(renamedDecl);
if (!alternative || !alternative->hasAsync())
return nullptr;
Expand Down
9 changes: 0 additions & 9 deletions lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1741,15 +1741,6 @@ shouldDiagnoseConflict(NominalTypeDecl *ty, AbstractFunctionDecl *newDecl,
}))
return false;

// If we're looking at protocol requirements, is the new method an async
// alternative of any existing method, or vice versa?
if (isa<ProtocolDecl>(ty) &&
llvm::any_of(vec, [&](AbstractFunctionDecl *oldDecl) {
return newDecl->getAsyncAlternative(/*isKnownObjC=*/true) == oldDecl
|| oldDecl->getAsyncAlternative(/*isKnownObjC=*/true) == newDecl;
}))
return false;

return true;
}

Expand Down
3 changes: 1 addition & 2 deletions lib/PrintAsClang/DeclAndTypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1547,8 +1547,7 @@ class DeclAndTypePrinter::Implementation
assert(!AvAttr->Rename.empty());

auto *renamedDecl = evaluateOrDefault(
getASTContext().evaluator, RenamedDeclRequest{D, AvAttr, false},
nullptr);
getASTContext().evaluator, RenamedDeclRequest{D, AvAttr}, nullptr);
if (renamedDecl) {
assert(shouldInclude(renamedDecl) &&
"ObjC printer logic mismatch with renamed decl");
Expand Down
5 changes: 2 additions & 3 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6657,8 +6657,7 @@ static bool parametersMatch(const AbstractFunctionDecl *a,

ValueDecl *RenamedDeclRequest::evaluate(Evaluator &evaluator,
const ValueDecl *attached,
const AvailableAttr *attr,
bool isKnownObjC) const {
const AvailableAttr *attr) const {
if (!attached || !attr)
return nullptr;

Expand Down Expand Up @@ -6689,7 +6688,7 @@ ValueDecl *RenamedDeclRequest::evaluate(Evaluator &evaluator,
auto minAccess = AccessLevel::Private;
if (attached->getModuleContext()->isExternallyConsumed())
minAccess = AccessLevel::Public;
bool attachedIsObjcVisible = isKnownObjC ||
bool attachedIsObjcVisible =
objc_translation::isVisibleToObjC(attached, minAccess);

SmallVector<ValueDecl *, 4> lookupResults;
Expand Down
18 changes: 17 additions & 1 deletion lib/Sema/TypeCheckDeclObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2448,20 +2448,36 @@ getObjCMethodConflictDecls(const SourceFile::ObjCMethodConflict &conflict) {
auto methods = conflict.typeDecl->lookupDirect(conflict.selector,
conflict.isInstanceMethod);

// Find async alternatives for each.
llvm::SmallDenseMap<AbstractFunctionDecl *, AbstractFunctionDecl *>
asyncAlternatives;
for (auto method : methods) {
if (isa<ProtocolDecl>(method->getDeclContext())) {
if (auto alt = method->getAsyncAlternative())
asyncAlternatives[method] = alt;
}
}

// Erase any invalid or stub declarations. We don't want to complain about
// them, because we might already have complained about redeclarations
// based on Swift matching.
llvm::erase_if(methods, [](AbstractFunctionDecl *afd) -> bool {
llvm::erase_if(methods,
[&asyncAlternatives](AbstractFunctionDecl *afd) -> bool {
if (afd->isInvalid())
return true;

// If there is an async alternative, remove this entry.
if (asyncAlternatives.count(afd))
return true;

if (auto ad = dyn_cast<AccessorDecl>(afd))
return ad->getStorage()->isInvalid();

if (auto *ctor = dyn_cast<ConstructorDecl>(afd)) {
if (ctor->hasStubImplementation())
return true;
}

return false;
});

Expand Down
11 changes: 11 additions & 0 deletions test/decl/objc_redeclaration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,16 @@ extension MyObject {
public override convenience init() {} // expected-error{{initializer 'init()' with Objective-C selector 'init' conflicts with implicit initializer 'init()' with the same Objective-C selector}}
}

// Ensure that we don't have cycles with the "renamed decl" request.
@available(SwiftStdlib 5.1, *)
@objc protocol MyProtocolWithAsync {
@available(*, renamed: "confirm(thing:)")
@objc(confirmThing:completion:)
optional func confirm(thing: AnyObject, completion: @escaping (AnyObject) -> Void)

@objc(confirmThing:completion:)
optional func confirm(thing: AnyObject) async -> AnyObject
}

// FIXME: Remove -verify-ignore-unknown.
// <unknown>:0: error: unexpected note produced: 'nsstringProperty2' previously declared here