Skip to content

[ConstraintSystem] Don't increase the score for unapplied function references if the expression is an argument to #selector. #39831

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 2 commits into from
Oct 21, 2021
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
3 changes: 2 additions & 1 deletion lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3464,7 +3464,8 @@ namespace {
// Note that the subexpression of a #selector expression is
// unevaluated.
if (auto sel = dyn_cast<ObjCSelectorExpr>(expr)) {
CG.getConstraintSystem().UnevaluatedRootExprs.insert(sel->getSubExpr());
auto *subExpr = sel->getSubExpr()->getSemanticsProvidingExpr();
CG.getConstraintSystem().UnevaluatedRootExprs.insert(subExpr);
}

// Check an objc key-path expression, which fills in its semantic
Expand Down
8 changes: 7 additions & 1 deletion lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2916,7 +2916,13 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
}

if (isa<AbstractFunctionDecl>(decl) || isa<TypeDecl>(decl)) {
if (choice.getFunctionRefKind() == FunctionRefKind::Unapplied) {
auto anchor = locator->getAnchor();
// TODO: Instead of not increasing the score for arguments to #selector,
// a better fix for this is to port over the #selector diagnostics from
// CSApply to constraint fixes, and not attempt invalid disjunction
// choices based on the selector kind on the valid code path.
if (choice.getFunctionRefKind() == FunctionRefKind::Unapplied &&
!UnevaluatedRootExprs.contains(getAsExpr(anchor))) {
increaseScore(SK_UnappliedFunction);
}
}
Expand Down
11 changes: 10 additions & 1 deletion test/expr/primary/selector/selector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,16 @@ extension SomeProtocol {
let _ = #selector(anotherFunction) // expected-error {{cannot use 'anotherFunction' as a selector because protocol 'SomeProtocol' is not exposed to Objective-C}} {{none}}
}

func anotherFunction() {
func anotherFunction() {
print("Hello world!")
}
}

@objc class OverloadedFuncAndProperty {
@objc static func f() {}
@objc var f: Int { 0 }
}

func test() -> Selector {
#selector(OverloadedFuncAndProperty.f)
}