Be more specific about constructor FnDefs in type mismatch

This commit is contained in:
Michael Goulet 2023-01-06 03:09:14 +00:00
parent b22c152958
commit ede5c31af4
11 changed files with 39 additions and 31 deletions

View File

@ -2,10 +2,10 @@
use crate::ty::diagnostics::suggest_constraining_type_param; use crate::ty::diagnostics::suggest_constraining_type_param;
use crate::ty::print::{with_forced_trimmed_paths, FmtPrinter, Printer}; use crate::ty::print::{with_forced_trimmed_paths, FmtPrinter, Printer};
use crate::ty::{self, BoundRegionKind, Region, Ty, TyCtxt}; use crate::ty::{self, BoundRegionKind, Region, Ty, TyCtxt};
use hir::def::DefKind;
use rustc_errors::Applicability::{MachineApplicable, MaybeIncorrect}; use rustc_errors::Applicability::{MachineApplicable, MaybeIncorrect};
use rustc_errors::{pluralize, Diagnostic, MultiSpan}; use rustc_errors::{pluralize, Diagnostic, MultiSpan};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::{CtorOf, DefKind};
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_span::symbol::{sym, Symbol}; use rustc_span::symbol::{sym, Symbol};
use rustc_span::{BytePos, Span}; use rustc_span::{BytePos, Span};
@ -319,7 +319,11 @@ pub fn sort_string(self, tcx: TyCtxt<'_>) -> Cow<'static, str> {
.into() .into()
} }
} }
ty::FnDef(..) => "fn item".into(), ty::FnDef(def_id, ..) => match tcx.def_kind(def_id) {
DefKind::Ctor(CtorOf::Struct, _) => "struct constructor".into(),
DefKind::Ctor(CtorOf::Variant, _) => "enum constructor".into(),
_ => "fn item".into(),
},
ty::FnPtr(_) => "fn pointer".into(), ty::FnPtr(_) => "fn pointer".into(),
ty::Dynamic(ref inner, ..) if let Some(principal) = inner.principal() => { ty::Dynamic(ref inner, ..) if let Some(principal) = inner.principal() => {
format!("trait object `dyn {}`", tcx.def_path_str(principal.def_id())).into() format!("trait object `dyn {}`", tcx.def_path_str(principal.def_id())).into()
@ -366,7 +370,11 @@ pub fn prefix_string(self, tcx: TyCtxt<'_>) -> Cow<'static, str> {
_ => "reference", _ => "reference",
} }
.into(), .into(),
ty::FnDef(..) => "fn item".into(), ty::FnDef(def_id, ..) => match tcx.def_kind(def_id) {
DefKind::Ctor(CtorOf::Struct, _) => "struct constructor".into(),
DefKind::Ctor(CtorOf::Variant, _) => "enum constructor".into(),
_ => "fn item".into(),
},
ty::FnPtr(_) => "fn pointer".into(), ty::FnPtr(_) => "fn pointer".into(),
ty::Dynamic(..) => "trait object".into(), ty::Dynamic(..) => "trait object".into(),
ty::Closure(..) => "closure".into(), ty::Closure(..) => "closure".into(),

View File

@ -5,12 +5,12 @@ LL | struct Foo(u32);
| ---------- fn(u32) -> Foo {Foo} defined here | ---------- fn(u32) -> Foo {Foo} defined here
LL | LL |
LL | fn test() -> Foo { Foo } LL | fn test() -> Foo { Foo }
| --- ^^^ expected struct `Foo`, found fn item | --- ^^^ expected struct `Foo`, found struct constructor
| | | |
| expected `Foo` because of return type | expected `Foo` because of return type
| |
= note: expected struct `Foo` = note: expected struct `Foo`
found fn item `fn(u32) -> Foo {Foo}` found struct constructor `fn(u32) -> Foo {Foo}`
help: use parentheses to construct this tuple struct help: use parentheses to construct this tuple struct
| |
LL | fn test() -> Foo { Foo(/* u32 */) } LL | fn test() -> Foo { Foo(/* u32 */) }

View File

@ -267,12 +267,12 @@ LL | Fn(u8),
| -- fn(u8) -> Z {Z::Fn} defined here | -- fn(u8) -> Z {Z::Fn} defined here
... ...
LL | let _: Z = Z::Fn; LL | let _: Z = Z::Fn;
| - ^^^^^ expected enum `Z`, found fn item | - ^^^^^ expected enum `Z`, found enum constructor
| | | |
| expected due to this | expected due to this
| |
= note: expected enum `Z` = note: expected enum `Z`
found fn item `fn(u8) -> Z {Z::Fn}` found enum constructor `fn(u8) -> Z {Z::Fn}`
help: use parentheses to construct this tuple variant help: use parentheses to construct this tuple variant
| |
LL | let _: Z = Z::Fn(/* u8 */); LL | let _: Z = Z::Fn(/* u8 */);
@ -308,12 +308,12 @@ LL | Fn(u8),
| -- fn(u8) -> E {E::Fn} defined here | -- fn(u8) -> E {E::Fn} defined here
... ...
LL | let _: E = m::E::Fn; LL | let _: E = m::E::Fn;
| - ^^^^^^^^ expected enum `E`, found fn item | - ^^^^^^^^ expected enum `E`, found enum constructor
| | | |
| expected due to this | expected due to this
| |
= note: expected enum `E` = note: expected enum `E`
found fn item `fn(u8) -> E {E::Fn}` found enum constructor `fn(u8) -> E {E::Fn}`
help: use parentheses to construct this tuple variant help: use parentheses to construct this tuple variant
| |
LL | let _: E = m::E::Fn(/* u8 */); LL | let _: E = m::E::Fn(/* u8 */);
@ -349,12 +349,12 @@ LL | Fn(u8),
| -- fn(u8) -> E {E::Fn} defined here | -- fn(u8) -> E {E::Fn} defined here
... ...
LL | let _: E = E::Fn; LL | let _: E = E::Fn;
| - ^^^^^ expected enum `E`, found fn item | - ^^^^^ expected enum `E`, found enum constructor
| | | |
| expected due to this | expected due to this
| |
= note: expected enum `E` = note: expected enum `E`
found fn item `fn(u8) -> E {E::Fn}` found enum constructor `fn(u8) -> E {E::Fn}`
help: use parentheses to construct this tuple variant help: use parentheses to construct this tuple variant
| |
LL | let _: E = E::Fn(/* u8 */); LL | let _: E = E::Fn(/* u8 */);

View File

@ -23,12 +23,12 @@ LL | struct S(usize, usize);
| -------- fn(usize, usize) -> S {S} defined here | -------- fn(usize, usize) -> S {S} defined here
... ...
LL | let _: S = S; LL | let _: S = S;
| - ^ expected struct `S`, found fn item | - ^ expected struct `S`, found struct constructor
| | | |
| expected due to this | expected due to this
| |
= note: expected struct `S` = note: expected struct `S`
found fn item `fn(usize, usize) -> S {S}` found struct constructor `fn(usize, usize) -> S {S}`
help: use parentheses to construct this tuple struct help: use parentheses to construct this tuple struct
| |
LL | let _: S = S(/* usize */, /* usize */); LL | let _: S = S(/* usize */, /* usize */);
@ -59,12 +59,12 @@ LL | struct V();
| -------- fn() -> V {V} defined here | -------- fn() -> V {V} defined here
... ...
LL | let _: V = V; LL | let _: V = V;
| - ^ expected struct `V`, found fn item | - ^ expected struct `V`, found struct constructor
| | | |
| expected due to this | expected due to this
| |
= note: expected struct `V` = note: expected struct `V`
found fn item `fn() -> V {V}` found struct constructor `fn() -> V {V}`
help: use parentheses to construct this tuple struct help: use parentheses to construct this tuple struct
| |
LL | let _: V = V(); LL | let _: V = V();
@ -113,12 +113,12 @@ LL | A(usize),
| - fn(usize) -> E {E::A} defined here | - fn(usize) -> E {E::A} defined here
... ...
LL | let _: E = E::A; LL | let _: E = E::A;
| - ^^^^ expected enum `E`, found fn item | - ^^^^ expected enum `E`, found enum constructor
| | | |
| expected due to this | expected due to this
| |
= note: expected enum `E` = note: expected enum `E`
found fn item `fn(usize) -> E {E::A}` found enum constructor `fn(usize) -> E {E::A}`
help: use parentheses to construct this tuple variant help: use parentheses to construct this tuple variant
| |
LL | let _: E = E::A(/* usize */); LL | let _: E = E::A(/* usize */);

View File

@ -10,5 +10,5 @@ fn foo(&self) { }
fn main() { fn main() {
let thing = Bar { bar: Foo }; let thing = Bar { bar: Foo };
thing.bar.foo(); thing.bar.foo();
//~^ ERROR no method named `foo` found for fn item `fn() -> Foo {Foo}` in the current scope [E0599] //~^ ERROR no method named `foo` found for struct constructor `fn() -> Foo {Foo}` in the current scope [E0599]
} }

View File

@ -1,4 +1,4 @@
error[E0599]: no method named `foo` found for fn item `fn() -> Foo {Foo}` in the current scope error[E0599]: no method named `foo` found for struct constructor `fn() -> Foo {Foo}` in the current scope
--> $DIR/empty-tuple-method.rs:12:15 --> $DIR/empty-tuple-method.rs:12:15
| |
LL | thing.bar.foo(); LL | thing.bar.foo();

View File

@ -12,5 +12,5 @@ fn foo(&self) { }
fn main() { fn main() {
let thing = Bar { bar: Foo::Tup }; let thing = Bar { bar: Foo::Tup };
thing.bar.foo(); thing.bar.foo();
//~^ ERROR no method named `foo` found for fn item `fn() -> Foo {Foo::Tup}` in the current scope [E0599] //~^ ERROR no method named `foo` found for enum constructor `fn() -> Foo {Foo::Tup}` in the current scope [E0599]
} }

View File

@ -1,4 +1,4 @@
error[E0599]: no method named `foo` found for fn item `fn() -> Foo {Foo::Tup}` in the current scope error[E0599]: no method named `foo` found for enum constructor `fn() -> Foo {Foo::Tup}` in the current scope
--> $DIR/enum-variant.rs:14:15 --> $DIR/enum-variant.rs:14:15
| |
LL | thing.bar.foo(); LL | thing.bar.foo();

View File

@ -10,5 +10,5 @@ fn foo() { }
fn main() { fn main() {
let thing = Bar { bar: Foo }; let thing = Bar { bar: Foo };
thing.bar.foo(); thing.bar.foo();
//~^ ERROR no method named `foo` found for fn item `fn(u8, i32) -> Foo {Foo}` in the current scope [E0599] //~^ ERROR no method named `foo` found for struct constructor `fn(u8, i32) -> Foo {Foo}` in the current scope [E0599]
} }

View File

@ -1,4 +1,4 @@
error[E0599]: no method named `foo` found for fn item `fn(u8, i32) -> Foo {Foo}` in the current scope error[E0599]: no method named `foo` found for struct constructor `fn(u8, i32) -> Foo {Foo}` in the current scope
--> $DIR/tuple-method.rs:12:15 --> $DIR/tuple-method.rs:12:15
| |
LL | thing.bar.foo(); LL | thing.bar.foo();

View File

@ -1,4 +1,4 @@
error[E0599]: no method named `nonexistent_method` found for fn item `fn(_) -> Option<_> {Option::<_>::Some}` in the current scope error[E0599]: no method named `nonexistent_method` found for enum constructor `fn(_) -> Option<_> {Option::<_>::Some}` in the current scope
--> $DIR/issue-96738.rs:2:10 --> $DIR/issue-96738.rs:2:10
| |
LL | Some.nonexistent_method(); LL | Some.nonexistent_method();