Skip to content

Commit fab7a6a

Browse files
authored
Rollup merge of #95238 - TaKO8Ki:stop-emitting-E0026-for-struct-enum-with-underscore, r=estebank
Stop emitting E0026 for struct enums with underscores This patch resolves a part of #83263; r? `@estebank`
2 parents 1f346bd + 925857d commit fab7a6a

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

compiler/rustc_typeck/src/check/pat.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
1717
use rustc_span::hygiene::DesugaringKind;
1818
use rustc_span::lev_distance::find_best_match_for_name;
1919
use rustc_span::source_map::{Span, Spanned};
20-
use rustc_span::symbol::{sym, Ident};
20+
use rustc_span::symbol::{kw, sym, Ident};
2121
use rustc_span::{BytePos, MultiSpan, DUMMY_SP};
2222
use rustc_trait_selection::autoderef::Autoderef;
2323
use rustc_trait_selection::traits::{ObligationCause, Pattern};
@@ -1275,7 +1275,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12751275
.filter(|(_, ident)| !used_fields.contains_key(ident))
12761276
.collect::<Vec<_>>();
12771277

1278-
let inexistent_fields_err = if !(inexistent_fields.is_empty() || variant.is_recovered()) {
1278+
let inexistent_fields_err = if !(inexistent_fields.is_empty() || variant.is_recovered())
1279+
&& !inexistent_fields.iter().any(|field| field.name == kw::Underscore)
1280+
{
12791281
Some(self.error_inexistent_fields(
12801282
adt.variant_descr(),
12811283
&inexistent_fields,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
enum Foo {
2+
Bar { bar: bool },
3+
Other,
4+
}
5+
6+
fn main() {
7+
let foo = Some(Foo::Other);
8+
9+
if let Some(Foo::Bar {_}) = foo {}
10+
//~^ ERROR expected identifier, found reserved identifier `_`
11+
//~| ERROR pattern does not mention field `bar` [E0027]
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error: expected identifier, found reserved identifier `_`
2+
--> $DIR/struct-enum-ignoring-field-with-underscore.rs:9:27
3+
|
4+
LL | if let Some(Foo::Bar {_}) = foo {}
5+
| ^ expected identifier, found reserved identifier
6+
7+
error[E0027]: pattern does not mention field `bar`
8+
--> $DIR/struct-enum-ignoring-field-with-underscore.rs:9:17
9+
|
10+
LL | if let Some(Foo::Bar {_}) = foo {}
11+
| ^^^^^^^^^^^^ missing field `bar`
12+
|
13+
help: include the missing field in the pattern
14+
|
15+
LL | if let Some(Foo::Bar {_, bar }) = foo {}
16+
| ~~~~~~~
17+
help: if you don't care about this missing field, you can explicitly ignore it
18+
|
19+
LL | if let Some(Foo::Bar {_, .. }) = foo {}
20+
| ~~~~~~
21+
22+
error: aborting due to 2 previous errors
23+
24+
For more information about this error, try `rustc --explain E0027`.

0 commit comments

Comments
 (0)