Skip to content

Break dependency cycle between @objc checking and and "renamed" availability #60982

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 1 commit into from
Sep 7, 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
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
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(/*isKnownObjC=*/true))
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