Skip to content

[5.9] Handle async vs. completion-handler mismatches in @_objcImplementation #65260

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
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
73 changes: 69 additions & 4 deletions lib/Sema/TypeCheckDeclObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2882,6 +2882,15 @@ class ObjCImplementationChecker {
/// Candidates with their explicit ObjC names, if any.
llvm::SmallDenseMap<ValueDecl *, ObjCSelector, 16> unmatchedCandidates;

/// Key that can be used to uniquely identify a particular Objective-C
/// method.
using ObjCMethodKey = std::pair<ObjCSelector, char>;

/// Mapping from Objective-C methods to the set of requirements within this
/// protocol that have the same selector and instance/class designation.
llvm::SmallDenseMap<ObjCMethodKey, TinyPtrVector<AbstractFunctionDecl *>, 4>
objcMethodRequirements;

public:
ObjCImplementationChecker(ExtensionDecl *ext)
: diags(ext->getASTContext().Diags)
Expand All @@ -2903,6 +2912,10 @@ class ObjCImplementationChecker {
}

private:
auto getObjCMethodKey(AbstractFunctionDecl *func) const -> ObjCMethodKey {
return ObjCMethodKey(func->getObjCSelector(), func->isInstanceMember());
}

void addRequirements(IterableDeclContext *idc) {
assert(idc->getDecl()->hasClangNode());
for (Decl *_member : idc->getMembers()) {
Expand All @@ -2918,6 +2931,10 @@ class ObjCImplementationChecker {

auto inserted = unmatchedRequirements.insert(member);
assert(inserted && "objc interface member added twice?");

if (auto func = dyn_cast<AbstractFunctionDecl>(member)) {
objcMethodRequirements[getObjCMethodKey(func)].push_back(func);
}
}
}

Expand Down Expand Up @@ -3011,6 +3028,19 @@ class ObjCImplementationChecker {
}
};

/// Determine whether the set of matched requirements are ambiguous for the
/// given candidate.
bool areRequirementsAmbiguous(const BestMatchList &reqs, ValueDecl *cand) {
if (reqs.matches.size() != 2)
return reqs.matches.size() > 2;

bool firstIsAsyncAlternative =
matchesAsyncAlternative(reqs.matches[0], cand);
bool secondIsAsyncAlternative =
matchesAsyncAlternative(reqs.matches[1], cand);
return firstIsAsyncAlternative == secondIsAsyncAlternative;
}

void matchRequirementsAtThreshold(MatchOutcome threshold) {
SmallString<32> scratch;

Expand Down Expand Up @@ -3070,11 +3100,14 @@ class ObjCImplementationChecker {
// removing them.
requirementsToRemove.set_union(matchedRequirements.matches);

if (matchedRequirements.matches.size() == 1) {
if (!areRequirementsAmbiguous(matchedRequirements, cand)) {
// Note that this is BestMatchList::insert(), so it'll only keep the
// matches with the best outcomes.
matchesByRequirement[matchedRequirements.matches.front()]
.insert(cand, matchedRequirements.currentOutcome);
for (auto req : matchedRequirements.matches) {
matchesByRequirement[req]
.insert(cand, matchedRequirements.currentOutcome);
}

continue;
}

Expand Down Expand Up @@ -3156,6 +3189,35 @@ class ObjCImplementationChecker {
unmatchedCandidates.erase(cand);
}

/// Whether the candidate matches the async alternative of the given
/// requirement.
bool matchesAsyncAlternative(ValueDecl *req, ValueDecl *cand) const {
auto reqFunc = dyn_cast<AbstractFunctionDecl>(req);
if (!reqFunc)
return false;

auto candFunc = dyn_cast<AbstractFunctionDecl>(cand);
if (!candFunc)
return false;

if (reqFunc->hasAsync() == candFunc->hasAsync())
return false;

auto otherReqFuncs =
objcMethodRequirements.find(getObjCMethodKey(reqFunc));
if (otherReqFuncs == objcMethodRequirements.end())
return false;

for (auto otherReqFunc : otherReqFuncs->second) {
if (otherReqFunc->getName() == cand->getName() &&
otherReqFunc->hasAsync() == candFunc->hasAsync() &&
req->getObjCRuntimeName() == cand->getObjCRuntimeName())
return true;
}

return false;
}

MatchOutcome matches(ValueDecl *req, ValueDecl *cand,
ObjCSelector explicitObjCName) const {
bool hasObjCNameMatch =
Expand All @@ -3173,7 +3235,10 @@ class ObjCImplementationChecker {
&& req->getObjCRuntimeName() != explicitObjCName)
return MatchOutcome::WrongExplicitObjCName;

if (!hasSwiftNameMatch)
// If the ObjC selectors matched but the Swift names do not, and these are
// functions with mismatched 'async', check whether the "other" requirement
// (the completion-handler or async version)'s Swift name matches.
if (!hasSwiftNameMatch && !matchesAsyncAlternative(req, cand))
return MatchOutcome::WrongSwiftName;

if (!hasObjCNameMatch)
Expand Down
8 changes: 8 additions & 0 deletions test/decl/ext/Inputs/objc_implementation.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@import Foundation;

@interface ObjCBaseClass


Expand Down Expand Up @@ -83,6 +85,12 @@

@end

@interface ObjCClass (Effects)
- (void)doSomethingAsynchronousWithCompletionHandler:(void (^ _Nonnull)(id _Nullable result, NSError * _Nullable error))completionHandler;
- (void)doSomethingElseAsynchronousWithCompletionHandler:(void (^ _Nullable)(id _Nonnull result))completionHandler;
- (void)doSomethingFunAndAsynchronousWithCompletionHandler:(void (^ _Nonnull)(id _Nullable result, NSError * _Nullable error))completionHandler;
@end

@interface ObjCSubclass : ObjCClass

- (void)subclassMethodFromHeader1:(int)param;
Expand Down
15 changes: 15 additions & 0 deletions test/decl/ext/objc_implementation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,21 @@
}
}

@_objcImplementation(Effects) extension ObjCClass {
@available(SwiftStdlib 5.1, *)
public func doSomethingAsynchronous() async throws -> Any {
return self
}

@available(SwiftStdlib 5.1, *)
public func doSomethingElseAsynchronous() async -> Any {
return self
}

public func doSomethingFunAndAsynchronous(completionHandler: @escaping (Any?, Error?) -> Void) {
}
}

@_objcImplementation extension ObjCClass {}
// expected-error@-1 {{duplicate implementation of Objective-C class 'ObjCClass'}}

Expand Down