Skip to content

Commit d375440

Browse files
label where constructor is defined and note that it should be called
1 parent ede5c31 commit d375440

File tree

6 files changed

+39
-29
lines changed

6 files changed

+39
-29
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::method::probe::{IsSuggestion, Mode, ProbeScope};
55
use rustc_ast::util::parser::{ExprPrecedence, PREC_POSTFIX};
66
use rustc_errors::{Applicability, Diagnostic, MultiSpan};
77
use rustc_hir as hir;
8-
use rustc_hir::def::{CtorOf, DefKind};
8+
use rustc_hir::def::{CtorKind, CtorOf, DefKind};
99
use rustc_hir::lang_items::LangItem;
1010
use rustc_hir::{
1111
Expr, ExprKind, GenericBound, Node, Path, QPath, Stmt, StmtKind, TyKind, WherePredicate,
@@ -417,10 +417,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
417417
} else if self.suggest_else_fn_with_closure(err, expr, found, expected) {
418418
return true;
419419
} else if self.suggest_fn_call(err, expr, found, |output| self.can_coerce(output, expected))
420-
&& let ty::FnDef(def_id, ..) = &found.kind()
421-
&& let Some(sp) = self.tcx.hir().span_if_local(*def_id)
420+
&& let ty::FnDef(def_id, ..) = *found.kind()
421+
&& let Some(sp) = self.tcx.hir().span_if_local(def_id)
422422
{
423-
err.span_label(sp, format!("{found} defined here"));
423+
let name = self.tcx.item_name(def_id);
424+
let kind = self.tcx.def_kind(def_id);
425+
if let DefKind::Ctor(of, CtorKind::Fn) = kind {
426+
err.span_label(sp, format!("`{name}` defines {} constructor here, which should be called", match of {
427+
CtorOf::Struct => "a struct",
428+
CtorOf::Variant => "an enum variant",
429+
}));
430+
} else {
431+
let descr = kind.descr(def_id);
432+
err.span_label(sp, format!("{descr} `{name}` defined here"));
433+
}
424434
return true;
425435
} else if self.check_for_cast(err, expr, found, expected, expected_ty_expr) {
426436
return true;

tests/ui/associated-types/substs-ppaux.normal.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/substs-ppaux.rs:16:17
33
|
44
LL | fn bar<'a, T>() where T: 'a {}
5-
| --------------------------- fn() {<i8 as Foo<'static, 'static, u8>>::bar::<'static, char>} defined here
5+
| --------------------------- associated function `bar` defined here
66
...
77
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>;
88
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
@@ -20,7 +20,7 @@ error[E0308]: mismatched types
2020
--> $DIR/substs-ppaux.rs:25:17
2121
|
2222
LL | fn bar<'a, T>() where T: 'a {}
23-
| --------------------------- fn() {<i8 as Foo<'static, 'static>>::bar::<'static, char>} defined here
23+
| --------------------------- associated function `bar` defined here
2424
...
2525
LL | let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>;
2626
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
@@ -38,7 +38,7 @@ error[E0308]: mismatched types
3838
--> $DIR/substs-ppaux.rs:33:17
3939
|
4040
LL | fn baz() {}
41-
| -------- fn() {<i8 as Foo<'static, 'static, u8>>::baz} defined here
41+
| -------- associated function `baz` defined here
4242
...
4343
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::baz;
4444
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
@@ -56,7 +56,7 @@ error[E0308]: mismatched types
5656
--> $DIR/substs-ppaux.rs:41:17
5757
|
5858
LL | fn foo<'z>() where &'z (): Sized {
59-
| -------------------------------- fn() {foo::<'static>} defined here
59+
| -------------------------------- function `foo` defined here
6060
...
6161
LL | let x: () = foo::<'static>;
6262
| -- ^^^^^^^^^^^^^^ expected `()`, found fn item

tests/ui/associated-types/substs-ppaux.verbose.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/substs-ppaux.rs:16:17
33
|
44
LL | fn bar<'a, T>() where T: 'a {}
5-
| --------------------------- fn() {<i8 as Foo<ReStatic, ReStatic, u8>>::bar::<ReStatic, char>} defined here
5+
| --------------------------- associated function `bar` defined here
66
...
77
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>;
88
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
@@ -20,7 +20,7 @@ error[E0308]: mismatched types
2020
--> $DIR/substs-ppaux.rs:25:17
2121
|
2222
LL | fn bar<'a, T>() where T: 'a {}
23-
| --------------------------- fn() {<i8 as Foo<ReStatic, ReStatic>>::bar::<ReStatic, char>} defined here
23+
| --------------------------- associated function `bar` defined here
2424
...
2525
LL | let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>;
2626
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
@@ -38,7 +38,7 @@ error[E0308]: mismatched types
3838
--> $DIR/substs-ppaux.rs:33:17
3939
|
4040
LL | fn baz() {}
41-
| -------- fn() {<i8 as Foo<ReStatic, ReStatic, u8>>::baz} defined here
41+
| -------- associated function `baz` defined here
4242
...
4343
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::baz;
4444
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
@@ -56,7 +56,7 @@ error[E0308]: mismatched types
5656
--> $DIR/substs-ppaux.rs:41:17
5757
|
5858
LL | fn foo<'z>() where &'z (): Sized {
59-
| -------------------------------- fn() {foo::<ReStatic>} defined here
59+
| -------------------------------- function `foo` defined here
6060
...
6161
LL | let x: () = foo::<'static>;
6262
| -- ^^^^^^^^^^^^^^ expected `()`, found fn item

tests/ui/issues/issue-35241.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/issue-35241.rs:3:20
33
|
44
LL | struct Foo(u32);
5-
| ---------- fn(u32) -> Foo {Foo} defined here
5+
| ---------- `Foo` defines a struct constructor here, which should be called
66
LL |
77
LL | fn test() -> Foo { Foo }
88
| --- ^^^ expected struct `Foo`, found struct constructor

tests/ui/resolve/privacy-enum-ctor.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ error[E0308]: mismatched types
264264
--> $DIR/privacy-enum-ctor.rs:27:20
265265
|
266266
LL | Fn(u8),
267-
| -- fn(u8) -> Z {Z::Fn} defined here
267+
| -- `Fn` defines an enum variant constructor here, which should be called
268268
...
269269
LL | let _: Z = Z::Fn;
270270
| - ^^^^^ expected enum `Z`, found enum constructor
@@ -305,7 +305,7 @@ error[E0308]: mismatched types
305305
--> $DIR/privacy-enum-ctor.rs:43:16
306306
|
307307
LL | Fn(u8),
308-
| -- fn(u8) -> E {E::Fn} defined here
308+
| -- `Fn` defines an enum variant constructor here, which should be called
309309
...
310310
LL | let _: E = m::E::Fn;
311311
| - ^^^^^^^^ expected enum `E`, found enum constructor
@@ -346,7 +346,7 @@ error[E0308]: mismatched types
346346
--> $DIR/privacy-enum-ctor.rs:51:16
347347
|
348348
LL | Fn(u8),
349-
| -- fn(u8) -> E {E::Fn} defined here
349+
| -- `Fn` defines an enum variant constructor here, which should be called
350350
...
351351
LL | let _: E = E::Fn;
352352
| - ^^^^^ expected enum `E`, found enum constructor

tests/ui/suggestions/fn-or-tuple-struct-without-args.stderr

+13-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/fn-or-tuple-struct-without-args.rs:29:20
33
|
44
LL | fn foo(a: usize, b: usize) -> usize { a }
5-
| ----------------------------------- fn(usize, usize) -> usize {foo} defined here
5+
| ----------------------------------- function `foo` defined here
66
...
77
LL | let _: usize = foo;
88
| ----- ^^^ expected `usize`, found fn item
@@ -20,7 +20,7 @@ error[E0308]: mismatched types
2020
--> $DIR/fn-or-tuple-struct-without-args.rs:30:16
2121
|
2222
LL | struct S(usize, usize);
23-
| -------- fn(usize, usize) -> S {S} defined here
23+
| -------- `S` defines a struct constructor here, which should be called
2424
...
2525
LL | let _: S = S;
2626
| - ^ expected struct `S`, found struct constructor
@@ -38,7 +38,7 @@ error[E0308]: mismatched types
3838
--> $DIR/fn-or-tuple-struct-without-args.rs:31:20
3939
|
4040
LL | fn bar() -> usize { 42 }
41-
| ----------------- fn() -> usize {bar} defined here
41+
| ----------------- function `bar` defined here
4242
...
4343
LL | let _: usize = bar;
4444
| ----- ^^^ expected `usize`, found fn item
@@ -56,7 +56,7 @@ error[E0308]: mismatched types
5656
--> $DIR/fn-or-tuple-struct-without-args.rs:32:16
5757
|
5858
LL | struct V();
59-
| -------- fn() -> V {V} defined here
59+
| -------- `V` defines a struct constructor here, which should be called
6060
...
6161
LL | let _: V = V;
6262
| - ^ expected struct `V`, found struct constructor
@@ -74,7 +74,7 @@ error[E0308]: mismatched types
7474
--> $DIR/fn-or-tuple-struct-without-args.rs:33:20
7575
|
7676
LL | fn baz(x: usize, y: usize) -> usize { x }
77-
| ----------------------------------- fn(usize, usize) -> usize {<_ as T>::baz} defined here
77+
| ----------------------------------- associated function `baz` defined here
7878
...
7979
LL | let _: usize = T::baz;
8080
| ----- ^^^^^^ expected `usize`, found fn item
@@ -92,7 +92,7 @@ error[E0308]: mismatched types
9292
--> $DIR/fn-or-tuple-struct-without-args.rs:34:20
9393
|
9494
LL | fn bat(x: usize) -> usize { 42 }
95-
| ------------------------- fn(usize) -> usize {<_ as T>::bat} defined here
95+
| ------------------------- associated function `bat` defined here
9696
...
9797
LL | let _: usize = T::bat;
9898
| ----- ^^^^^^ expected `usize`, found fn item
@@ -110,7 +110,7 @@ error[E0308]: mismatched types
110110
--> $DIR/fn-or-tuple-struct-without-args.rs:35:16
111111
|
112112
LL | A(usize),
113-
| - fn(usize) -> E {E::A} defined here
113+
| - `A` defines an enum variant constructor here, which should be called
114114
...
115115
LL | let _: E = E::A;
116116
| - ^^^^ expected enum `E`, found enum constructor
@@ -134,7 +134,7 @@ error[E0308]: mismatched types
134134
--> $DIR/fn-or-tuple-struct-without-args.rs:37:20
135135
|
136136
LL | fn baz(x: usize, y: usize) -> usize { x }
137-
| ----------------------------------- fn(usize, usize) -> usize {<X as T>::baz} defined here
137+
| ----------------------------------- associated function `baz` defined here
138138
...
139139
LL | let _: usize = X::baz;
140140
| ----- ^^^^^^ expected `usize`, found fn item
@@ -152,7 +152,7 @@ error[E0308]: mismatched types
152152
--> $DIR/fn-or-tuple-struct-without-args.rs:38:20
153153
|
154154
LL | fn bat(x: usize) -> usize { 42 }
155-
| ------------------------- fn(usize) -> usize {<X as T>::bat} defined here
155+
| ------------------------- associated function `bat` defined here
156156
...
157157
LL | let _: usize = X::bat;
158158
| ----- ^^^^^^ expected `usize`, found fn item
@@ -170,7 +170,7 @@ error[E0308]: mismatched types
170170
--> $DIR/fn-or-tuple-struct-without-args.rs:39:20
171171
|
172172
LL | fn bax(x: usize) -> usize { 42 }
173-
| ------------------------- fn(usize) -> usize {<X as T>::bax} defined here
173+
| ------------------------- associated function `bax` defined here
174174
...
175175
LL | let _: usize = X::bax;
176176
| ----- ^^^^^^ expected `usize`, found fn item
@@ -188,7 +188,7 @@ error[E0308]: mismatched types
188188
--> $DIR/fn-or-tuple-struct-without-args.rs:40:20
189189
|
190190
LL | fn bach(x: usize) -> usize;
191-
| --------------------------- fn(usize) -> usize {<X as T>::bach} defined here
191+
| --------------------------- associated function `bach` defined here
192192
...
193193
LL | let _: usize = X::bach;
194194
| ----- ^^^^^^^ expected `usize`, found fn item
@@ -206,7 +206,7 @@ error[E0308]: mismatched types
206206
--> $DIR/fn-or-tuple-struct-without-args.rs:41:20
207207
|
208208
LL | fn ban(&self) -> usize { 42 }
209-
| ---------------------- for<'a> fn(&'a X) -> usize {<X as T>::ban} defined here
209+
| ---------------------- associated function `ban` defined here
210210
...
211211
LL | let _: usize = X::ban;
212212
| ----- ^^^^^^ expected `usize`, found fn item
@@ -224,7 +224,7 @@ error[E0308]: mismatched types
224224
--> $DIR/fn-or-tuple-struct-without-args.rs:42:20
225225
|
226226
LL | fn bal(&self) -> usize;
227-
| ----------------------- for<'a> fn(&'a X) -> usize {<X as T>::bal} defined here
227+
| ----------------------- associated function `bal` defined here
228228
...
229229
LL | let _: usize = X::bal;
230230
| ----- ^^^^^^ expected `usize`, found fn item

0 commit comments

Comments
 (0)