-
Notifications
You must be signed in to change notification settings - Fork 13.5k
On type mismatch involving fn/method call, point at definition #119340
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,6 +94,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
self.suggest_method_call_on_range_literal(err, expr, expr_ty, expected); | ||
self.suggest_return_binding_for_missing_tail_expr(err, expr, expr_ty, expected); | ||
self.note_wrong_return_ty_due_to_generic_arg(err, expr, expr_ty); | ||
self.note_fn_method_def_due_to_call(err, expr, expected); | ||
} | ||
|
||
/// Really hacky heuristic to remap an `assert_eq!` error to the user | ||
|
@@ -1170,6 +1171,56 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
_ => return, | ||
} | ||
} | ||
|
||
fn note_fn_method_def_due_to_call( | ||
&self, | ||
err: &mut Diagnostic, | ||
expr: &hir::Expr<'_>, | ||
expected: Ty<'_>, | ||
) { | ||
let (def_id, ident, callee_str) = if let hir::ExprKind::Call(fun, _) = expr.kind | ||
&& let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = fun.kind | ||
&& let hir::def::Res::Def(def_kind, def_id) = path.res | ||
&& !matches!(def_kind, hir::def::DefKind::Ctor(..)) | ||
{ | ||
(def_id, path.segments[0].ident, "function") | ||
} else if let hir::ExprKind::MethodCall(method, ..) = expr.kind | ||
&& let Some(def_id) = self.typeck_results.borrow().type_dependent_def_id(expr.hir_id) | ||
&& !matches!(self.tcx.def_kind(def_id), hir::def::DefKind::Ctor(..)) | ||
{ | ||
(def_id, method.ident, "method") | ||
} else { | ||
return; | ||
}; | ||
sjwang05 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
err.span_note( | ||
self.tcx.def_span(def_id), | ||
format!("the {callee_str} {ident} is defined here"), | ||
); | ||
|
||
if let Some(local_did) = def_id.as_local() | ||
&& let Some(node) = self.tcx.opt_hir_node(self.tcx.local_def_id_to_hir_id(local_did)) | ||
&& let hir::Node::TraitItem(hir::TraitItem { | ||
kind: hir::TraitItemKind::Fn(sig, ..), | ||
.. | ||
sjwang05 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
| hir::Node::ImplItem(hir::ImplItem { | ||
kind: hir::ImplItemKind::Fn(sig, ..), .. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do we check we aren't suggesting a change in a trait impl? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't match on |
||
}) | ||
| hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, ..), .. }) = node | ||
&& let ret_span = sig.decl.output.span() | ||
&& !ret_span.from_expansion() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not from expansions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mainly to avoid giving weird suggestions in desugaring like |
||
&& expected.has_concrete_skeleton() | ||
sjwang05 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
let sugg = | ||
if ret_span.is_empty() { format!("-> {expected}") } else { format!("{expected}") }; | ||
sjwang05 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
err.span_suggestion( | ||
ret_span, | ||
format!("consider changing {ident}'s return type"), | ||
sugg, | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
} | ||
} | ||
|
||
pub enum TypeMismatchSource<'tcx> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ LL | fn f(x: &mut dyn Iterator<Item: Iterator<Item = &'_ ()>>) -> Option<&'_ ()> | |
| | ||
= note: expected enum `Option<&()>` | ||
found enum `Option<impl Iterator<Item = &'_ ()>>` | ||
note: the method next is defined here | ||
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what's up with cases like this one--FWIW the span gets printed correctly when I run locally. |
||
|
||
error: aborting due to 2 previous errors | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
fn foo(_f: impl Fn()) {} | ||
|
||
fn bar() -> i32 { | ||
fn bar() -> i32 { //~ HELP consider changing bar's return type | ||
1 | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.