Skip to content

Commit ede5c31

Browse files
Be more specific about constructor FnDefs in type mismatch
1 parent b22c152 commit ede5c31

11 files changed

+39
-31
lines changed

compiler/rustc_middle/src/ty/error.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use crate::traits::{ObligationCause, ObligationCauseCode};
22
use crate::ty::diagnostics::suggest_constraining_type_param;
33
use crate::ty::print::{with_forced_trimmed_paths, FmtPrinter, Printer};
44
use crate::ty::{self, BoundRegionKind, Region, Ty, TyCtxt};
5-
use hir::def::DefKind;
65
use rustc_errors::Applicability::{MachineApplicable, MaybeIncorrect};
76
use rustc_errors::{pluralize, Diagnostic, MultiSpan};
87
use rustc_hir as hir;
8+
use rustc_hir::def::{CtorOf, DefKind};
99
use rustc_hir::def_id::DefId;
1010
use rustc_span::symbol::{sym, Symbol};
1111
use rustc_span::{BytePos, Span};
@@ -319,7 +319,11 @@ impl<'tcx> Ty<'tcx> {
319319
.into()
320320
}
321321
}
322-
ty::FnDef(..) => "fn item".into(),
322+
ty::FnDef(def_id, ..) => match tcx.def_kind(def_id) {
323+
DefKind::Ctor(CtorOf::Struct, _) => "struct constructor".into(),
324+
DefKind::Ctor(CtorOf::Variant, _) => "enum constructor".into(),
325+
_ => "fn item".into(),
326+
},
323327
ty::FnPtr(_) => "fn pointer".into(),
324328
ty::Dynamic(ref inner, ..) if let Some(principal) = inner.principal() => {
325329
format!("trait object `dyn {}`", tcx.def_path_str(principal.def_id())).into()
@@ -366,7 +370,11 @@ impl<'tcx> Ty<'tcx> {
366370
_ => "reference",
367371
}
368372
.into(),
369-
ty::FnDef(..) => "fn item".into(),
373+
ty::FnDef(def_id, ..) => match tcx.def_kind(def_id) {
374+
DefKind::Ctor(CtorOf::Struct, _) => "struct constructor".into(),
375+
DefKind::Ctor(CtorOf::Variant, _) => "enum constructor".into(),
376+
_ => "fn item".into(),
377+
},
370378
ty::FnPtr(_) => "fn pointer".into(),
371379
ty::Dynamic(..) => "trait object".into(),
372380
ty::Closure(..) => "closure".into(),

tests/ui/issues/issue-35241.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ LL | struct Foo(u32);
55
| ---------- fn(u32) -> Foo {Foo} defined here
66
LL |
77
LL | fn test() -> Foo { Foo }
8-
| --- ^^^ expected struct `Foo`, found fn item
8+
| --- ^^^ expected struct `Foo`, found struct constructor
99
| |
1010
| expected `Foo` because of return type
1111
|
12-
= note: expected struct `Foo`
13-
found fn item `fn(u32) -> Foo {Foo}`
12+
= note: expected struct `Foo`
13+
found struct constructor `fn(u32) -> Foo {Foo}`
1414
help: use parentheses to construct this tuple struct
1515
|
1616
LL | fn test() -> Foo { Foo(/* u32 */) }

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,12 @@ LL | Fn(u8),
267267
| -- fn(u8) -> Z {Z::Fn} defined here
268268
...
269269
LL | let _: Z = Z::Fn;
270-
| - ^^^^^ expected enum `Z`, found fn item
270+
| - ^^^^^ expected enum `Z`, found enum constructor
271271
| |
272272
| expected due to this
273273
|
274-
= note: expected enum `Z`
275-
found fn item `fn(u8) -> Z {Z::Fn}`
274+
= note: expected enum `Z`
275+
found enum constructor `fn(u8) -> Z {Z::Fn}`
276276
help: use parentheses to construct this tuple variant
277277
|
278278
LL | let _: Z = Z::Fn(/* u8 */);
@@ -308,12 +308,12 @@ LL | Fn(u8),
308308
| -- fn(u8) -> E {E::Fn} defined here
309309
...
310310
LL | let _: E = m::E::Fn;
311-
| - ^^^^^^^^ expected enum `E`, found fn item
311+
| - ^^^^^^^^ expected enum `E`, found enum constructor
312312
| |
313313
| expected due to this
314314
|
315-
= note: expected enum `E`
316-
found fn item `fn(u8) -> E {E::Fn}`
315+
= note: expected enum `E`
316+
found enum constructor `fn(u8) -> E {E::Fn}`
317317
help: use parentheses to construct this tuple variant
318318
|
319319
LL | let _: E = m::E::Fn(/* u8 */);
@@ -349,12 +349,12 @@ LL | Fn(u8),
349349
| -- fn(u8) -> E {E::Fn} defined here
350350
...
351351
LL | let _: E = E::Fn;
352-
| - ^^^^^ expected enum `E`, found fn item
352+
| - ^^^^^ expected enum `E`, found enum constructor
353353
| |
354354
| expected due to this
355355
|
356-
= note: expected enum `E`
357-
found fn item `fn(u8) -> E {E::Fn}`
356+
= note: expected enum `E`
357+
found enum constructor `fn(u8) -> E {E::Fn}`
358358
help: use parentheses to construct this tuple variant
359359
|
360360
LL | let _: E = E::Fn(/* u8 */);

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ LL | struct S(usize, usize);
2323
| -------- fn(usize, usize) -> S {S} defined here
2424
...
2525
LL | let _: S = S;
26-
| - ^ expected struct `S`, found fn item
26+
| - ^ expected struct `S`, found struct constructor
2727
| |
2828
| expected due to this
2929
|
30-
= note: expected struct `S`
31-
found fn item `fn(usize, usize) -> S {S}`
30+
= note: expected struct `S`
31+
found struct constructor `fn(usize, usize) -> S {S}`
3232
help: use parentheses to construct this tuple struct
3333
|
3434
LL | let _: S = S(/* usize */, /* usize */);
@@ -59,12 +59,12 @@ LL | struct V();
5959
| -------- fn() -> V {V} defined here
6060
...
6161
LL | let _: V = V;
62-
| - ^ expected struct `V`, found fn item
62+
| - ^ expected struct `V`, found struct constructor
6363
| |
6464
| expected due to this
6565
|
66-
= note: expected struct `V`
67-
found fn item `fn() -> V {V}`
66+
= note: expected struct `V`
67+
found struct constructor `fn() -> V {V}`
6868
help: use parentheses to construct this tuple struct
6969
|
7070
LL | let _: V = V();
@@ -113,12 +113,12 @@ LL | A(usize),
113113
| - fn(usize) -> E {E::A} defined here
114114
...
115115
LL | let _: E = E::A;
116-
| - ^^^^ expected enum `E`, found fn item
116+
| - ^^^^ expected enum `E`, found enum constructor
117117
| |
118118
| expected due to this
119119
|
120-
= note: expected enum `E`
121-
found fn item `fn(usize) -> E {E::A}`
120+
= note: expected enum `E`
121+
found enum constructor `fn(usize) -> E {E::A}`
122122
help: use parentheses to construct this tuple variant
123123
|
124124
LL | let _: E = E::A(/* usize */);

tests/ui/typeck/issue-87181/empty-tuple-method.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ impl Foo {
1010
fn main() {
1111
let thing = Bar { bar: Foo };
1212
thing.bar.foo();
13-
//~^ ERROR no method named `foo` found for fn item `fn() -> Foo {Foo}` in the current scope [E0599]
13+
//~^ ERROR no method named `foo` found for struct constructor `fn() -> Foo {Foo}` in the current scope [E0599]
1414
}

tests/ui/typeck/issue-87181/empty-tuple-method.stderr

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

tests/ui/typeck/issue-87181/enum-variant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ impl Foo {
1212
fn main() {
1313
let thing = Bar { bar: Foo::Tup };
1414
thing.bar.foo();
15-
//~^ ERROR no method named `foo` found for fn item `fn() -> Foo {Foo::Tup}` in the current scope [E0599]
15+
//~^ ERROR no method named `foo` found for enum constructor `fn() -> Foo {Foo::Tup}` in the current scope [E0599]
1616
}

tests/ui/typeck/issue-87181/enum-variant.stderr

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

tests/ui/typeck/issue-87181/tuple-method.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ impl Foo {
1010
fn main() {
1111
let thing = Bar { bar: Foo };
1212
thing.bar.foo();
13-
//~^ ERROR no method named `foo` found for fn item `fn(u8, i32) -> Foo {Foo}` in the current scope [E0599]
13+
//~^ ERROR no method named `foo` found for struct constructor `fn(u8, i32) -> Foo {Foo}` in the current scope [E0599]
1414
}

tests/ui/typeck/issue-87181/tuple-method.stderr

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

tests/ui/typeck/issue-96738.stderr

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

0 commit comments

Comments
 (0)