label where constructor is defined and note that it should be called

This commit is contained in:
Michael Goulet 2023-01-06 03:33:57 +00:00
parent ede5c31af4
commit d375440dab
6 changed files with 39 additions and 29 deletions

View File

@ -5,7 +5,7 @@ use crate::method::probe::{IsSuggestion, Mode, ProbeScope};
use rustc_ast::util::parser::{ExprPrecedence, PREC_POSTFIX};
use rustc_errors::{Applicability, Diagnostic, MultiSpan};
use rustc_hir as hir;
use rustc_hir::def::{CtorOf, DefKind};
use rustc_hir::def::{CtorKind, CtorOf, DefKind};
use rustc_hir::lang_items::LangItem;
use rustc_hir::{
Expr, ExprKind, GenericBound, Node, Path, QPath, Stmt, StmtKind, TyKind, WherePredicate,
@ -417,10 +417,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else if self.suggest_else_fn_with_closure(err, expr, found, expected) {
return true;
} else if self.suggest_fn_call(err, expr, found, |output| self.can_coerce(output, expected))
&& let ty::FnDef(def_id, ..) = &found.kind()
&& let Some(sp) = self.tcx.hir().span_if_local(*def_id)
&& let ty::FnDef(def_id, ..) = *found.kind()
&& let Some(sp) = self.tcx.hir().span_if_local(def_id)
{
err.span_label(sp, format!("{found} defined here"));
let name = self.tcx.item_name(def_id);
let kind = self.tcx.def_kind(def_id);
if let DefKind::Ctor(of, CtorKind::Fn) = kind {
err.span_label(sp, format!("`{name}` defines {} constructor here, which should be called", match of {
CtorOf::Struct => "a struct",
CtorOf::Variant => "an enum variant",
}));
} else {
let descr = kind.descr(def_id);
err.span_label(sp, format!("{descr} `{name}` defined here"));
}
return true;
} else if self.check_for_cast(err, expr, found, expected, expected_ty_expr) {
return true;

View File

@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/substs-ppaux.rs:16:17
|
LL | fn bar<'a, T>() where T: 'a {}
| --------------------------- fn() {<i8 as Foo<'static, 'static, u8>>::bar::<'static, char>} defined here
| --------------------------- associated function `bar` defined here
...
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>;
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
@ -20,7 +20,7 @@ error[E0308]: mismatched types
--> $DIR/substs-ppaux.rs:25:17
|
LL | fn bar<'a, T>() where T: 'a {}
| --------------------------- fn() {<i8 as Foo<'static, 'static>>::bar::<'static, char>} defined here
| --------------------------- associated function `bar` defined here
...
LL | let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>;
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
@ -38,7 +38,7 @@ error[E0308]: mismatched types
--> $DIR/substs-ppaux.rs:33:17
|
LL | fn baz() {}
| -------- fn() {<i8 as Foo<'static, 'static, u8>>::baz} defined here
| -------- associated function `baz` defined here
...
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::baz;
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
@ -56,7 +56,7 @@ error[E0308]: mismatched types
--> $DIR/substs-ppaux.rs:41:17
|
LL | fn foo<'z>() where &'z (): Sized {
| -------------------------------- fn() {foo::<'static>} defined here
| -------------------------------- function `foo` defined here
...
LL | let x: () = foo::<'static>;
| -- ^^^^^^^^^^^^^^ expected `()`, found fn item

View File

@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/substs-ppaux.rs:16:17
|
LL | fn bar<'a, T>() where T: 'a {}
| --------------------------- fn() {<i8 as Foo<ReStatic, ReStatic, u8>>::bar::<ReStatic, char>} defined here
| --------------------------- associated function `bar` defined here
...
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>;
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
@ -20,7 +20,7 @@ error[E0308]: mismatched types
--> $DIR/substs-ppaux.rs:25:17
|
LL | fn bar<'a, T>() where T: 'a {}
| --------------------------- fn() {<i8 as Foo<ReStatic, ReStatic>>::bar::<ReStatic, char>} defined here
| --------------------------- associated function `bar` defined here
...
LL | let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>;
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
@ -38,7 +38,7 @@ error[E0308]: mismatched types
--> $DIR/substs-ppaux.rs:33:17
|
LL | fn baz() {}
| -------- fn() {<i8 as Foo<ReStatic, ReStatic, u8>>::baz} defined here
| -------- associated function `baz` defined here
...
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::baz;
| -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found fn item
@ -56,7 +56,7 @@ error[E0308]: mismatched types
--> $DIR/substs-ppaux.rs:41:17
|
LL | fn foo<'z>() where &'z (): Sized {
| -------------------------------- fn() {foo::<ReStatic>} defined here
| -------------------------------- function `foo` defined here
...
LL | let x: () = foo::<'static>;
| -- ^^^^^^^^^^^^^^ expected `()`, found fn item

View File

@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/issue-35241.rs:3:20
|
LL | struct Foo(u32);
| ---------- fn(u32) -> Foo {Foo} defined here
| ---------- `Foo` defines a struct constructor here, which should be called
LL |
LL | fn test() -> Foo { Foo }
| --- ^^^ expected struct `Foo`, found struct constructor

View File

@ -264,7 +264,7 @@ error[E0308]: mismatched types
--> $DIR/privacy-enum-ctor.rs:27:20
|
LL | Fn(u8),
| -- fn(u8) -> Z {Z::Fn} defined here
| -- `Fn` defines an enum variant constructor here, which should be called
...
LL | let _: Z = Z::Fn;
| - ^^^^^ expected enum `Z`, found enum constructor
@ -305,7 +305,7 @@ error[E0308]: mismatched types
--> $DIR/privacy-enum-ctor.rs:43:16
|
LL | Fn(u8),
| -- fn(u8) -> E {E::Fn} defined here
| -- `Fn` defines an enum variant constructor here, which should be called
...
LL | let _: E = m::E::Fn;
| - ^^^^^^^^ expected enum `E`, found enum constructor
@ -346,7 +346,7 @@ error[E0308]: mismatched types
--> $DIR/privacy-enum-ctor.rs:51:16
|
LL | Fn(u8),
| -- fn(u8) -> E {E::Fn} defined here
| -- `Fn` defines an enum variant constructor here, which should be called
...
LL | let _: E = E::Fn;
| - ^^^^^ expected enum `E`, found enum constructor

View File

@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/fn-or-tuple-struct-without-args.rs:29:20
|
LL | fn foo(a: usize, b: usize) -> usize { a }
| ----------------------------------- fn(usize, usize) -> usize {foo} defined here
| ----------------------------------- function `foo` defined here
...
LL | let _: usize = foo;
| ----- ^^^ expected `usize`, found fn item
@ -20,7 +20,7 @@ error[E0308]: mismatched types
--> $DIR/fn-or-tuple-struct-without-args.rs:30:16
|
LL | struct S(usize, usize);
| -------- fn(usize, usize) -> S {S} defined here
| -------- `S` defines a struct constructor here, which should be called
...
LL | let _: S = S;
| - ^ expected struct `S`, found struct constructor
@ -38,7 +38,7 @@ error[E0308]: mismatched types
--> $DIR/fn-or-tuple-struct-without-args.rs:31:20
|
LL | fn bar() -> usize { 42 }
| ----------------- fn() -> usize {bar} defined here
| ----------------- function `bar` defined here
...
LL | let _: usize = bar;
| ----- ^^^ expected `usize`, found fn item
@ -56,7 +56,7 @@ error[E0308]: mismatched types
--> $DIR/fn-or-tuple-struct-without-args.rs:32:16
|
LL | struct V();
| -------- fn() -> V {V} defined here
| -------- `V` defines a struct constructor here, which should be called
...
LL | let _: V = V;
| - ^ expected struct `V`, found struct constructor
@ -74,7 +74,7 @@ error[E0308]: mismatched types
--> $DIR/fn-or-tuple-struct-without-args.rs:33:20
|
LL | fn baz(x: usize, y: usize) -> usize { x }
| ----------------------------------- fn(usize, usize) -> usize {<_ as T>::baz} defined here
| ----------------------------------- associated function `baz` defined here
...
LL | let _: usize = T::baz;
| ----- ^^^^^^ expected `usize`, found fn item
@ -92,7 +92,7 @@ error[E0308]: mismatched types
--> $DIR/fn-or-tuple-struct-without-args.rs:34:20
|
LL | fn bat(x: usize) -> usize { 42 }
| ------------------------- fn(usize) -> usize {<_ as T>::bat} defined here
| ------------------------- associated function `bat` defined here
...
LL | let _: usize = T::bat;
| ----- ^^^^^^ expected `usize`, found fn item
@ -110,7 +110,7 @@ error[E0308]: mismatched types
--> $DIR/fn-or-tuple-struct-without-args.rs:35:16
|
LL | A(usize),
| - fn(usize) -> E {E::A} defined here
| - `A` defines an enum variant constructor here, which should be called
...
LL | let _: E = E::A;
| - ^^^^ expected enum `E`, found enum constructor
@ -134,7 +134,7 @@ error[E0308]: mismatched types
--> $DIR/fn-or-tuple-struct-without-args.rs:37:20
|
LL | fn baz(x: usize, y: usize) -> usize { x }
| ----------------------------------- fn(usize, usize) -> usize {<X as T>::baz} defined here
| ----------------------------------- associated function `baz` defined here
...
LL | let _: usize = X::baz;
| ----- ^^^^^^ expected `usize`, found fn item
@ -152,7 +152,7 @@ error[E0308]: mismatched types
--> $DIR/fn-or-tuple-struct-without-args.rs:38:20
|
LL | fn bat(x: usize) -> usize { 42 }
| ------------------------- fn(usize) -> usize {<X as T>::bat} defined here
| ------------------------- associated function `bat` defined here
...
LL | let _: usize = X::bat;
| ----- ^^^^^^ expected `usize`, found fn item
@ -170,7 +170,7 @@ error[E0308]: mismatched types
--> $DIR/fn-or-tuple-struct-without-args.rs:39:20
|
LL | fn bax(x: usize) -> usize { 42 }
| ------------------------- fn(usize) -> usize {<X as T>::bax} defined here
| ------------------------- associated function `bax` defined here
...
LL | let _: usize = X::bax;
| ----- ^^^^^^ expected `usize`, found fn item
@ -188,7 +188,7 @@ error[E0308]: mismatched types
--> $DIR/fn-or-tuple-struct-without-args.rs:40:20
|
LL | fn bach(x: usize) -> usize;
| --------------------------- fn(usize) -> usize {<X as T>::bach} defined here
| --------------------------- associated function `bach` defined here
...
LL | let _: usize = X::bach;
| ----- ^^^^^^^ expected `usize`, found fn item
@ -206,7 +206,7 @@ error[E0308]: mismatched types
--> $DIR/fn-or-tuple-struct-without-args.rs:41:20
|
LL | fn ban(&self) -> usize { 42 }
| ---------------------- for<'a> fn(&'a X) -> usize {<X as T>::ban} defined here
| ---------------------- associated function `ban` defined here
...
LL | let _: usize = X::ban;
| ----- ^^^^^^ expected `usize`, found fn item
@ -224,7 +224,7 @@ error[E0308]: mismatched types
--> $DIR/fn-or-tuple-struct-without-args.rs:42:20
|
LL | fn bal(&self) -> usize;
| ----------------------- for<'a> fn(&'a X) -> usize {<X as T>::bal} defined here
| ----------------------- associated function `bal` defined here
...
LL | let _: usize = X::bal;
| ----- ^^^^^^ expected `usize`, found fn item