Future-proof against loose bounds if default variant is non-exhaustive.

Co-Authored-By: Mark Rousskov <mark.simulacrum@gmail.com>
This commit is contained in:
Daniel Henry-Mantilla 2022-09-15 18:56:12 +02:00
parent 975e72fc0f
commit 3d4980bc8d
2 changed files with 32 additions and 1 deletions

View File

@ -116,3 +116,24 @@ fn main() {
trace_macros!(invalid); //~ ERROR
}
/// Check that `#[derive(Default)]` does use a `T : Default` bound when the
/// `#[default]` variant is `#[non_exhaustive]` (should this end up allowed).
const _: () = {
#[derive(Default)]
enum NonExhaustiveDefaultGeneric<T> {
#[default]
#[non_exhaustive]
Foo, //~ ERROR default variant must be exhaustive
Bar(T),
}
fn assert_impls_default<T: Default>() {}
enum NotDefault {}
// Note: the `derive(Default)` currently bails early enough for trait-checking
// not to happen. Should it bail late enough, or even pass, make sure to
// assert that the following line fails.
let _ = assert_impls_default::<NonExhaustiveDefaultGeneric<NotDefault>>;
};

View File

@ -215,11 +215,21 @@ error: trace_macros! accepts only `true` or `false`
LL | trace_macros!(invalid);
| ^^^^^^^^^^^^^^^^^^^^^^
error: default variant must be exhaustive
--> $DIR/macros-nonfatal-errors.rs:127:9
|
LL | #[non_exhaustive]
| ----------------- declared `#[non_exhaustive]` here
LL | Foo,
| ^^^
|
= help: consider a manual implementation of `Default`
error: cannot find macro `llvm_asm` in this scope
--> $DIR/macros-nonfatal-errors.rs:99:5
|
LL | llvm_asm!(invalid);
| ^^^^^^^^
error: aborting due to 27 previous errors
error: aborting due to 28 previous errors