Skip to content

Commit f378155

Browse files
authored
Rollup merge of #101161 - ldm0:ldm_fix_diagnostic, r=cjgillot
Fix uintended diagnostic caused by `drain(..)` Calling `drain(..)` makes later `suggestable_variants.is_empty()` always true, which makes the diagnostics unintended.
2 parents ba0011c + 3f66efd commit f378155

File tree

4 files changed

+58
-27
lines changed

4 files changed

+58
-27
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
17921792
}
17931793
};
17941794

1795-
let mut suggestable_variants = variants
1795+
let suggestable_variants = variants
17961796
.iter()
17971797
.filter(|(_, def_id, kind)| !needs_placeholder(*def_id, *kind))
17981798
.map(|(variant, _, kind)| (path_names_to_string(variant), kind))
@@ -1802,8 +1802,9 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
18021802
CtorKind::Fictive => format!("({} {{}})", variant),
18031803
})
18041804
.collect::<Vec<_>>();
1805+
let no_suggestable_variant = suggestable_variants.is_empty();
18051806

1806-
if !suggestable_variants.is_empty() {
1807+
if !no_suggestable_variant {
18071808
let msg = if suggestable_variants.len() == 1 {
18081809
"you might have meant to use the following enum variant"
18091810
} else {
@@ -1813,7 +1814,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
18131814
err.span_suggestions(
18141815
span,
18151816
msg,
1816-
suggestable_variants.drain(..),
1817+
suggestable_variants.into_iter(),
18171818
Applicability::MaybeIncorrect,
18181819
);
18191820
}
@@ -1830,15 +1831,15 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
18301831
.collect::<Vec<_>>();
18311832

18321833
if !suggestable_variants_with_placeholders.is_empty() {
1833-
let msg = match (
1834-
suggestable_variants.is_empty(),
1835-
suggestable_variants_with_placeholders.len(),
1836-
) {
1837-
(true, 1) => "the following enum variant is available",
1838-
(true, _) => "the following enum variants are available",
1839-
(false, 1) => "alternatively, the following enum variant is available",
1840-
(false, _) => "alternatively, the following enum variants are also available",
1841-
};
1834+
let msg =
1835+
match (no_suggestable_variant, suggestable_variants_with_placeholders.len()) {
1836+
(true, 1) => "the following enum variant is available",
1837+
(true, _) => "the following enum variants are available",
1838+
(false, 1) => "alternatively, the following enum variant is available",
1839+
(false, _) => {
1840+
"alternatively, the following enum variants are also available"
1841+
}
1842+
};
18421843

18431844
err.span_suggestions(
18441845
span,

src/test/ui/resolve/issue-73427.rs

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ enum D {
2222
Unit,
2323
}
2424

25+
enum E {
26+
TupleWithFields(()),
27+
}
28+
2529
fn main() {
2630
// Only variants without fields are suggested (and others mentioned in a note) where an enum
2731
// is used rather than a variant.
@@ -34,6 +38,8 @@ fn main() {
3438
//~^ ERROR expected value, found enum `C`
3539
D.foo();
3640
//~^ ERROR expected value, found enum `D`
41+
E.foo();
42+
//~^ ERROR expected value, found enum `E`
3743

3844
// Only tuple variants are suggested in calls or tuple struct pattern matching.
3945

src/test/ui/resolve/issue-73427.stderr

+34-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0423]: expected value, found enum `A`
2-
--> $DIR/issue-73427.rs:29:5
2+
--> $DIR/issue-73427.rs:33:5
33
|
44
LL | A.foo();
55
| ^
@@ -23,15 +23,15 @@ LL | (A::Tuple()).foo();
2323
| ~~~~~~~~~~~~
2424
LL | A::Unit.foo();
2525
| ~~~~~~~
26-
help: the following enum variants are available
26+
help: alternatively, the following enum variants are also available
2727
|
2828
LL | (A::StructWithFields { /* fields */ }).foo();
2929
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3030
LL | (A::TupleWithFields(/* fields */)).foo();
3131
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3232

3333
error[E0423]: expected value, found enum `B`
34-
--> $DIR/issue-73427.rs:31:5
34+
--> $DIR/issue-73427.rs:35:5
3535
|
3636
LL | B.foo();
3737
| ^
@@ -52,7 +52,7 @@ LL | (B::TupleWithFields(/* fields */)).foo();
5252
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5353

5454
error[E0423]: expected value, found enum `C`
55-
--> $DIR/issue-73427.rs:33:5
55+
--> $DIR/issue-73427.rs:37:5
5656
|
5757
LL | C.foo();
5858
| ^
@@ -70,15 +70,15 @@ help: you might have meant to use the following enum variant
7070
|
7171
LL | C::Unit.foo();
7272
| ~~~~~~~
73-
help: the following enum variants are available
73+
help: alternatively, the following enum variants are also available
7474
|
7575
LL | (C::StructWithFields { /* fields */ }).foo();
7676
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7777
LL | (C::TupleWithFields(/* fields */)).foo();
7878
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7979

8080
error[E0423]: expected value, found enum `D`
81-
--> $DIR/issue-73427.rs:35:5
81+
--> $DIR/issue-73427.rs:39:5
8282
|
8383
LL | D.foo();
8484
| ^
@@ -95,13 +95,37 @@ help: you might have meant to use the following enum variant
9595
|
9696
LL | D::Unit.foo();
9797
| ~~~~~~~
98-
help: the following enum variant is available
98+
help: alternatively, the following enum variant is available
9999
|
100100
LL | (D::TupleWithFields(/* fields */)).foo();
101101
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102102

103+
error[E0423]: expected value, found enum `E`
104+
--> $DIR/issue-73427.rs:41:5
105+
|
106+
LL | E.foo();
107+
| ^
108+
|
109+
note: the enum is defined here
110+
--> $DIR/issue-73427.rs:25:1
111+
|
112+
LL | / enum E {
113+
LL | | TupleWithFields(()),
114+
LL | | }
115+
| |_^
116+
help: the following enum variant is available
117+
|
118+
LL | (E::TupleWithFields(/* fields */)).foo();
119+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
120+
help: consider importing one of these items instead
121+
|
122+
LL | use std::f32::consts::E;
123+
|
124+
LL | use std::f64::consts::E;
125+
|
126+
103127
error[E0423]: expected function, tuple struct or tuple variant, found enum `A`
104-
--> $DIR/issue-73427.rs:40:13
128+
--> $DIR/issue-73427.rs:46:13
105129
|
106130
LL | let x = A(3);
107131
| ^
@@ -126,7 +150,7 @@ LL | let x = A::TupleWithFields(3);
126150
| ~~~~~~~~~~~~~~~~~~
127151

128152
error[E0532]: expected tuple struct or tuple variant, found enum `A`
129-
--> $DIR/issue-73427.rs:42:12
153+
--> $DIR/issue-73427.rs:48:12
130154
|
131155
LL | if let A(3) = x { }
132156
| ^
@@ -150,7 +174,7 @@ LL | if let A::Tuple(3) = x { }
150174
LL | if let A::TupleWithFields(3) = x { }
151175
| ~~~~~~~~~~~~~~~~~~
152176

153-
error: aborting due to 6 previous errors
177+
error: aborting due to 7 previous errors
154178

155179
Some errors have detailed explanations: E0423, E0532.
156180
For more information about an error, try `rustc --explain E0423`.

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ help: you might have meant to use the following enum variant
1919
|
2020
LL | m::Z::Unit;
2121
| ~~~~~~~~~~
22-
help: the following enum variants are available
22+
help: alternatively, the following enum variants are also available
2323
|
2424
LL | (m::Z::Fn(/* fields */));
2525
| ~~~~~~~~~~~~~~~~~~~~~~~~
@@ -47,7 +47,7 @@ help: you might have meant to use the following enum variant
4747
|
4848
LL | m::Z::Unit;
4949
| ~~~~~~~~~~
50-
help: the following enum variants are available
50+
help: alternatively, the following enum variants are also available
5151
|
5252
LL | (m::Z::Fn(/* fields */));
5353
| ~~~~~~~~~~~~~~~~~~~~~~~~
@@ -89,7 +89,7 @@ help: you might have meant to use the following enum variant
8989
|
9090
LL | let _: E = E::Unit;
9191
| ~~~~~~~
92-
help: the following enum variants are available
92+
help: alternatively, the following enum variants are also available
9393
|
9494
LL | let _: E = (E::Fn(/* fields */));
9595
| ~~~~~~~~~~~~~~~~~~~~~
@@ -143,7 +143,7 @@ help: you might have meant to use the following enum variant
143143
|
144144
LL | let _: E = E::Unit;
145145
| ~~~~~~~
146-
help: the following enum variants are available
146+
help: alternatively, the following enum variants are also available
147147
|
148148
LL | let _: E = (E::Fn(/* fields */));
149149
| ~~~~~~~~~~~~~~~~~~~~~
@@ -203,7 +203,7 @@ help: you might have meant to use the following enum variant
203203
|
204204
LL | let _: Z = m::Z::Unit;
205205
| ~~~~~~~~~~
206-
help: the following enum variants are available
206+
help: alternatively, the following enum variants are also available
207207
|
208208
LL | let _: Z = (m::Z::Fn(/* fields */));
209209
| ~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)