Skip to content

Commit 3d4980b

Browse files
Future-proof against loose bounds if default variant is non-exhaustive.
Co-Authored-By: Mark Rousskov <[email protected]>
1 parent 975e72f commit 3d4980b

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

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

+21
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,24 @@ fn main() {
116116

117117
trace_macros!(invalid); //~ ERROR
118118
}
119+
120+
/// Check that `#[derive(Default)]` does use a `T : Default` bound when the
121+
/// `#[default]` variant is `#[non_exhaustive]` (should this end up allowed).
122+
const _: () = {
123+
#[derive(Default)]
124+
enum NonExhaustiveDefaultGeneric<T> {
125+
#[default]
126+
#[non_exhaustive]
127+
Foo, //~ ERROR default variant must be exhaustive
128+
Bar(T),
129+
}
130+
131+
fn assert_impls_default<T: Default>() {}
132+
133+
enum NotDefault {}
134+
135+
// Note: the `derive(Default)` currently bails early enough for trait-checking
136+
// not to happen. Should it bail late enough, or even pass, make sure to
137+
// assert that the following line fails.
138+
let _ = assert_impls_default::<NonExhaustiveDefaultGeneric<NotDefault>>;
139+
};

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,21 @@ error: trace_macros! accepts only `true` or `false`
215215
LL | trace_macros!(invalid);
216216
| ^^^^^^^^^^^^^^^^^^^^^^
217217

218+
error: default variant must be exhaustive
219+
--> $DIR/macros-nonfatal-errors.rs:127:9
220+
|
221+
LL | #[non_exhaustive]
222+
| ----------------- declared `#[non_exhaustive]` here
223+
LL | Foo,
224+
| ^^^
225+
|
226+
= help: consider a manual implementation of `Default`
227+
218228
error: cannot find macro `llvm_asm` in this scope
219229
--> $DIR/macros-nonfatal-errors.rs:99:5
220230
|
221231
LL | llvm_asm!(invalid);
222232
| ^^^^^^^^
223233

224-
error: aborting due to 27 previous errors
234+
error: aborting due to 28 previous errors
225235

0 commit comments

Comments
 (0)