-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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 all 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,54 @@ 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) = if let hir::ExprKind::Call(fun, _) = expr.kind | ||
&& let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = fun.kind | ||
&& let hir::def::Res::Def(_, def_id) = path.res | ||
{ | ||
(def_id, path.segments[0].ident) | ||
} else if let hir::ExprKind::MethodCall(method, ..) = expr.kind | ||
&& let Some(def_id) = self.typeck_results.borrow().type_dependent_def_id(expr.hir_id) | ||
{ | ||
(def_id, method.ident) | ||
} else { | ||
return; | ||
}; | ||
if !matches!(self.tcx.def_kind(def_id), hir::def::DefKind::AssocFn | hir::def::DefKind::Fn) | ||
{ | ||
return; | ||
} | ||
err.span_note( | ||
self.tcx.def_span(def_id), | ||
format!("the {} {ident} is defined here", self.tcx.def_descr(def_id)), | ||
); | ||
|
||
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)) | ||
&& !matches!(node, hir::Node::TraitItem(..)) | ||
&& let Some(sig) = node.fn_sig() | ||
&& 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 = match sig.decl.output { | ||
hir::FnRetTy::DefaultReturn(..) => format!("-> {expected}"), | ||
hir::FnRetTy::Return(..) => format!("{expected}"), | ||
}; | ||
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.