Skip to content

Commit e6b4102

Browse files
committed
Add experimental unnamed_enum_variants feature gate
1 parent c0bb140 commit e6b4102

10 files changed

Lines changed: 73 additions & 2 deletions

File tree

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
857857
}
858858

859859
fn lower_variant(&mut self, item_kind: &ItemKind, v: &Variant) -> hir::Variant<'hir> {
860+
if v.ident.name == kw::Underscore && self.tcx.features().unnamed_enum_variants() {
861+
// FIXME(#156628): lower unnamed enum variants to HIR.
862+
self.dcx()
863+
.struct_span_fatal(v.span, "unnamed enum variants are not yet implemented")
864+
.emit()
865+
}
860866
let hir_id = self.lower_node_id(v.id);
861867
self.lower_attrs(hir_id, &v.attrs, v.span, Target::Variant);
862868
hir::Variant {

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
511511
gate_all!(return_type_notation, "return type notation is experimental");
512512
gate_all!(super_let, "`super let` is experimental");
513513
gate_all!(try_blocks_heterogeneous, "`try bikeshed` expression is experimental");
514+
gate_all!(unnamed_enum_variants, "unnamed enum variants are experimental");
514515
gate_all!(unsafe_binders, "unsafe binder types are experimental");
515516
gate_all!(unsafe_fields, "`unsafe` fields are experimental");
516517
gate_all!(view_types, "view types are experimental");

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ declare_features! (
750750
/// Allows creation of instances of a struct by moving fields that have
751751
/// not changed from prior instances of the same struct (RFC #2528)
752752
(unstable, type_changing_struct_update, "1.58.0", Some(86555)),
753+
/// Allows using `_ = <range-or-int>` enum variants.
754+
(incomplete, unnamed_enum_variants, "CURRENT_RUSTC_VERSION", Some(156628)),
753755
/// Allows using `unsafe<'a> &'a T` unsafe binder types.
754756
(incomplete, unsafe_binders, "1.85.0", Some(130516)),
755757
/// Allows declaring fields `unsafe`.

compiler/rustc_parse/src/parser/item.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,14 +1915,18 @@ impl<'a> Parser<'a> {
19151915
None
19161916
};
19171917

1918+
let span = vlo.to(this.prev_token.span);
1919+
if ident.name == kw::Underscore {
1920+
this.psess.gated_spans.gate(sym::unnamed_enum_variants, span);
1921+
}
19181922
let vr = ast::Variant {
19191923
ident,
19201924
vis,
19211925
id: DUMMY_NODE_ID,
19221926
attrs: variant_attrs,
19231927
data: struct_def,
19241928
disr_expr,
1925-
span: vlo.to(this.prev_token.span),
1929+
span,
19261930
is_placeholder: false,
19271931
};
19281932

@@ -2382,7 +2386,10 @@ impl<'a> Parser<'a> {
23822386
/// for better diagnostics and suggestions.
23832387
fn parse_field_ident(&mut self, adt_ty: &str, lo: Span) -> PResult<'a, Ident> {
23842388
let (ident, is_raw) = self.ident_or_err(true)?;
2385-
if is_raw == IdentIsRaw::No && ident.is_reserved() {
2389+
if is_raw == IdentIsRaw::No
2390+
&& ident.is_reserved()
2391+
&& !(ident.name == kw::Underscore && adt_ty == "enum")
2392+
{
23862393
let snapshot = self.create_snapshot_for_diagnostic();
23872394
let err = if self.check_fn_front_matter(false, Case::Sensitive) {
23882395
let inherited_vis =

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,6 +2171,7 @@ symbols! {
21712171
unix,
21722172
unlikely,
21732173
unmarked_api,
2174+
unnamed_enum_variants,
21742175
unnamed_fields,
21752176
unpin,
21762177
unqualified_local_imports,

tests/ui/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,10 @@ See [Uninhabited | Reference](https://doc.rust-lang.org/reference/glossary.html?
15011501

15021502
See [Unions | Reference](https://doc.rust-lang.org/reference/items/unions.html).
15031503

1504+
## `tests/ui/unnamed-enum-variants`: `_ = <range-or-int>` in an `enum`
1505+
1506+
See [Tracking Issue for Unnamed Enum Variants (Open Enums) #156628](https://github.com/rust-lang/rust/issues/156628)
1507+
15041508
## `tests/ui/unop/`: Unary operators `-`, `*` and `!`
15051509

15061510
Tests the three unary operators for negating, dereferencing and inverting, across different contexts.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[repr(u8)]
2+
enum Foo {
3+
X = 0,
4+
_ = 1, //~ ERROR unnamed enum variants are experimental
5+
}
6+
7+
// This should not parse as an unnamed enum variant.
8+
#[cfg(false)]
9+
struct Foo {
10+
_: i32, //~ ERROR expected identifier, found reserved identifier `_`
11+
}
12+
13+
fn main() {}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error: expected identifier, found reserved identifier `_`
2+
--> $DIR/feature-gate-unnamed-enum-variants.rs:10:5
3+
|
4+
LL | struct Foo {
5+
| --- while parsing this struct
6+
LL | _: i32,
7+
| ^ expected identifier, found reserved identifier
8+
9+
error[E0658]: unnamed enum variants are experimental
10+
--> $DIR/feature-gate-unnamed-enum-variants.rs:4:5
11+
|
12+
LL | _ = 1,
13+
| ^^^^^
14+
|
15+
= note: see issue #156628 <https://github.com/rust-lang/rust/issues/156628> for more information
16+
= help: add `#![feature(unnamed_enum_variants)]` to the crate attributes to enable
17+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0658`.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@ compile-flags: --crate-type=lib
2+
#![allow(incomplete_features)]
3+
#![feature(unnamed_enum_variants)]
4+
5+
#[repr(u8)]
6+
enum Foo {
7+
_ = 1, //~ ERROR unnamed enum variants are not yet implemented
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unnamed enum variants are not yet implemented
2+
--> $DIR/unimplemented.rs:7:5
3+
|
4+
LL | _ = 1,
5+
| ^^^^^
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)