Skip to content

Commit de08d0e

Browse files
authored
Rollup merge of rust-lang#59323 - euclio:enum-instead-of-variant, r=varkor
use suggestions for "enum instead of variant" error
2 parents 9d33d6f + 16a8abe commit de08d0e

File tree

5 files changed

+118
-47
lines changed

5 files changed

+118
-47
lines changed

src/librustc_resolve/error_reporting.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,20 @@ impl<'a> Resolver<'a> {
293293
(Def::Enum(..), PathSource::TupleStruct)
294294
| (Def::Enum(..), PathSource::Expr(..)) => {
295295
if let Some(variants) = self.collect_enum_variants(def) {
296-
err.note(&format!("did you mean to use one \
297-
of the following variants?\n{}",
298-
variants.iter()
299-
.map(|suggestion| path_names_to_string(suggestion))
300-
.map(|suggestion| format!("- `{}`", suggestion))
301-
.collect::<Vec<_>>()
302-
.join("\n")));
296+
if !variants.is_empty() {
297+
let msg = if variants.len() == 1 {
298+
"try using the enum's variant"
299+
} else {
300+
"try using one of the enum's variants"
301+
};
302+
303+
err.span_suggestions(
304+
span,
305+
msg,
306+
variants.iter().map(path_names_to_string),
307+
Applicability::MaybeIncorrect,
308+
);
309+
}
303310
} else {
304311
err.note("did you mean to use one of the enum's variants?");
305312
}

src/test/ui/did_you_mean/issue-43871-enum-instead-of-variant.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
enum Example { Ex(String), NotEx }
22

3+
enum Void {}
4+
5+
enum ManyVariants {
6+
One,
7+
Two,
8+
Three,
9+
Four,
10+
Five,
11+
Six,
12+
Seven,
13+
Eight,
14+
Nine,
15+
Ten,
16+
}
17+
318
fn result_test() {
419
let x = Option(1); //~ ERROR expected function, found enum
520

@@ -12,6 +27,10 @@ fn result_test() {
1227
if let Example(_) = y { //~ ERROR expected tuple struct/variant, found enum
1328
println!("It is OK.");
1429
}
30+
31+
let y = Void(); //~ ERROR expected function, found enum
32+
33+
let z = ManyVariants(); //~ ERROR expected function, found enum
1534
}
1635

1736
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,63 @@
11
error[E0423]: expected function, found enum `Option`
2-
--> $DIR/issue-43871-enum-instead-of-variant.rs:4:13
2+
--> $DIR/issue-43871-enum-instead-of-variant.rs:19:13
33
|
44
LL | let x = Option(1);
55
| ^^^^^^
6+
help: try using one of the enum's variants
67
|
7-
= note: did you mean to use one of the following variants?
8-
- `std::prelude::v1::Option::None`
9-
- `std::prelude::v1::Option::Some`
8+
LL | let x = std::prelude::v1::Option::None(1);
9+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
LL | let x = std::prelude::v1::Option::Some(1);
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1012

1113
error[E0532]: expected tuple struct/variant, found enum `Option`
12-
--> $DIR/issue-43871-enum-instead-of-variant.rs:6:12
14+
--> $DIR/issue-43871-enum-instead-of-variant.rs:21:12
1315
|
1416
LL | if let Option(_) = x {
1517
| ^^^^^^
18+
help: try using one of the enum's variants
1619
|
17-
= note: did you mean to use one of the following variants?
18-
- `std::prelude::v1::Option::None`
19-
- `std::prelude::v1::Option::Some`
20+
LL | if let std::prelude::v1::Option::None(_) = x {
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22+
LL | if let std::prelude::v1::Option::Some(_) = x {
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024

2125
error[E0532]: expected tuple struct/variant, found enum `Example`
22-
--> $DIR/issue-43871-enum-instead-of-variant.rs:12:12
26+
--> $DIR/issue-43871-enum-instead-of-variant.rs:27:12
2327
|
2428
LL | if let Example(_) = y {
2529
| ^^^^^^^
30+
help: try using one of the enum's variants
2631
|
27-
= note: did you mean to use one of the following variants?
28-
- `Example::Ex`
29-
- `Example::NotEx`
32+
LL | if let Example::Ex(_) = y {
33+
| ^^^^^^^^^^^
34+
LL | if let Example::NotEx(_) = y {
35+
| ^^^^^^^^^^^^^^
3036

31-
error: aborting due to 3 previous errors
37+
error[E0423]: expected function, found enum `Void`
38+
--> $DIR/issue-43871-enum-instead-of-variant.rs:31:13
39+
|
40+
LL | let y = Void();
41+
| ^^^^
42+
43+
error[E0423]: expected function, found enum `ManyVariants`
44+
--> $DIR/issue-43871-enum-instead-of-variant.rs:33:13
45+
|
46+
LL | let z = ManyVariants();
47+
| ^^^^^^^^^^^^
48+
help: try using one of the enum's variants
49+
|
50+
LL | let z = ManyVariants::Eight();
51+
| ^^^^^^^^^^^^^^^^^^^
52+
LL | let z = ManyVariants::Five();
53+
| ^^^^^^^^^^^^^^^^^^
54+
LL | let z = ManyVariants::Four();
55+
| ^^^^^^^^^^^^^^^^^^
56+
LL | let z = ManyVariants::Nine();
57+
| ^^^^^^^^^^^^^^^^^^
58+
and 6 other candidates
59+
60+
error: aborting due to 5 previous errors
3261

3362
Some errors occurred: E0423, E0532.
3463
For more information about an error, try `rustc --explain E0423`.

src/test/ui/glob-resolve1.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ error[E0423]: expected value, found enum `B`
2222
--> $DIR/glob-resolve1.rs:24:5
2323
|
2424
LL | B;
25-
| ^
26-
|
27-
= note: did you mean to use one of the following variants?
28-
- `B::B1`
25+
| ^ help: try using the enum's variant: `B::B1`
2926

3027
error[E0425]: cannot find value `C` in this scope
3128
--> $DIR/glob-resolve1.rs:25:5

src/test/ui/resolve/privacy-enum-ctor.stderr

+42-23
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,32 @@ error[E0423]: expected value, found enum `n::Z`
33
|
44
LL | n::Z;
55
| ^^^^
6-
|
7-
= note: did you mean to use one of the following variants?
8-
- `m::Z::Fn`
9-
- `m::Z::Struct`
10-
- `m::Z::Unit`
6+
help: try using one of the enum's variants
7+
|
8+
LL | m::Z::Fn;
9+
| ^^^^^^^^
10+
LL | m::Z::Struct;
11+
| ^^^^^^^^^^^^
12+
LL | m::Z::Unit;
13+
| ^^^^^^^^^^
1114

1215
error[E0423]: expected value, found enum `Z`
1316
--> $DIR/privacy-enum-ctor.rs:25:9
1417
|
1518
LL | Z;
16-
| ^ help: a function with a similar name exists: `f`
19+
| ^
20+
help: a function with a similar name exists
1721
|
18-
= note: did you mean to use one of the following variants?
19-
- `m::Z::Fn`
20-
- `m::Z::Struct`
21-
- `m::Z::Unit`
22+
LL | f;
23+
| ^
24+
help: try using one of the enum's variants
25+
|
26+
LL | m::Z::Fn;
27+
| ^^^^^^^^
28+
LL | m::Z::Struct;
29+
| ^^^^^^^^^^^^
30+
LL | m::Z::Unit;
31+
| ^^^^^^^^^^
2232

2333
error[E0423]: expected value, found struct variant `Z::Struct`
2434
--> $DIR/privacy-enum-ctor.rs:29:20
@@ -31,15 +41,18 @@ error[E0423]: expected value, found enum `m::E`
3141
|
3242
LL | let _: E = m::E;
3343
| ^^^^
34-
|
35-
= note: did you mean to use one of the following variants?
36-
- `E::Fn`
37-
- `E::Struct`
38-
- `E::Unit`
3944
help: a function with a similar name exists
4045
|
4146
LL | let _: E = m::f;
4247
| ^
48+
help: try using one of the enum's variants
49+
|
50+
LL | let _: E = E::Fn;
51+
| ^^^^^
52+
LL | let _: E = E::Struct;
53+
| ^^^^^^^^^
54+
LL | let _: E = E::Unit;
55+
| ^^^^^^^
4356
help: possible better candidates are found in other modules, you can import them into scope
4457
|
4558
LL | use std::f32::consts::E;
@@ -58,11 +71,14 @@ error[E0423]: expected value, found enum `E`
5871
|
5972
LL | let _: E = E;
6073
| ^
74+
help: try using one of the enum's variants
6175
|
62-
= note: did you mean to use one of the following variants?
63-
- `E::Fn`
64-
- `E::Struct`
65-
- `E::Unit`
76+
LL | let _: E = E::Fn;
77+
| ^^^^^
78+
LL | let _: E = E::Struct;
79+
| ^^^^^^^^^
80+
LL | let _: E = E::Unit;
81+
| ^^^^^^^
6682
help: possible better candidates are found in other modules, you can import them into scope
6783
|
6884
LL | use std::f32::consts::E;
@@ -95,11 +111,14 @@ error[E0423]: expected value, found enum `m::n::Z`
95111
|
96112
LL | let _: Z = m::n::Z;
97113
| ^^^^^^^
114+
help: try using one of the enum's variants
98115
|
99-
= note: did you mean to use one of the following variants?
100-
- `m::Z::Fn`
101-
- `m::Z::Struct`
102-
- `m::Z::Unit`
116+
LL | let _: Z = m::Z::Fn;
117+
| ^^^^^^^^
118+
LL | let _: Z = m::Z::Struct;
119+
| ^^^^^^^^^^^^
120+
LL | let _: Z = m::Z::Unit;
121+
| ^^^^^^^^^^
103122

104123
error[E0412]: cannot find type `Z` in this scope
105124
--> $DIR/privacy-enum-ctor.rs:61:12

0 commit comments

Comments
 (0)