Skip to content
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

"call expression requires function" error points to wrong definition #80853

Closed
camelid opened this issue Jan 9, 2021 · 6 comments · Fixed by #82220
Closed

"call expression requires function" error points to wrong definition #80853

camelid opened this issue Jan 9, 2021 · 6 comments · Fixed by #82220
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@camelid
Copy link
Member

camelid commented Jan 9, 2021

struct S;

fn repro_ref(thing: S) {
    thing();
}

(Playground)

It should either point to the definition of S or it should say "thing defined here":

   Compiling playground v0.0.1 (/playground)
error[E0618]: expected function, found `S`
 --> src/lib.rs:4:5
  |
3 | fn repro_ref(thing: S) {
  |              ----- `S` defined here
4 |     thing();
  |     ^^^^^--
  |     |
  |     call expression requires function

error: aborting due to previous error

For more information about this error, try `rustc --explain E0618`.
error: could not compile `playground`

To learn more, run the command again with --verbose.
@camelid camelid added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. labels Jan 9, 2021
@camelid
Copy link
Member Author

camelid commented Jan 9, 2021

Relevant code:

let mut inner_callee_path = None;
let def = match callee.kind {
hir::ExprKind::Path(ref qpath) => {
self.typeck_results.borrow().qpath_res(qpath, callee.hir_id)
}
hir::ExprKind::Call(ref inner_callee, _) => {
// If the call spans more than one line and the callee kind is
// itself another `ExprCall`, that's a clue that we might just be
// missing a semicolon (Issue #51055)
let call_is_multiline =
self.tcx.sess.source_map().is_multiline(call_expr.span);
if call_is_multiline {
err.span_suggestion(
callee.span.shrink_to_hi(),
"try adding a semicolon",
";".to_owned(),
Applicability::MaybeIncorrect,
);
}
if let hir::ExprKind::Path(ref inner_qpath) = inner_callee.kind {
inner_callee_path = Some(inner_qpath);
self.typeck_results
.borrow()
.qpath_res(inner_qpath, inner_callee.hir_id)
} else {
Res::Err
}
}
_ => Res::Err,
};
err.span_label(call_expr.span, "call expression requires function");
if let Some(span) = self.tcx.hir().res_span(def) {
let callee_ty = callee_ty.to_string();
let label = match (unit_variant, inner_callee_path) {
(Some(path), _) => Some(format!("`{}` defined here", path)),
(_, Some(hir::QPath::Resolved(_, path))) => {
self.tcx.sess.source_map().span_to_snippet(path.span).ok().map(
|p| format!("`{}` defined here returns `{}`", p, callee_ty),
)
}
_ => Some(format!("`{}` defined here", callee_ty)),
};
if let Some(label) = label {
err.span_label(span, label);
}
}
err.emit();

@henryboisdequin
Copy link
Contributor

Would it be helpful if the compiler said expected function, found struct S and then emitted a note which said S is not a function in this case?

@henryboisdequin
Copy link
Contributor

@rustbot claim

@camelid
Copy link
Member Author

camelid commented Feb 11, 2021

Perhaps, but the bug this issue is about is this:

3 | fn repro_ref(thing: S) {
  |              ----- `S` defined here

It says "S defined here", but actually it's thing that's defined there. Instead it should point to the definition of S.

@henryboisdequin
Copy link
Contributor

@camelid I've been working on this for a bit and don't really know how to approach it. Since the code is calling thing as a function, an error happens. The definition of thing is as an argument to repro_ref. Would I need to detect whether it's an argument and then, if so, finding the root definition, or is there a better way to do this?

@camelid
Copy link
Member Author

camelid commented Feb 16, 2021

@henryboisdequin

Would I need to detect whether it's an argument and then, if so, finding the root definition, or is there a better way to do this?

I thought about this a bit more and realized that my original suggestion of pointing to the definition of the struct S is probably not that helpful and would not generalize. Probably a good approach is to:

I am not super familiar with the type-checker, but I can try to help you with what knowledge I have. You can also ask on Zulip if you need more help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants