Skip to content

Commit

Permalink
Rollup merge of rust-lang#94457 - jhpratt:stabilize-derive_default_en…
Browse files Browse the repository at this point in the history
…um, r=davidtwco

Stabilize `derive_default_enum`

This stabilizes `#![feature(derive_default_enum)]`, as proposed in [RFC 3107](rust-lang/rfcs#3107) and tracked in rust-lang#87517. In short, it permits you to `#[derive(Default)]` on `enum`s, indicating what the default should be by placing a `#[default]` attribute on the desired variant (which must be a unit variant in the interest of forward compatibility).

`@rustbot` label +S-waiting-on-review +T-lang
  • Loading branch information
matthiaskrgr authored Apr 14, 2022
2 parents f9d4d12 + a3dd654 commit f1e6a6d
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 73 deletions.
13 changes: 1 addition & 12 deletions compiler/rustc_builtin_macros/src/deriving/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,7 @@ pub fn expand_deriving_default(
StaticStruct(_, fields) => {
default_struct_substructure(cx, trait_span, substr, fields)
}
StaticEnum(enum_def, _) => {
if !cx.sess.features_untracked().derive_default_enum {
rustc_session::parse::feature_err(
cx.parse_sess(),
sym::derive_default_enum,
span,
"deriving `Default` on enums is experimental",
)
.emit();
}
default_enum_substructure(cx, trait_span, enum_def)
}
StaticEnum(enum_def, _) => default_enum_substructure(cx, trait_span, enum_def),
_ => cx.span_bug(trait_span, "method in `derive(Default)`"),
}
})),
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/accepted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ declare_features! (
(accepted, default_type_params, "1.0.0", None, None),
/// Allows `#[deprecated]` attribute.
(accepted, deprecated, "1.9.0", Some(29935), None),
/// Allows `#[derive(Default)]` and `#[default]` on enums.
(accepted, derive_default_enum, "1.62.0", Some(86985), None),
/// Allows the use of destructuring assignments.
(accepted, destructuring_assignment, "1.59.0", Some(71126), None),
/// Allows `#[doc(alias = "...")]`.
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,6 @@ declare_features! (
(active, deprecated_safe, "1.61.0", Some(94978), None),
/// Allows having using `suggestion` in the `#[deprecated]` attribute.
(active, deprecated_suggestion, "1.61.0", Some(94785), None),
/// Allows `#[derive(Default)]` and `#[default]` on enums.
(active, derive_default_enum, "1.56.0", Some(86985), None),
/// Tells rustdoc to automatically generate `#[doc(cfg(...))]`.
(active, doc_auto_cfg, "1.58.0", Some(43781), None),
/// Allows `#[doc(cfg(...))]`.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_feature/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! even if it is stabilized or removed, *do not remove it*. Instead, move the
//! symbol to the `accepted` or `removed` modules respectively.

#![feature(derive_default_enum)]
#![cfg_attr(bootstrap, feature(derive_default_enum))]
#![feature(once_cell)]

mod accepted;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#![feature(bool_to_option)]
#![feature(box_patterns)]
#![feature(control_flow_enum)]
#![feature(derive_default_enum)]
#![cfg_attr(bootstrap, feature(derive_default_enum))]
#![feature(extend_one)]
#![feature(label_break_value)]
#![feature(let_chains)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#![feature(bool_to_option)]
#![feature(box_patterns)]
#![feature(core_intrinsics)]
#![feature(derive_default_enum)]
#![cfg_attr(bootstrap, feature(derive_default_enum))]
#![feature(discriminant_kind)]
#![feature(exhaustive_patterns)]
#![feature(get_mut_unchecked)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_session/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![feature(crate_visibility_modifier)]
#![feature(derive_default_enum)]
#![feature(if_let_guard)]
#![feature(let_chains)]
#![cfg_attr(bootstrap, feature(derive_default_enum))]
#![feature(let_else)]
#![feature(min_specialization)]
#![feature(never_type)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#![feature(box_patterns)]
#![feature(control_flow_enum)]
#![feature(crate_visibility_modifier)]
#![feature(derive_default_enum)]
#![cfg_attr(bootstrap, feature(derive_default_enum))]
#![feature(drain_filter)]
#![feature(hash_drain_filter)]
#![feature(label_break_value)]
Expand Down
17 changes: 17 additions & 0 deletions library/core/src/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@
/// This trait can be used with `#[derive]` if all of the type's fields implement
/// `Default`. When `derive`d, it will use the default value for each field's type.
///
/// ### `enum`s
///
/// When using `#[derive(Default)]` on an `enum`, you need to choose which unit variant will be
/// default. You do this by placing the `#[default]` attribute on the variant.
///
/// ```
/// #[derive(Default)]
/// enum Kind {
/// #[default]
/// A,
/// B,
/// C,
/// }
/// ```
///
/// You cannot use the `#[default]` attribute on non-unit or non-exhaustive variants.
///
/// ## How can I implement `Default`?
///
/// Provide an implementation for the `default()` method that returns the value of
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
#![feature(const_precise_live_drops)]
#![feature(const_refs_to_cell)]
#![feature(decl_macro)]
#![feature(derive_default_enum)]
#![cfg_attr(bootstrap, feature(derive_default_enum))]
#![feature(deprecated_suggestion)]
#![feature(doc_cfg)]
#![feature(doc_notable_trait)]
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/deriving/deriving-default-enum.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// run-pass

#![feature(derive_default_enum)]

// nb: does not impl Default
#[derive(Debug, PartialEq)]
struct NotDefault;
Expand Down
1 change: 0 additions & 1 deletion src/test/ui/deriving/deriving-with-helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#![feature(lang_items)]
#![feature(no_core)]
#![feature(rustc_attrs)]
#![feature(derive_default_enum)]

#![no_core]

Expand Down
7 changes: 0 additions & 7 deletions src/test/ui/feature-gates/feature-gate-derive_default_enum.rs

This file was deleted.

13 changes: 0 additions & 13 deletions src/test/ui/feature-gates/feature-gate-derive_default_enum.stderr

This file was deleted.

1 change: 0 additions & 1 deletion src/test/ui/macros/macros-nonfatal-errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#![feature(trace_macros, concat_idents)]
#![feature(stmt_expr_attributes, arbitrary_enum_discriminant)]
#![feature(derive_default_enum)]

use std::arch::asm;

Expand Down
58 changes: 29 additions & 29 deletions src/test/ui/macros/macros-nonfatal-errors.stderr
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
error: the `#[default]` attribute may only be used on unit enum variants
--> $DIR/macros-nonfatal-errors.rs:14:5
--> $DIR/macros-nonfatal-errors.rs:13:5
|
LL | #[default]
| ^^^^^^^^^^

error: the `#[default]` attribute may only be used on unit enum variants
--> $DIR/macros-nonfatal-errors.rs:19:36
--> $DIR/macros-nonfatal-errors.rs:18:36
|
LL | struct DefaultInnerAttrTupleStruct(#[default] ());
| ^^^^^^^^^^

error: the `#[default]` attribute may only be used on unit enum variants
--> $DIR/macros-nonfatal-errors.rs:23:1
--> $DIR/macros-nonfatal-errors.rs:22:1
|
LL | #[default]
| ^^^^^^^^^^

error: the `#[default]` attribute may only be used on unit enum variants
--> $DIR/macros-nonfatal-errors.rs:27:1
--> $DIR/macros-nonfatal-errors.rs:26:1
|
LL | #[default]
| ^^^^^^^^^^

error: the `#[default]` attribute may only be used on unit enum variants
--> $DIR/macros-nonfatal-errors.rs:37:11
--> $DIR/macros-nonfatal-errors.rs:36:11
|
LL | Foo = #[default] 0,
| ^^^^^^^^^^

error: the `#[default]` attribute may only be used on unit enum variants
--> $DIR/macros-nonfatal-errors.rs:38:14
--> $DIR/macros-nonfatal-errors.rs:37:14
|
LL | Bar([u8; #[default] 1]),
| ^^^^^^^^^^

error: no default declared
--> $DIR/macros-nonfatal-errors.rs:43:10
--> $DIR/macros-nonfatal-errors.rs:42:10
|
LL | #[derive(Default)]
| ^^^^^^^
Expand All @@ -44,7 +44,7 @@ LL | #[derive(Default)]
= note: this error originates in the derive macro `Default` (in Nightly builds, run with -Z macro-backtrace for more info)

error: multiple declared defaults
--> $DIR/macros-nonfatal-errors.rs:49:10
--> $DIR/macros-nonfatal-errors.rs:48:10
|
LL | #[derive(Default)]
| ^^^^^^^
Expand All @@ -62,15 +62,15 @@ LL | Baz,
= note: this error originates in the derive macro `Default` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `#[default]` attribute does not accept a value
--> $DIR/macros-nonfatal-errors.rs:61:5
--> $DIR/macros-nonfatal-errors.rs:60:5
|
LL | #[default = 1]
| ^^^^^^^^^^^^^^
|
= help: try using `#[default]`

error: multiple `#[default]` attributes
--> $DIR/macros-nonfatal-errors.rs:69:5
--> $DIR/macros-nonfatal-errors.rs:68:5
|
LL | #[default]
| ---------- `#[default]` used here
Expand All @@ -81,13 +81,13 @@ LL | Foo,
|
= note: only one `#[default]` attribute is needed
help: try removing this
--> $DIR/macros-nonfatal-errors.rs:68:5
--> $DIR/macros-nonfatal-errors.rs:67:5
|
LL | #[default]
| ^^^^^^^^^^

error: multiple `#[default]` attributes
--> $DIR/macros-nonfatal-errors.rs:79:5
--> $DIR/macros-nonfatal-errors.rs:78:5
|
LL | #[default]
| ---------- `#[default]` used here
Expand All @@ -99,7 +99,7 @@ LL | Foo,
|
= note: only one `#[default]` attribute is needed
help: try removing these
--> $DIR/macros-nonfatal-errors.rs:76:5
--> $DIR/macros-nonfatal-errors.rs:75:5
|
LL | #[default]
| ^^^^^^^^^^
Expand All @@ -109,15 +109,15 @@ LL | #[default]
| ^^^^^^^^^^

error: the `#[default]` attribute may only be used on unit enum variants
--> $DIR/macros-nonfatal-errors.rs:86:5
--> $DIR/macros-nonfatal-errors.rs:85:5
|
LL | Foo {},
| ^^^
|
= help: consider a manual implementation of `Default`

error: default variant must be exhaustive
--> $DIR/macros-nonfatal-errors.rs:94:5
--> $DIR/macros-nonfatal-errors.rs:93:5
|
LL | #[non_exhaustive]
| ----------------- declared `#[non_exhaustive]` here
Expand All @@ -127,45 +127,45 @@ LL | Foo,
= help: consider a manual implementation of `Default`

error: asm template must be a string literal
--> $DIR/macros-nonfatal-errors.rs:99:10
--> $DIR/macros-nonfatal-errors.rs:98:10
|
LL | asm!(invalid);
| ^^^^^^^

error: concat_idents! requires ident args
--> $DIR/macros-nonfatal-errors.rs:102:5
--> $DIR/macros-nonfatal-errors.rs:101:5
|
LL | concat_idents!("not", "idents");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: argument must be a string literal
--> $DIR/macros-nonfatal-errors.rs:104:17
--> $DIR/macros-nonfatal-errors.rs:103:17
|
LL | option_env!(invalid);
| ^^^^^^^

error: expected string literal
--> $DIR/macros-nonfatal-errors.rs:105:10
--> $DIR/macros-nonfatal-errors.rs:104:10
|
LL | env!(invalid);
| ^^^^^^^

error: expected string literal
--> $DIR/macros-nonfatal-errors.rs:106:10
--> $DIR/macros-nonfatal-errors.rs:105:10
|
LL | env!(foo, abr, baz);
| ^^^

error: environment variable `RUST_HOPEFULLY_THIS_DOESNT_EXIST` not defined
--> $DIR/macros-nonfatal-errors.rs:107:5
--> $DIR/macros-nonfatal-errors.rs:106:5
|
LL | env!("RUST_HOPEFULLY_THIS_DOESNT_EXIST");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)

error: format argument must be a string literal
--> $DIR/macros-nonfatal-errors.rs:109:13
--> $DIR/macros-nonfatal-errors.rs:108:13
|
LL | format!(invalid);
| ^^^^^^^
Expand All @@ -176,47 +176,47 @@ LL | format!("{}", invalid);
| +++++

error: argument must be a string literal
--> $DIR/macros-nonfatal-errors.rs:111:14
--> $DIR/macros-nonfatal-errors.rs:110:14
|
LL | include!(invalid);
| ^^^^^^^

error: argument must be a string literal
--> $DIR/macros-nonfatal-errors.rs:113:18
--> $DIR/macros-nonfatal-errors.rs:112:18
|
LL | include_str!(invalid);
| ^^^^^^^

error: couldn't read $DIR/i'd be quite surprised if a file with this name existed: $FILE_NOT_FOUND_MSG (os error 2)
--> $DIR/macros-nonfatal-errors.rs:114:5
--> $DIR/macros-nonfatal-errors.rs:113:5
|
LL | include_str!("i'd be quite surprised if a file with this name existed");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `include_str` (in Nightly builds, run with -Z macro-backtrace for more info)

error: argument must be a string literal
--> $DIR/macros-nonfatal-errors.rs:115:20
--> $DIR/macros-nonfatal-errors.rs:114:20
|
LL | include_bytes!(invalid);
| ^^^^^^^

error: couldn't read $DIR/i'd be quite surprised if a file with this name existed: $FILE_NOT_FOUND_MSG (os error 2)
--> $DIR/macros-nonfatal-errors.rs:116:5
--> $DIR/macros-nonfatal-errors.rs:115:5
|
LL | include_bytes!("i'd be quite surprised if a file with this name existed");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `include_bytes` (in Nightly builds, run with -Z macro-backtrace for more info)

error: trace_macros! accepts only `true` or `false`
--> $DIR/macros-nonfatal-errors.rs:118:5
--> $DIR/macros-nonfatal-errors.rs:117:5
|
LL | trace_macros!(invalid);
| ^^^^^^^^^^^^^^^^^^^^^^

error: cannot find macro `llvm_asm` in this scope
--> $DIR/macros-nonfatal-errors.rs:100:5
--> $DIR/macros-nonfatal-errors.rs:99:5
|
LL | llvm_asm!(invalid);
| ^^^^^^^^
Expand Down

0 comments on commit f1e6a6d

Please sign in to comment.