Skip to content

Commit 6bf4867

Browse files
authored
Rollup merge of #82220 - henryboisdequin:fixes-80853, r=varkor
fix the false 'defined here' messages Closes #80853. Take this code: ```rust struct S; fn repro_ref(thing: S) { thing(); } ``` Previously, the error message would be this: ``` 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 ``` This is incorrect as `S` is not defined in the function arguments, `thing` is defined there. With this change, the following is emitted: ``` error[E0618]: expected function, found `S` --> $DIR/80853.rs:4:5 | LL | fn repro_ref(thing: S) { | ----- is of type `S` LL | thing(); | ^^^^^-- | | | call expression requires function | = note: local variable `S` is not a function error: aborting due to previous error ``` As you can see, this error message points out that `thing` is of type `S` and later in a note, that `S` is not a function. This change does seem like a downside for some error messages. Take this example: ``` LL | struct Empty2; | -------------- is of type `Empty2` ``` As you can see, the error message shows that the definition of `Empty2` is of type `Empty2`. Although this isn't wrong, it would be more helpful if it would say something like this (which was there previously): ``` LL | struct Empty2; | -------------- `Empty2` defined here ``` If there is a better way of doing this, where the `Empty2` example would stay the same as without this change, please inform me. **Update: This is now fixed** CC `@camelid`
2 parents 00aa3e6 + d7cb66d commit 6bf4867

File tree

11 files changed

+67
-9
lines changed

11 files changed

+67
-9
lines changed

compiler/rustc_typeck/src/check/callee.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::type_error_struct;
44

55
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
66
use rustc_hir as hir;
7-
use rustc_hir::def::Res;
7+
use rustc_hir::def::{Namespace, Res};
88
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
99
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
1010
use rustc_infer::{infer, traits};
@@ -374,7 +374,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
374374
|p| format!("`{}` defined here returns `{}`", p, callee_ty),
375375
)
376376
}
377-
_ => Some(format!("`{}` defined here", callee_ty)),
377+
_ => {
378+
match def {
379+
// Emit a different diagnostic for local variables, as they are not
380+
// type definitions themselves, but rather variables *of* that type.
381+
Res::Local(hir_id) => Some(format!(
382+
"`{}` has type `{}`",
383+
self.tcx.hir().name(hir_id),
384+
callee_ty
385+
)),
386+
Res::Def(kind, def_id)
387+
if kind.ns() == Some(Namespace::ValueNS) =>
388+
{
389+
Some(format!(
390+
"`{}` defined here",
391+
self.tcx.def_path_str(def_id),
392+
))
393+
}
394+
_ => Some(format!("`{}` defined here", callee_ty)),
395+
}
396+
}
378397
};
379398
if let Some(label) = label {
380399
err.span_label(span, label);

src/test/ui/consts/const-as-fn.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const FOO: usize = 0;
2+
3+
fn main() {
4+
FOO(); //~ ERROR expected function, found `usize`
5+
}

src/test/ui/consts/const-as-fn.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0618]: expected function, found `usize`
2+
--> $DIR/const-as-fn.rs:4:5
3+
|
4+
LL | const FOO: usize = 0;
5+
| --------------------- `FOO` defined here
6+
...
7+
LL | FOO();
8+
| ^^^--
9+
| |
10+
| call expression requires function
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0618`.

src/test/ui/error-codes/E0618.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ error[E0618]: expected function, found `i32`
1818
--> $DIR/E0618.rs:9:5
1919
|
2020
LL | let x = 0i32;
21-
| - `i32` defined here
21+
| - `x` has type `i32`
2222
LL | x();
2323
| ^--
2424
| |

src/test/ui/issues/issue-10969.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0618]: expected function, found `i32`
22
--> $DIR/issue-10969.rs:2:5
33
|
44
LL | fn func(i: i32) {
5-
| - `i32` defined here
5+
| - `i` has type `i32`
66
LL | i();
77
| ^--
88
| |
@@ -12,7 +12,7 @@ error[E0618]: expected function, found `i32`
1212
--> $DIR/issue-10969.rs:6:5
1313
|
1414
LL | let i = 0i32;
15-
| - `i32` defined here
15+
| - `i` has type `i32`
1616
LL | i();
1717
| ^--
1818
| |

src/test/ui/issues/issue-21701.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0618]: expected function, found `U`
22
--> $DIR/issue-21701.rs:2:13
33
|
44
LL | fn foo<U>(t: U) {
5-
| - `U` defined here
5+
| - `t` has type `U`
66
LL | let y = t();
77
| ^--
88
| |

src/test/ui/issues/issue-22468.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0618]: expected function, found `&str`
22
--> $DIR/issue-22468.rs:3:13
33
|
44
LL | let foo = "bar";
5-
| --- `&str` defined here
5+
| --- `foo` has type `&str`
66
LL | let x = foo("baz");
77
| ^^^-------
88
| |

src/test/ui/issues/issue-26237.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | $not_a_function($some_argument)
55
| ------------------------------- call expression requires function
66
...
77
LL | let mut value_a = 0;
8-
| ----------- `{integer}` defined here
8+
| ----------- `value_a` has type `{integer}`
99
LL | let mut value_b = 0;
1010
LL | macro_panic!(value_a, value_b);
1111
| ^^^^^^^

src/test/ui/parser/parse-error-correct.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ error[E0618]: expected function, found `{integer}`
1414
--> $DIR/parse-error-correct.rs:7:13
1515
|
1616
LL | let y = 42;
17-
| - `{integer}` defined here
17+
| - `y` has type `{integer}`
1818
LL | let x = y.;
1919
LL | let x = y.();
2020
| ^---

src/test/ui/structs/80853.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
struct S;
2+
3+
fn repro_ref(thing: S) {
4+
thing(); //~ ERROR expected function, found `S`
5+
}
6+
7+
fn main() {}

src/test/ui/structs/80853.stderr

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0618]: expected function, found `S`
2+
--> $DIR/80853.rs:4:5
3+
|
4+
LL | fn repro_ref(thing: S) {
5+
| ----- `thing` has type `S`
6+
LL | thing();
7+
| ^^^^^--
8+
| |
9+
| call expression requires function
10+
11+
error: aborting due to previous error
12+
13+
For more information about this error, try `rustc --explain E0618`.

0 commit comments

Comments
 (0)