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

On type mismatch involving fn/method call, point at definition #119340

Closed
wants to merge 3 commits into from
Closed
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
49 changes: 49 additions & 0 deletions compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
};
sjwang05 marked this conversation as resolved.
Show resolved Hide resolved
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not from expansions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly to avoid giving weird suggestions in desugaring like async fn, though if you think we should still give suggestions in macros, we could check for desugaring_kind().is_none() instead.

&& 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> {
Expand Down
5 changes: 3 additions & 2 deletions tests/incremental/circular-dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Foo;
pub fn consume_foo(_: Foo) {}
//[cfail2]~^ NOTE function defined here

pub fn produce_foo() -> Foo {
pub fn produce_foo() -> Foo { //[cfail2]~ NOTE the function produce_foo is defined here
Foo
}

Expand All @@ -28,10 +28,11 @@ fn test() {
//[cfail2]~| NOTE the crate `circular_dependencies` is compiled multiple times, possibly with different configurations
//[cfail2]~| NOTE function defined here

consume_foo(aux::produce_foo());
consume_foo(aux::produce_foo()); //
//[cfail2]~^ ERROR mismatched types [E0308]
//[cfail2]~| NOTE expected `Foo`, found `circular_dependencies::Foo`
//[cfail2]~| NOTE arguments to this function are incorrect
//[cfail2]~| NOTE `circular_dependencies::Foo` and `Foo` have similar names, but are actually distinct types
//[cfail2]~| NOTE the crate `circular_dependencies` is compiled multiple times, possibly with different configurations
//[cfail2]~| NOTE the function aux is defined here
}
12 changes: 11 additions & 1 deletion tests/ui/alloc-error/alloc-error-handler-bad-signature-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,24 @@ LL | #[alloc_error_handler]
LL | // fn oom(
LL | || info: &Layout,
LL | || ) -> ()
| ||_______^ expected `!`, found `()`
| || -^
| ||______||
| | |expected `!`, found `()`
| | help: consider changing oom's return type: `!`
LL | | {
LL | | loop {}
LL | | }
| |__- expected `!` because of return type
|
= note: expected type `!`
found unit type `()`
note: the function oom is defined here
--> $DIR/alloc-error-handler-bad-signature-1.rs:10:1
|
LL | / fn oom(
LL | | info: &Layout,
LL | | ) -> ()
| |_______^
= note: this error originates in the attribute macro `alloc_error_handler` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors
Expand Down
11 changes: 10 additions & 1 deletion tests/ui/alloc-error/alloc-error-handler-bad-signature-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,22 @@ LL | #[alloc_error_handler]
LL | // fn oom(
LL | || info: Layout,
LL | || ) {
| ||_^ expected `!`, found `()`
| || ^- help: consider changing oom's return type: `-> !`
| ||_|
| | expected `!`, found `()`
LL | | loop {}
LL | | }
| |__- expected `!` because of return type
|
= note: expected type `!`
found unit type `()`
note: the function oom is defined here
--> $DIR/alloc-error-handler-bad-signature-2.rs:10:1
|
LL | / fn oom(
LL | | info: Layout,
LL | | ) {
| |_^
= note: this error originates in the attribute macro `alloc_error_handler` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ LL | let _low = self.lows.remove(low.identify()).unwrap();
found associated type `<impl LowT as Identify>::Id`
= help: consider constraining the associated type `<impl LowT as Identify>::Id` to `&I`
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
note: the method identify is defined here
--> $DIR/do-not-look-at-parent-item-in-suggestion-for-type-param-of-current-assoc-item.rs:8:5
|
LL | fn identify(&self) -> Self::Id;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: method defined here
--> $SRC_DIR/std/src/collections/hash/map.rs:LL:COL

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/associated-type-bounds/elision.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Expand Down
5 changes: 5 additions & 0 deletions tests/ui/associated-types/associated-types-eq-3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ LL | let _: Bar = x.boo();
|
= note: expected struct `Bar`
found associated type `<I as Foo>::A`
note: the method boo is defined here
--> $DIR/associated-types-eq-3.rs:6:5
|
LL | fn boo(&self) -> <Self as Foo>::A;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider constraining the associated type `<I as Foo>::A` to `Bar`
|
LL | fn foo2<I: Foo<A = Bar>>(x: I) {
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/associated-types/associated-types-path-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,19 @@ LL | let _: i32 = f2(2i32);
| |
| expected due to this
|
note: the function f2 is defined here
--> $DIR/associated-types-path-2.rs:14:1
|
LL | pub fn f2<T: Foo>(a: T) -> T::A {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: you can convert a `u32` to an `i32` and panic if the converted value doesn't fit
|
LL | let _: i32 = f2(2i32).try_into().unwrap();
| ++++++++++++++++++++
help: consider changing f2's return type
|
LL | pub fn f2<T: Foo>(a: T) -> i32 {
| ~~~

error: aborting due to 8 previous errors

Expand Down
5 changes: 5 additions & 0 deletions tests/ui/async-await/coroutine-desc.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ LL | fun(one(), two());
|
= help: consider `await`ing on both `Future`s
= note: distinct uses of `impl Trait` result in different opaque types
note: the function two is defined here
--> $DIR/coroutine-desc.rs:6:1
|
LL | async fn two() {}
| ^^^^^^^^^^^^^^
note: function defined here
--> $DIR/coroutine-desc.rs:8:4
|
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/async-await/issues/issue-102206.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ LL | std::mem::size_of_val(foo());
| |
| arguments to this function are incorrect
|
note: the function foo is defined here
--> $DIR/issue-102206.rs:3:1
|
LL | async fn foo() {}
| ^^^^^^^^^^^^^^
note: function defined here
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
help: consider borrowing here
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/async-await/suggest-missing-await.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ note: calling an async function returns a future
|
LL | dummy()
| ^^^^^^^
note: the function dummy is defined here
--> $DIR/suggest-missing-await.rs:18:1
|
LL | async fn dummy() {}
| ^^^^^^^^^^^^^^^^
help: consider `await`ing on the `Future`
|
LL | dummy().await
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/block-result/unexpected-return-on-unit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ error[E0308]: mismatched types
LL | foo()
| ^^^^^ expected `()`, found `usize`
|
note: the function foo is defined here
--> $DIR/unexpected-return-on-unit.rs:4:1
|
LL | fn foo() -> usize {
| ^^^^^^^^^^^^^^^^^
help: consider using a semicolon here
|
LL | foo();
Expand All @@ -12,6 +17,10 @@ help: try adding a return type
|
LL | fn bar() -> usize {
| ++++++++
help: consider changing foo's return type
|
LL | fn foo() -> () {
| ~~

error: aborting due to 1 previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/closures/add_semicolon_non_block_closure.rs
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
}

Expand Down
9 changes: 9 additions & 0 deletions tests/ui/closures/add_semicolon_non_block_closure.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ LL | fn main() {
LL | foo(|| bar())
| ^^^^^ expected `()`, found `i32`
|
note: the function bar is defined here
--> $DIR/add_semicolon_non_block_closure.rs:3:1
|
LL | fn bar() -> i32 {
| ^^^^^^^^^^^^^^^
help: consider using a semicolon here
|
LL | foo(|| { bar(); })
| + +++
help: consider changing bar's return type
|
LL | fn bar() -> () {
| ~~

error: aborting due to 1 previous error

Expand Down
9 changes: 9 additions & 0 deletions tests/ui/coercion/retslot-cast.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@ LL | -> Option<&Iterator<Item=()>> {
...
LL | inner(x)
| ^^^^^^^^ expected trait `Iterator<Item = ()>`, found trait `Iterator<Item = ()> + Send`
...
LL | -> Option<&(Iterator<Item=()>+Send)> {
| --------------------------------- help: consider changing inner's return type: `Option<&dyn Iterator<Item = ()>>`
|
= note: expected enum `Option<&dyn Iterator<Item = ()>>`
found enum `Option<&dyn Iterator<Item = ()> + Send>`
note: the function inner is defined here
--> $DIR/retslot-cast.rs:16:1
|
LL | / pub fn inner(x: Option<&(Iterator<Item=()>+Send)>)
LL | | -> Option<&(Iterator<Item=()>+Send)> {
| |_________________________________________________^

error: aborting due to 1 previous error

Expand Down
9 changes: 9 additions & 0 deletions tests/ui/const-generics/occurs-check/unify-n-nplusone.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
error[E0308]: mismatched types
--> $DIR/unify-n-nplusone.rs:14:11
|
LL | fn bind<const N: usize>(value: [u8; N]) -> [u8; N + 1] {
| ----------- help: consider changing bind's return type: `[u8; _]`
...
LL | arr = bind(arr);
| ^^^^^^^^^ encountered a self-referencing constant
|
note: the function bind is defined here
--> $DIR/unify-n-nplusone.rs:6:1
|
LL | fn bind<const N: usize>(value: [u8; N]) -> [u8; N + 1] {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

Expand Down
9 changes: 9 additions & 0 deletions tests/ui/const-generics/occurs-check/unused-substs-4.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
error[E0308]: mismatched types
--> $DIR/unused-substs-4.rs:10:11
|
LL | fn bind<const N: usize>(value: [u8; N]) -> [u8; 3 + 4] {
| ----------- help: consider changing bind's return type: `[u8; _]`
...
LL | arr = bind(arr);
| ^^^^^^^^^ encountered a self-referencing constant
|
note: the function bind is defined here
--> $DIR/unused-substs-4.rs:4:1
|
LL | fn bind<const N: usize>(value: [u8; N]) -> [u8; 3 + 4] {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/const-generics/occurs-check/unused-substs-5.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ LL | x = q::<_, N>(x);
| ^^^^^^^^^^^^- help: try using a conversion method: `.to_vec()`
| |
| cyclic type of infinite size
|
note: the function q is defined here
--> $DIR/unused-substs-5.rs:5:1
|
LL | fn q<T, const N: usize>(_: T) -> [u8; N + 1] {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

Expand Down
1 change: 1 addition & 0 deletions tests/ui/did_you_mean/compatible-variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct Foo {
}

fn f() {}
//~^ HELP consider changing f's return type

fn a() -> Option<()> {
while false {
Expand Down
Loading
Loading