Skip to content

Allow ~const bounds on Drop impls #92922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions compiler/rustc_typeck/src/check/dropck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,15 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
let predicate = predicate.kind();
let p = p.kind();
match (predicate.skip_binder(), p.skip_binder()) {
(ty::PredicateKind::Trait(a), ty::PredicateKind::Trait(b)) => {
relator.relate(predicate.rebind(a), p.rebind(b)).is_ok()
}
(ty::PredicateKind::Trait(a), ty::PredicateKind::Trait(b)) => relator
.relate(
predicate.rebind(ty::TraitPredicate {
constness: ty::BoundConstness::NotConst,
..a
}),
p.rebind(b),
)
.is_ok(),
(ty::PredicateKind::Projection(a), ty::PredicateKind::Projection(b)) => {
relator.relate(predicate.rebind(a), p.rebind(b)).is_ok()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
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
|
Expand Down
3 changes: 1 addition & 2 deletions src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
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
|
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/rfc-2632-const-trait-impl/const-drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ mod t {

pub struct HasConstDrop(pub ConstDrop);
pub struct TrivialFields(pub u8, pub i8, pub usize, pub isize);

pub trait SomeTrait {}
impl const SomeTrait for () {}

pub struct ConstDropWithBound<T: SomeTrait>;

impl<T: ~const SomeTrait> const Drop for ConstDropWithBound<T> {
fn drop(&mut self) {}
}
}

use t::*;
Expand All @@ -61,6 +70,7 @@ implements_const_drop! {
TrivialFields(1, 2, 3, 4),
&1,
&1 as *const i32,
ConstDropWithBound<()>,
}

fn main() {
Expand Down