Skip to content

Commit 731df44

Browse files
authored
Rollup merge of rust-lang#94457 - jhpratt:stabilize-derive_default_enum, 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
2 parents e371eeb + a3dd654 commit 731df44

File tree

16 files changed

+55
-73
lines changed

16 files changed

+55
-73
lines changed

compiler/rustc_builtin_macros/src/deriving/default.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,7 @@ pub fn expand_deriving_default(
4646
StaticStruct(_, fields) => {
4747
default_struct_substructure(cx, trait_span, substr, fields)
4848
}
49-
StaticEnum(enum_def, _) => {
50-
if !cx.sess.features_untracked().derive_default_enum {
51-
rustc_session::parse::feature_err(
52-
cx.parse_sess(),
53-
sym::derive_default_enum,
54-
span,
55-
"deriving `Default` on enums is experimental",
56-
)
57-
.emit();
58-
}
59-
default_enum_substructure(cx, trait_span, enum_def)
60-
}
49+
StaticEnum(enum_def, _) => default_enum_substructure(cx, trait_span, enum_def),
6150
_ => cx.span_bug(trait_span, "method in `derive(Default)`"),
6251
}
6352
})),

compiler/rustc_feature/src/accepted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ declare_features! (
126126
(accepted, default_type_params, "1.0.0", None, None),
127127
/// Allows `#[deprecated]` attribute.
128128
(accepted, deprecated, "1.9.0", Some(29935), None),
129+
/// Allows `#[derive(Default)]` and `#[default]` on enums.
130+
(accepted, derive_default_enum, "1.62.0", Some(86985), None),
129131
/// Allows the use of destructuring assignments.
130132
(accepted, destructuring_assignment, "1.59.0", Some(71126), None),
131133
/// Allows `#[doc(alias = "...")]`.

compiler/rustc_feature/src/active.rs

-2
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,6 @@ declare_features! (
368368
(active, deprecated_safe, "1.61.0", Some(94978), None),
369369
/// Allows having using `suggestion` in the `#[deprecated]` attribute.
370370
(active, deprecated_suggestion, "1.61.0", Some(94785), None),
371-
/// Allows `#[derive(Default)]` and `#[default]` on enums.
372-
(active, derive_default_enum, "1.56.0", Some(86985), None),
373371
/// Tells rustdoc to automatically generate `#[doc(cfg(...))]`.
374372
(active, doc_auto_cfg, "1.58.0", Some(43781), None),
375373
/// Allows `#[doc(cfg(...))]`.

compiler/rustc_feature/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! even if it is stabilized or removed, *do not remove it*. Instead, move the
1212
//! symbol to the `accepted` or `removed` modules respectively.
1313
14-
#![feature(derive_default_enum)]
14+
#![cfg_attr(bootstrap, feature(derive_default_enum))]
1515
#![feature(once_cell)]
1616

1717
mod accepted;

compiler/rustc_infer/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#![feature(bool_to_option)]
1818
#![feature(box_patterns)]
1919
#![feature(control_flow_enum)]
20-
#![feature(derive_default_enum)]
20+
#![cfg_attr(bootstrap, feature(derive_default_enum))]
2121
#![feature(extend_one)]
2222
#![feature(label_break_value)]
2323
#![feature(let_chains)]

compiler/rustc_middle/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#![feature(bool_to_option)]
3131
#![feature(box_patterns)]
3232
#![feature(core_intrinsics)]
33-
#![feature(derive_default_enum)]
33+
#![cfg_attr(bootstrap, feature(derive_default_enum))]
3434
#![feature(discriminant_kind)]
3535
#![feature(exhaustive_patterns)]
3636
#![feature(get_mut_unchecked)]

compiler/rustc_session/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(crate_visibility_modifier)]
2-
#![feature(derive_default_enum)]
32
#![feature(if_let_guard)]
43
#![feature(let_chains)]
4+
#![cfg_attr(bootstrap, feature(derive_default_enum))]
55
#![feature(let_else)]
66
#![feature(min_specialization)]
77
#![feature(never_type)]

compiler/rustc_trait_selection/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#![feature(box_patterns)]
1717
#![feature(control_flow_enum)]
1818
#![feature(crate_visibility_modifier)]
19-
#![feature(derive_default_enum)]
19+
#![cfg_attr(bootstrap, feature(derive_default_enum))]
2020
#![feature(drain_filter)]
2121
#![feature(hash_drain_filter)]
2222
#![feature(label_break_value)]

library/core/src/default.rs

+17
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@
5252
/// This trait can be used with `#[derive]` if all of the type's fields implement
5353
/// `Default`. When `derive`d, it will use the default value for each field's type.
5454
///
55+
/// ### `enum`s
56+
///
57+
/// When using `#[derive(Default)]` on an `enum`, you need to choose which unit variant will be
58+
/// default. You do this by placing the `#[default]` attribute on the variant.
59+
///
60+
/// ```
61+
/// #[derive(Default)]
62+
/// enum Kind {
63+
/// #[default]
64+
/// A,
65+
/// B,
66+
/// C,
67+
/// }
68+
/// ```
69+
///
70+
/// You cannot use the `#[default]` attribute on non-unit or non-exhaustive variants.
71+
///
5572
/// ## How can I implement `Default`?
5673
///
5774
/// Provide an implementation for the `default()` method that returns the value of

library/core/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@
167167
#![feature(const_precise_live_drops)]
168168
#![feature(const_refs_to_cell)]
169169
#![feature(decl_macro)]
170-
#![feature(derive_default_enum)]
170+
#![cfg_attr(bootstrap, feature(derive_default_enum))]
171171
#![feature(deprecated_suggestion)]
172172
#![feature(doc_cfg)]
173173
#![feature(doc_notable_trait)]

src/test/ui/deriving/deriving-default-enum.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// run-pass
22

3-
#![feature(derive_default_enum)]
4-
53
// nb: does not impl Default
64
#[derive(Debug, PartialEq)]
75
struct NotDefault;

src/test/ui/deriving/deriving-with-helper.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#![feature(lang_items)]
66
#![feature(no_core)]
77
#![feature(rustc_attrs)]
8-
#![feature(derive_default_enum)]
98

109
#![no_core]
1110

src/test/ui/feature-gates/feature-gate-derive_default_enum.rs

-7
This file was deleted.

src/test/ui/feature-gates/feature-gate-derive_default_enum.stderr

-13
This file was deleted.

src/test/ui/macros/macros-nonfatal-errors.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
#![feature(trace_macros, concat_idents)]
77
#![feature(stmt_expr_attributes, arbitrary_enum_discriminant)]
8-
#![feature(derive_default_enum)]
98

109
use std::arch::asm;
1110

src/test/ui/macros/macros-nonfatal-errors.stderr

+29-29
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
11
error: the `#[default]` attribute may only be used on unit enum variants
2-
--> $DIR/macros-nonfatal-errors.rs:14:5
2+
--> $DIR/macros-nonfatal-errors.rs:13:5
33
|
44
LL | #[default]
55
| ^^^^^^^^^^
66

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

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

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

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

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

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

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

6464
error: `#[default]` attribute does not accept a value
65-
--> $DIR/macros-nonfatal-errors.rs:61:5
65+
--> $DIR/macros-nonfatal-errors.rs:60:5
6666
|
6767
LL | #[default = 1]
6868
| ^^^^^^^^^^^^^^
6969
|
7070
= help: try using `#[default]`
7171

7272
error: multiple `#[default]` attributes
73-
--> $DIR/macros-nonfatal-errors.rs:69:5
73+
--> $DIR/macros-nonfatal-errors.rs:68:5
7474
|
7575
LL | #[default]
7676
| ---------- `#[default]` used here
@@ -81,13 +81,13 @@ LL | Foo,
8181
|
8282
= note: only one `#[default]` attribute is needed
8383
help: try removing this
84-
--> $DIR/macros-nonfatal-errors.rs:68:5
84+
--> $DIR/macros-nonfatal-errors.rs:67:5
8585
|
8686
LL | #[default]
8787
| ^^^^^^^^^^
8888

8989
error: multiple `#[default]` attributes
90-
--> $DIR/macros-nonfatal-errors.rs:79:5
90+
--> $DIR/macros-nonfatal-errors.rs:78:5
9191
|
9292
LL | #[default]
9393
| ---------- `#[default]` used here
@@ -99,7 +99,7 @@ LL | Foo,
9999
|
100100
= note: only one `#[default]` attribute is needed
101101
help: try removing these
102-
--> $DIR/macros-nonfatal-errors.rs:76:5
102+
--> $DIR/macros-nonfatal-errors.rs:75:5
103103
|
104104
LL | #[default]
105105
| ^^^^^^^^^^
@@ -109,15 +109,15 @@ LL | #[default]
109109
| ^^^^^^^^^^
110110

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

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

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

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

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

147147
error: expected string literal
148-
--> $DIR/macros-nonfatal-errors.rs:105:10
148+
--> $DIR/macros-nonfatal-errors.rs:104:10
149149
|
150150
LL | env!(invalid);
151151
| ^^^^^^^
152152

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

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

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

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

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

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

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

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

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

218218
error: cannot find macro `llvm_asm` in this scope
219-
--> $DIR/macros-nonfatal-errors.rs:100:5
219+
--> $DIR/macros-nonfatal-errors.rs:99:5
220220
|
221221
LL | llvm_asm!(invalid);
222222
| ^^^^^^^^

0 commit comments

Comments
 (0)