diff options
| author | bors <bors@rust-lang.org> | 2022-01-24 08:05:37 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-01-24 08:05:37 +0000 |
| commit | ef119d704d87a05435ea97ef4161529142313a9b (patch) | |
| tree | 79f04c5b12b95c3b6bab52e108640bf8d131e12b /src/test | |
| parent | 42313dd29b3edb0ab453a0d43d12876ec7e48ce0 (diff) | |
| parent | b7e443397471062e5681ecefb6638577cefb9571 (diff) | |
Auto merge of #93028 - compiler-errors:const_drop_bounds, r=fee1-dead
Check `const Drop` impls considering `~const` Bounds This PR adds logic to trait selection to account for `~const` bounds in custom `impl const Drop` for types, elaborates the `const Drop` check in `rustc_const_eval` to check those bounds, and steals some drop linting fixes from #92922, thanks `@DrMeepster.` r? `@fee1-dead` `@oli-obk` <sup>(edit: guess I can't request review from two people, lol)</sup> since each of you wrote and reviewed #88558, respectively. Since the logic here is more complicated than what existed, it's possible that this is a perf regression. But it works correctly with tests, and that makes me happy. Fixes #92881
Diffstat (limited to 'src/test')
4 files changed, 82 insertions, 67 deletions
diff --git a/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.precise.stderr b/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.precise.stderr index 04c21101e75..721636e0743 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.precise.stderr +++ b/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.precise.stderr @@ -1,13 +1,5 @@ -error: `~const` is not allowed here - --> $DIR/const-drop-fail.rs:27:35 - | -LL | struct ConstDropImplWithBounds<T: ~const A>(PhantomData<T>); - | ^^^^^^^^ - | - = note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions - error[E0277]: the trait bound `NonTrivialDrop: Drop` is not satisfied - --> $DIR/const-drop-fail.rs:45:5 + --> $DIR/const-drop-fail.rs:44:5 | LL | const _: () = check($exp); | ----- required by a bound introduced by this call @@ -16,50 +8,51 @@ LL | NonTrivialDrop, | ^^^^^^^^^^^^^^ the trait `Drop` is not implemented for `NonTrivialDrop` | note: required by a bound in `check` - --> $DIR/const-drop-fail.rs:36:19 + --> $DIR/const-drop-fail.rs:35:19 | LL | const fn check<T: ~const Drop>(_: T) {} | ^^^^^^^^^^^ required by this bound in `check` -error[E0277]: the trait bound `ConstImplWithDropGlue: Drop` is not satisfied - --> $DIR/const-drop-fail.rs:47:5 +error[E0277]: the trait bound `NonTrivialDrop: Drop` is not satisfied in `ConstImplWithDropGlue` + --> $DIR/const-drop-fail.rs:46:5 | LL | const _: () = check($exp); | ----- required by a bound introduced by this call ... LL | ConstImplWithDropGlue(NonTrivialDrop), - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Drop` is not implemented for `ConstImplWithDropGlue` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ within `ConstImplWithDropGlue`, the trait `Drop` is not implemented for `NonTrivialDrop` | +note: required because it appears within the type `ConstImplWithDropGlue` + --> $DIR/const-drop-fail.rs:17:8 + | +LL | struct ConstImplWithDropGlue(NonTrivialDrop); + | ^^^^^^^^^^^^^^^^^^^^^ note: required by a bound in `check` - --> $DIR/const-drop-fail.rs:36:19 + --> $DIR/const-drop-fail.rs:35:19 | LL | const fn check<T: ~const Drop>(_: T) {} | ^^^^^^^^^^^ required by this bound in `check` error[E0277]: the trait bound `NonTrivialDrop: A` is not satisfied - --> $DIR/const-drop-fail.rs:49:5 - | -LL | ConstDropImplWithBounds::<NonTrivialDrop>(PhantomData), - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `A` is not implemented for `NonTrivialDrop` - | -note: required by a bound in `ConstDropImplWithBounds` - --> $DIR/const-drop-fail.rs:27:35 - | -LL | struct ConstDropImplWithBounds<T: ~const A>(PhantomData<T>); - | ^^^^^^^^ required by this bound in `ConstDropImplWithBounds` - -error[E0277]: the trait bound `NonTrivialDrop: A` is not satisfied - --> $DIR/const-drop-fail.rs:49:5 + --> $DIR/const-drop-fail.rs:48:5 | +LL | const _: () = check($exp); + | ----- required by a bound introduced by this call +... LL | ConstDropImplWithBounds::<NonTrivialDrop>(PhantomData), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `A` is not implemented for `NonTrivialDrop` | -note: required by a bound in `ConstDropImplWithBounds` - --> $DIR/const-drop-fail.rs:27:35 +note: required because of the requirements on the impl of `Drop` for `ConstDropImplWithBounds<NonTrivialDrop>` + --> $DIR/const-drop-fail.rs:29:25 | -LL | struct ConstDropImplWithBounds<T: ~const A>(PhantomData<T>); - | ^^^^^^^^ required by this bound in `ConstDropImplWithBounds` +LL | impl<T: ~const A> const Drop for ConstDropImplWithBounds<T> { + | ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: required by a bound in `check` + --> $DIR/const-drop-fail.rs:35:19 + | +LL | const fn check<T: ~const Drop>(_: T) {} + | ^^^^^^^^^^^ required by this bound in `check` -error: aborting due to 5 previous errors +error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.rs b/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.rs index 3d4de088f55..4622723c189 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.rs +++ b/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.rs @@ -24,8 +24,7 @@ trait A { fn a() { println!("A"); } } impl A for NonTrivialDrop {} -struct ConstDropImplWithBounds<T: ~const A>(PhantomData<T>); -//~^ ERROR `~const` is not allowed +struct ConstDropImplWithBounds<T: A>(PhantomData<T>); impl<T: ~const A> const Drop for ConstDropImplWithBounds<T> { fn drop(&mut self) { @@ -48,7 +47,6 @@ check_all! { //~^ ERROR the trait bound ConstDropImplWithBounds::<NonTrivialDrop>(PhantomData), //~^ ERROR the trait bound - //~| ERROR the trait bound } fn main() {} diff --git a/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.stock.stderr b/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.stock.stderr index 04c21101e75..721636e0743 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.stock.stderr +++ b/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.stock.stderr @@ -1,13 +1,5 @@ -error: `~const` is not allowed here - --> $DIR/const-drop-fail.rs:27:35 - | -LL | struct ConstDropImplWithBounds<T: ~const A>(PhantomData<T>); - | ^^^^^^^^ - | - = note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions - error[E0277]: the trait bound `NonTrivialDrop: Drop` is not satisfied - --> $DIR/const-drop-fail.rs:45:5 + --> $DIR/const-drop-fail.rs:44:5 | LL | const _: () = check($exp); | ----- required by a bound introduced by this call @@ -16,50 +8,51 @@ LL | NonTrivialDrop, | ^^^^^^^^^^^^^^ the trait `Drop` is not implemented for `NonTrivialDrop` | note: required by a bound in `check` - --> $DIR/const-drop-fail.rs:36:19 + --> $DIR/const-drop-fail.rs:35:19 | LL | const fn check<T: ~const Drop>(_: T) {} | ^^^^^^^^^^^ required by this bound in `check` -error[E0277]: the trait bound `ConstImplWithDropGlue: Drop` is not satisfied - --> $DIR/const-drop-fail.rs:47:5 +error[E0277]: the trait bound `NonTrivialDrop: Drop` is not satisfied in `ConstImplWithDropGlue` + --> $DIR/const-drop-fail.rs:46:5 | LL | const _: () = check($exp); | ----- required by a bound introduced by this call ... LL | ConstImplWithDropGlue(NonTrivialDrop), - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Drop` is not implemented for `ConstImplWithDropGlue` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ within `ConstImplWithDropGlue`, the trait `Drop` is not implemented for `NonTrivialDrop` | +note: required because it appears within the type `ConstImplWithDropGlue` + --> $DIR/const-drop-fail.rs:17:8 + | +LL | struct ConstImplWithDropGlue(NonTrivialDrop); + | ^^^^^^^^^^^^^^^^^^^^^ note: required by a bound in `check` - --> $DIR/const-drop-fail.rs:36:19 + --> $DIR/const-drop-fail.rs:35:19 | LL | const fn check<T: ~const Drop>(_: T) {} | ^^^^^^^^^^^ required by this bound in `check` error[E0277]: the trait bound `NonTrivialDrop: A` is not satisfied - --> $DIR/const-drop-fail.rs:49:5 - | -LL | ConstDropImplWithBounds::<NonTrivialDrop>(PhantomData), - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `A` is not implemented for `NonTrivialDrop` - | -note: required by a bound in `ConstDropImplWithBounds` - --> $DIR/const-drop-fail.rs:27:35 - | -LL | struct ConstDropImplWithBounds<T: ~const A>(PhantomData<T>); - | ^^^^^^^^ required by this bound in `ConstDropImplWithBounds` - -error[E0277]: the trait bound `NonTrivialDrop: A` is not satisfied - --> $DIR/const-drop-fail.rs:49:5 + --> $DIR/const-drop-fail.rs:48:5 | +LL | const _: () = check($exp); + | ----- required by a bound introduced by this call +... LL | ConstDropImplWithBounds::<NonTrivialDrop>(PhantomData), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `A` is not implemented for `NonTrivialDrop` | -note: required by a bound in `ConstDropImplWithBounds` - --> $DIR/const-drop-fail.rs:27:35 +note: required because of the requirements on the impl of `Drop` for `ConstDropImplWithBounds<NonTrivialDrop>` + --> $DIR/const-drop-fail.rs:29:25 | -LL | struct ConstDropImplWithBounds<T: ~const A>(PhantomData<T>); - | ^^^^^^^^ required by this bound in `ConstDropImplWithBounds` +LL | impl<T: ~const A> const Drop for ConstDropImplWithBounds<T> { + | ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: required by a bound in `check` + --> $DIR/const-drop-fail.rs:35:19 + | +LL | const fn check<T: ~const Drop>(_: T) {} + | ^^^^^^^^^^^ required by this bound in `check` -error: aborting due to 5 previous errors +error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/rfc-2632-const-trait-impl/const-drop.rs b/src/test/ui/rfc-2632-const-trait-impl/const-drop.rs index 292017a1de2..13363c506d5 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/const-drop.rs +++ b/src/test/ui/rfc-2632-const-trait-impl/const-drop.rs @@ -3,6 +3,7 @@ #![feature(const_trait_impl)] #![feature(const_fn_trait_bound)] #![feature(const_mut_refs)] +#![feature(never_type)] #![cfg_attr(precise, feature(const_precise_live_drops))] struct S<'a>(&'a mut u8); @@ -45,6 +46,33 @@ mod t { pub struct HasConstDrop(pub ConstDrop); pub struct TrivialFields(pub u8, pub i8, pub usize, pub isize); + + pub trait SomeTrait { + fn foo(); + } + impl const SomeTrait for () { + fn foo() {} + } + // non-const impl + impl SomeTrait for i32 { + fn foo() {} + } + + pub struct ConstDropWithBound<T: SomeTrait>(pub core::marker::PhantomData<T>); + + impl<T: ~const SomeTrait> const Drop for ConstDropWithBound<T> { + fn drop(&mut self) { + T::foo(); + } + } + + pub struct ConstDropWithNonconstBound<T: SomeTrait>(pub core::marker::PhantomData<T>); + + impl<T: SomeTrait> const Drop for ConstDropWithNonconstBound<T> { + fn drop(&mut self) { + // Note: we DON'T use the `T: SomeTrait` bound + } + } } use t::*; @@ -61,6 +89,9 @@ implements_const_drop! { TrivialFields(1, 2, 3, 4), &1, &1 as *const i32, + ConstDropWithBound::<()>, + ConstDropWithNonconstBound::<i32>, + Result::<i32, !>::Ok(1), } fn main() { |
