Skip to content

Commit c4b721d

Browse files
committed
Remove const hack from impl Clone for Option
1 parent 48c2570 commit c4b721d

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

library/core/src/option.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,9 +2147,7 @@ const fn expect_failed(msg: &str) -> ! {
21472147
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
21482148
impl<T> const Clone for Option<T>
21492149
where
2150-
// FIXME(const_hack): the T: ~const Destruct should be inferred from the Self: ~const Destruct in clone_from.
2151-
// See https://github.com/rust-lang/rust/issues/144207
2152-
T: ~const Clone + ~const Destruct,
2150+
T: ~const Clone,
21532151
{
21542152
#[inline]
21552153
fn clone(&self) -> Self {
@@ -2160,7 +2158,10 @@ where
21602158
}
21612159

21622160
#[inline]
2163-
fn clone_from(&mut self, source: &Self) {
2161+
fn clone_from(&mut self, source: &Self)
2162+
where
2163+
Self: ~const Destruct,
2164+
{
21642165
match (self, source) {
21652166
(Some(to), Some(from)) => to.clone_from(from),
21662167
(to, from) => *to = from.clone(),
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Demonstrates that `impl<T> const Clone for Option<T>` does not require const_hack bounds.
2+
// See issue #144207.
3+
//@ revisions: next old
4+
//@ [next] compile-flags: -Znext-solver
5+
//@ check-pass
6+
7+
#![feature(const_trait_impl, const_destruct)]
8+
9+
use std::marker::Destruct;
10+
11+
#[const_trait]
12+
pub trait CloneLike: Sized {
13+
fn clone(&self) -> Self;
14+
15+
fn clone_from(&mut self, source: &Self)
16+
where
17+
Self: [const] Destruct,
18+
{
19+
*self = source.clone()
20+
}
21+
}
22+
23+
enum OptionLike<T> {
24+
None,
25+
Some(T),
26+
}
27+
28+
impl<T> const CloneLike for OptionLike<T>
29+
where
30+
T: [const] CloneLike,
31+
{
32+
fn clone(&self) -> Self {
33+
match self {
34+
Self::Some(x) => Self::Some(x.clone()),
35+
Self::None => Self::None,
36+
}
37+
}
38+
39+
fn clone_from(&mut self, source: &Self)
40+
where
41+
Self: [const] Destruct,
42+
{
43+
match (self, source) {
44+
(Self::Some(to), Self::Some(from)) => to.clone_from(from),
45+
(to, from) => *to = from.clone(),
46+
}
47+
}
48+
}
49+
50+
fn main() {}

0 commit comments

Comments
 (0)