Skip to content

Commit 6620244

Browse files
committed
Make some Clone impls const
1 parent b9a37ad commit 6620244

File tree

7 files changed

+33
-11
lines changed

7 files changed

+33
-11
lines changed

library/core/src/clone.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ pub trait Clone: Sized {
127127
/// allocations.
128128
#[inline]
129129
#[stable(feature = "rust1", since = "1.0.0")]
130-
fn clone_from(&mut self, source: &Self) {
130+
#[default_method_body_is_const]
131+
fn clone_from(&mut self, source: &Self)
132+
where
133+
Self: ~const Drop,
134+
{
131135
*self = source.clone()
132136
}
133137
}
@@ -178,7 +182,8 @@ mod impls {
178182
($($t:ty)*) => {
179183
$(
180184
#[stable(feature = "rust1", since = "1.0.0")]
181-
impl Clone for $t {
185+
#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
186+
impl const Clone for $t {
182187
#[inline]
183188
fn clone(&self) -> Self {
184189
*self
@@ -196,23 +201,26 @@ mod impls {
196201
}
197202

198203
#[unstable(feature = "never_type", issue = "35121")]
199-
impl Clone for ! {
204+
#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
205+
impl const Clone for ! {
200206
#[inline]
201207
fn clone(&self) -> Self {
202208
*self
203209
}
204210
}
205211

206212
#[stable(feature = "rust1", since = "1.0.0")]
207-
impl<T: ?Sized> Clone for *const T {
213+
#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
214+
impl<T: ?Sized> const Clone for *const T {
208215
#[inline]
209216
fn clone(&self) -> Self {
210217
*self
211218
}
212219
}
213220

214221
#[stable(feature = "rust1", since = "1.0.0")]
215-
impl<T: ?Sized> Clone for *mut T {
222+
#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
223+
impl<T: ?Sized> const Clone for *mut T {
216224
#[inline]
217225
fn clone(&self) -> Self {
218226
*self
@@ -221,7 +229,8 @@ mod impls {
221229

222230
/// Shared references can be cloned, but mutable references *cannot*!
223231
#[stable(feature = "rust1", since = "1.0.0")]
224-
impl<T: ?Sized> Clone for &T {
232+
#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
233+
impl<T: ?Sized> const Clone for &T {
225234
#[inline]
226235
#[rustc_diagnostic_item = "noop_method_clone"]
227236
fn clone(&self) -> Self {

library/core/src/convert/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,8 @@ impl AsMut<str> for str {
683683
pub enum Infallible {}
684684

685685
#[stable(feature = "convert_infallible", since = "1.34.0")]
686-
impl Clone for Infallible {
686+
#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
687+
impl const Clone for Infallible {
687688
fn clone(&self) -> Infallible {
688689
match *self {}
689690
}

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
#![feature(const_caller_location)]
107107
#![feature(const_cell_into_inner)]
108108
#![feature(const_char_convert)]
109+
#![feature(const_clone)]
109110
#![feature(const_discriminant)]
110111
#![feature(const_eval_select)]
111112
#![feature(const_float_bits_conv)]

library/core/src/option.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,11 @@ const fn expect_failed(msg: &str) -> ! {
16681668
/////////////////////////////////////////////////////////////////////////////
16691669

16701670
#[stable(feature = "rust1", since = "1.0.0")]
1671-
impl<T: Clone> Clone for Option<T> {
1671+
#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
1672+
impl<T> const Clone for Option<T>
1673+
where
1674+
T: ~const Clone + ~const Drop,
1675+
{
16721676
#[inline]
16731677
fn clone(&self) -> Self {
16741678
match self {

library/core/src/ptr/non_null.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,8 @@ impl<T> NonNull<[T]> {
635635
}
636636

637637
#[stable(feature = "nonnull", since = "1.25.0")]
638-
impl<T: ?Sized> Clone for NonNull<T> {
638+
#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
639+
impl<T: ?Sized> const Clone for NonNull<T> {
639640
#[inline]
640641
fn clone(&self) -> Self {
641642
*self

library/core/src/ptr/unique.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ impl<T: ?Sized> Unique<T> {
146146
}
147147

148148
#[unstable(feature = "ptr_internals", issue = "none")]
149-
impl<T: ?Sized> Clone for Unique<T> {
149+
#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
150+
impl<T: ?Sized> const Clone for Unique<T> {
150151
#[inline]
151152
fn clone(&self) -> Self {
152153
*self

library/core/src/result.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1665,7 +1665,12 @@ fn unwrap_failed(msg: &str, error: &dyn fmt::Debug) -> ! {
16651665
/////////////////////////////////////////////////////////////////////////////
16661666

16671667
#[stable(feature = "rust1", since = "1.0.0")]
1668-
impl<T: Clone, E: Clone> Clone for Result<T, E> {
1668+
#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
1669+
impl<T, E> const Clone for Result<T, E>
1670+
where
1671+
T: ~const Clone + ~const Drop,
1672+
E: ~const Clone + ~const Drop,
1673+
{
16691674
#[inline]
16701675
fn clone(&self) -> Self {
16711676
match self {

0 commit comments

Comments
 (0)