Skip to content

Commit 4172756

Browse files
authored
Rollup merge of rust-lang#80613 - bugadani:issue-80607, r=matthewjasper
Diag: print enum variant instead of enum type Closes rust-lang#80607
2 parents 7a1b01e + e030071 commit 4172756

File tree

3 files changed

+60
-13
lines changed

3 files changed

+60
-13
lines changed

compiler/rustc_typeck/src/check/expr.rs

+36-13
Original file line numberDiff line numberDiff line change
@@ -1381,19 +1381,42 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13811381
ty,
13821382
);
13831383
match variant.ctor_kind {
1384-
CtorKind::Fn => {
1385-
err.span_label(variant.ident.span, format!("`{adt}` defined here", adt = ty));
1386-
err.span_label(field.ident.span, "field does not exist");
1387-
err.span_label(
1388-
ty_span,
1389-
format!(
1390-
"`{adt}` is a tuple {kind_name}, \
1391-
use the appropriate syntax: `{adt}(/* fields */)`",
1392-
adt = ty,
1393-
kind_name = kind_name
1394-
),
1395-
);
1396-
}
1384+
CtorKind::Fn => match ty.kind() {
1385+
ty::Adt(adt, ..) if adt.is_enum() => {
1386+
err.span_label(
1387+
variant.ident.span,
1388+
format!(
1389+
"`{adt}::{variant}` defined here",
1390+
adt = ty,
1391+
variant = variant.ident,
1392+
),
1393+
);
1394+
err.span_label(field.ident.span, "field does not exist");
1395+
err.span_label(
1396+
ty_span,
1397+
format!(
1398+
"`{adt}::{variant}` is a tuple {kind_name}, \
1399+
use the appropriate syntax: `{adt}::{variant}(/* fields */)`",
1400+
adt = ty,
1401+
variant = variant.ident,
1402+
kind_name = kind_name
1403+
),
1404+
);
1405+
}
1406+
_ => {
1407+
err.span_label(variant.ident.span, format!("`{adt}` defined here", adt = ty));
1408+
err.span_label(field.ident.span, "field does not exist");
1409+
err.span_label(
1410+
ty_span,
1411+
format!(
1412+
"`{adt}` is a tuple {kind_name}, \
1413+
use the appropriate syntax: `{adt}(/* fields */)`",
1414+
adt = ty,
1415+
kind_name = kind_name
1416+
),
1417+
);
1418+
}
1419+
},
13971420
_ => {
13981421
// prevent all specified fields from being suggested
13991422
let skip_fields = skip_fields.iter().map(|ref x| x.ident.name);

src/test/ui/issues/issue-80607.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// This tests makes sure the diagnostics print the offending enum variant, not just the type.
2+
pub enum Enum {
3+
V1(i32),
4+
}
5+
6+
pub fn foo(x: i32) -> Enum {
7+
Enum::V1 { x } //~ ERROR `Enum::V1` has no field named `x`
8+
}
9+
10+
fn main() {}

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

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0559]: variant `Enum::V1` has no field named `x`
2+
--> $DIR/issue-80607.rs:7:16
3+
|
4+
LL | V1(i32),
5+
| -- `Enum::V1` defined here
6+
...
7+
LL | Enum::V1 { x }
8+
| -------- ^ field does not exist
9+
| |
10+
| `Enum::V1` is a tuple variant, use the appropriate syntax: `Enum::V1(/* fields */)`
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0559`.

0 commit comments

Comments
 (0)