Skip to content

Commit

Permalink
Rollup merge of rust-lang#101161 - ldm0:ldm_fix_diagnostic, r=cjgillot
Browse files Browse the repository at this point in the history
Fix uintended diagnostic caused by `drain(..)`

Calling `drain(..)` makes later `suggestable_variants.is_empty()` always true, which makes the diagnostics unintended.
  • Loading branch information
matthiaskrgr authored Aug 31, 2022
2 parents 3af942d + 3f66efd commit 579c050
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 27 deletions.
25 changes: 13 additions & 12 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1792,7 +1792,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
}
};

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

if !suggestable_variants.is_empty() {
if !no_suggestable_variant {
let msg = if suggestable_variants.len() == 1 {
"you might have meant to use the following enum variant"
} else {
Expand All @@ -1813,7 +1814,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
err.span_suggestions(
span,
msg,
suggestable_variants.drain(..),
suggestable_variants.into_iter(),
Applicability::MaybeIncorrect,
);
}
Expand All @@ -1830,15 +1831,15 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
.collect::<Vec<_>>();

if !suggestable_variants_with_placeholders.is_empty() {
let msg = match (
suggestable_variants.is_empty(),
suggestable_variants_with_placeholders.len(),
) {
(true, 1) => "the following enum variant is available",
(true, _) => "the following enum variants are available",
(false, 1) => "alternatively, the following enum variant is available",
(false, _) => "alternatively, the following enum variants are also available",
};
let msg =
match (no_suggestable_variant, suggestable_variants_with_placeholders.len()) {
(true, 1) => "the following enum variant is available",
(true, _) => "the following enum variants are available",
(false, 1) => "alternatively, the following enum variant is available",
(false, _) => {
"alternatively, the following enum variants are also available"
}
};

err.span_suggestions(
span,
Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/resolve/issue-73427.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ enum D {
Unit,
}

enum E {
TupleWithFields(()),
}

fn main() {
// Only variants without fields are suggested (and others mentioned in a note) where an enum
// is used rather than a variant.
Expand All @@ -34,6 +38,8 @@ fn main() {
//~^ ERROR expected value, found enum `C`
D.foo();
//~^ ERROR expected value, found enum `D`
E.foo();
//~^ ERROR expected value, found enum `E`

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

Expand Down
44 changes: 34 additions & 10 deletions src/test/ui/resolve/issue-73427.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0423]: expected value, found enum `A`
--> $DIR/issue-73427.rs:29:5
--> $DIR/issue-73427.rs:33:5
|
LL | A.foo();
| ^
Expand All @@ -23,15 +23,15 @@ LL | (A::Tuple()).foo();
| ~~~~~~~~~~~~
LL | A::Unit.foo();
| ~~~~~~~
help: the following enum variants are available
help: alternatively, the following enum variants are also available
|
LL | (A::StructWithFields { /* fields */ }).foo();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LL | (A::TupleWithFields(/* fields */)).foo();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

error[E0423]: expected value, found enum `C`
--> $DIR/issue-73427.rs:33:5
--> $DIR/issue-73427.rs:37:5
|
LL | C.foo();
| ^
Expand All @@ -70,15 +70,15 @@ help: you might have meant to use the following enum variant
|
LL | C::Unit.foo();
| ~~~~~~~
help: the following enum variants are available
help: alternatively, the following enum variants are also available
|
LL | (C::StructWithFields { /* fields */ }).foo();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LL | (C::TupleWithFields(/* fields */)).foo();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0423]: expected value, found enum `D`
--> $DIR/issue-73427.rs:35:5
--> $DIR/issue-73427.rs:39:5
|
LL | D.foo();
| ^
Expand All @@ -95,13 +95,37 @@ help: you might have meant to use the following enum variant
|
LL | D::Unit.foo();
| ~~~~~~~
help: the following enum variant is available
help: alternatively, the following enum variant is available
|
LL | (D::TupleWithFields(/* fields */)).foo();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0423]: expected value, found enum `E`
--> $DIR/issue-73427.rs:41:5
|
LL | E.foo();
| ^
|
note: the enum is defined here
--> $DIR/issue-73427.rs:25:1
|
LL | / enum E {
LL | | TupleWithFields(()),
LL | | }
| |_^
help: the following enum variant is available
|
LL | (E::TupleWithFields(/* fields */)).foo();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help: consider importing one of these items instead
|
LL | use std::f32::consts::E;
|
LL | use std::f64::consts::E;
|

error[E0423]: expected function, tuple struct or tuple variant, found enum `A`
--> $DIR/issue-73427.rs:40:13
--> $DIR/issue-73427.rs:46:13
|
LL | let x = A(3);
| ^
Expand All @@ -126,7 +150,7 @@ LL | let x = A::TupleWithFields(3);
| ~~~~~~~~~~~~~~~~~~

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

error: aborting due to 6 previous errors
error: aborting due to 7 previous errors

Some errors have detailed explanations: E0423, E0532.
For more information about an error, try `rustc --explain E0423`.
10 changes: 5 additions & 5 deletions src/test/ui/resolve/privacy-enum-ctor.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ help: you might have meant to use the following enum variant
|
LL | m::Z::Unit;
| ~~~~~~~~~~
help: the following enum variants are available
help: alternatively, the following enum variants are also available
|
LL | (m::Z::Fn(/* fields */));
| ~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -47,7 +47,7 @@ help: you might have meant to use the following enum variant
|
LL | m::Z::Unit;
| ~~~~~~~~~~
help: the following enum variants are available
help: alternatively, the following enum variants are also available
|
LL | (m::Z::Fn(/* fields */));
| ~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -89,7 +89,7 @@ help: you might have meant to use the following enum variant
|
LL | let _: E = E::Unit;
| ~~~~~~~
help: the following enum variants are available
help: alternatively, the following enum variants are also available
|
LL | let _: E = (E::Fn(/* fields */));
| ~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -143,7 +143,7 @@ help: you might have meant to use the following enum variant
|
LL | let _: E = E::Unit;
| ~~~~~~~
help: the following enum variants are available
help: alternatively, the following enum variants are also available
|
LL | let _: E = (E::Fn(/* fields */));
| ~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -203,7 +203,7 @@ help: you might have meant to use the following enum variant
|
LL | let _: Z = m::Z::Unit;
| ~~~~~~~~~~
help: the following enum variants are available
help: alternatively, the following enum variants are also available
|
LL | let _: Z = (m::Z::Fn(/* fields */));
| ~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down

0 comments on commit 579c050

Please sign in to comment.