Skip to content

Commit

Permalink
[Sema] Improve diagnostics for #selector() expressions
Browse files Browse the repository at this point in the history
When the `#selector()` expression refers to a parameter or variable, we
don't want to call it a property.

Fixes the rest of SR-880.
  • Loading branch information
lilyball committed Mar 5, 2016
1 parent 502b159 commit 59df509
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
4 changes: 2 additions & 2 deletions include/swift/AST/DiagnosticsSema.def
Expand Up @@ -367,8 +367,8 @@ ERROR(expr_selector_module_missing,none,
"import the 'ObjectiveC' module to use '#selector'", ())
ERROR(expr_selector_no_declaration,none,
"argument of '#selector' does not refer to an initializer or method", ())
ERROR(expr_selector_property,none,
"argument of '#selector' cannot refer to a property", ())
ERROR(expr_selector_var,none,
"argument of '#selector' cannot refer to a %select{property|parameter|variable}0", (int))
ERROR(expr_selector_not_method_or_init,none,
"argument of '#selector' does not refer to a method or initializer", ())
ERROR(expr_selector_not_objc,none,
Expand Down
19 changes: 14 additions & 5 deletions lib/Sema/CSApply.cpp
Expand Up @@ -3342,11 +3342,20 @@ namespace {
// complain.
auto func = dyn_cast<AbstractFunctionDecl>(foundDecl);
if (!func) {
tc.diagnose(E->getLoc(),
isa<VarDecl>(foundDecl)
? diag::expr_selector_property
: diag::expr_selector_not_method_or_init)
.highlight(subExpr->getSourceRange());
Optional<InFlightDiagnostic> diag;
if (auto VD = dyn_cast<VarDecl>(foundDecl)) {
diag.emplace(tc.diagnose(E->getLoc(), diag::expr_selector_var,
isa<ParamDecl>(VD)
? 1
: VD->getDeclContext()->isTypeContext()
? 0
: 2));
} else {
diag.emplace(tc.diagnose(E->getLoc(),
diag::expr_selector_not_method_or_init));
}
diag->highlight(subExpr->getSourceRange());
diag.reset();
tc.diagnose(foundDecl, diag::decl_declared_here,
foundDecl->getFullName());
return E;
Expand Down
8 changes: 8 additions & 0 deletions test/expr/unary/selector/selector.swift
Expand Up @@ -26,6 +26,14 @@ class C1 {
_ = testUnqualifiedSelector // suppress unused warning
}

@objc func testParam(testParam: A) { // expected-note{{'testParam' declared here}}
_ = #selector(testParam) // expected-error{{argument of '#selector' cannot refer to a parameter}}
}

@objc func testVariable() {
let testVariable = 1 // expected-note{{'testVariable' declared here}}
_ = #selector(testVariable) // expected-error{{argument of '#selector' cannot refer to a variable}}
}
}

@objc protocol P1 {
Expand Down

0 comments on commit 59df509

Please sign in to comment.