-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Elaborate destruct host effect clauses with structurally implied clauses #144856
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2147,9 +2147,7 @@ const fn expect_failed(msg: &str) -> ! { | |
#[rustc_const_unstable(feature = "const_try", issue = "74935")] | ||
impl<T> const Clone for Option<T> | ||
where | ||
// FIXME(const_hack): the T: ~const Destruct should be inferred from the Self: ~const Destruct in clone_from. | ||
// See https://github.com/rust-lang/rust/issues/144207 | ||
T: ~const Clone + ~const Destruct, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused how a bound on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this might be okay since this bound was on |
||
T: ~const Clone, | ||
{ | ||
#[inline] | ||
fn clone(&self) -> Self { | ||
|
@@ -2160,7 +2158,10 @@ where | |
} | ||
|
||
#[inline] | ||
fn clone_from(&mut self, source: &Self) { | ||
fn clone_from(&mut self, source: &Self) | ||
where | ||
Self: ~const Destruct, | ||
{ | ||
match (self, source) { | ||
(Some(to), Some(from)) => to.clone_from(from), | ||
(to, from) => *to = from.clone(), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Demonstrates that `impl<T> const Clone for Option<T>` does not require const_hack bounds. | ||
// See issue #144207. | ||
//@ revisions: next old | ||
//@ [next] compile-flags: -Znext-solver | ||
//@ check-pass | ||
|
||
#![feature(const_trait_impl, const_destruct)] | ||
|
||
use std::marker::Destruct; | ||
|
||
#[const_trait] | ||
pub trait CloneLike: Sized { | ||
fn clone(&self) -> Self; | ||
|
||
fn clone_from(&mut self, source: &Self) | ||
where | ||
Self: [const] Destruct, | ||
{ | ||
*self = source.clone() | ||
} | ||
} | ||
|
||
enum OptionLike<T> { | ||
None, | ||
Some(T), | ||
} | ||
|
||
impl<T> const CloneLike for OptionLike<T> | ||
where | ||
T: [const] CloneLike, | ||
{ | ||
fn clone(&self) -> Self { | ||
match self { | ||
Self::Some(x) => Self::Some(x.clone()), | ||
Self::None => Self::None, | ||
} | ||
} | ||
|
||
fn clone_from(&mut self, source: &Self) | ||
where | ||
Self: [const] Destruct, | ||
{ | ||
match (self, source) { | ||
(Self::Some(to), Self::Some(from)) => to.clone_from(from), | ||
(to, from) => *to = from.clone(), | ||
} | ||
} | ||
} | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//@ revisions: next old | ||
//@ [next] compile-flags: -Znext-solver | ||
//@ check-pass | ||
|
||
#![feature(const_trait_impl, const_destruct)] | ||
|
||
use std::marker::Destruct; | ||
|
||
const fn ensure_const_destruct<T: [const] Destruct>(_t: T) {} | ||
|
||
enum Either<T, U> { | ||
Left(T), | ||
Right(U), | ||
} | ||
|
||
struct Foo<T, U, V>(Either<T, U>, Option<V>); | ||
|
||
struct Bar<T, U, V>(T, Result<U, V>); | ||
|
||
const fn f<T>(x: T) | ||
where | ||
Option<T>: [const] Destruct, | ||
{ | ||
ensure_const_destruct(x); | ||
} | ||
|
||
const fn g<T, U>(x: Result<T, U>) | ||
where | ||
Either<T, U>: [const] Destruct, | ||
{ | ||
ensure_const_destruct(x); | ||
} | ||
|
||
const fn h<T, U, V>(x: Foo<T, U, V>) | ||
where | ||
Bar<T, U, V>: [const] Destruct, | ||
{ | ||
ensure_const_destruct(x); | ||
} | ||
|
||
fn main() {} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we did apply this, I'd probably want to abstract out the function that maps from a ty to its constituent types and share it between the
Destruct
trait logic and here.(T,): Destruct
should probably implyT: Destruct
, for example.And
[Ty; N]: Destruct
should probably implyTy: Destruct
(perhaps except for 0, but that's another problem with the destruct trait...).Logic would probably need to be pulled out of https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_next_trait_solver/solve/assembly/structural_traits.rs.html#743-820
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, absolutely I should do so and fill up missing cases for arrays and tuples. Thanks!
And it seems that I should also check for whether
ManuallyDrop<T>: Destruct
bound accidentally implies the boundT: Destruct
as well 😅