Skip to content

Commit c025962

Browse files
authoredMar 9, 2022
Rollup merge of #91804 - woppopo:const_clone, r=oli-obk
Make some `Clone` impls `const` Tracking issue: #91805 `Clone::clone_from` and some impls (Option, Result) bounded on `~const Drop`. ```rust // core::clone impl const Clone for INTEGER impl const Clone for FLOAT impl const Clone for bool impl const Clone for char impl const Clone for ! impl<T: ?Sized> const Clone for *const T impl<T: ?Sized> const Clone for *mut T impl<T: ?Sized> const Clone for &T // core::option impl<T> const Clone for Option<T> where T: ~const Clone + ~const Drop // core::result impl<T, E> const Clone for Result<T, E> where T: ~const Clone + ~const Drop, E: ~const Clone + ~const Drop, // core::convert impl const Clone for Infallible // core::ptr impl<T: ?Sized> const Clone for NonNull<T> impl<T: ?Sized> const Clone for Unique<T> ```
2 parents 10dccdc + 6620244 commit c025962

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
@@ -690,7 +690,8 @@ impl AsMut<str> for str {
690690
pub enum Infallible {}
691691

692692
#[stable(feature = "convert_infallible", since = "1.34.0")]
693-
impl Clone for Infallible {
693+
#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
694+
impl const Clone for Infallible {
694695
fn clone(&self) -> Infallible {
695696
match *self {}
696697
}

‎library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
#![feature(const_caller_location)]
105105
#![feature(const_cell_into_inner)]
106106
#![feature(const_char_convert)]
107+
#![feature(const_clone)]
107108
#![feature(const_discriminant)]
108109
#![feature(const_eval_select)]
109110
#![feature(const_float_bits_conv)]

‎library/core/src/option.rs

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

18591859
#[stable(feature = "rust1", since = "1.0.0")]
1860-
impl<T: Clone> Clone for Option<T> {
1860+
#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
1861+
impl<T> const Clone for Option<T>
1862+
where
1863+
T: ~const Clone + ~const Drop,
1864+
{
18611865
#[inline]
18621866
fn clone(&self) -> Self {
18631867
match self {

‎library/core/src/ptr/non_null.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,8 @@ impl<T> NonNull<[T]> {
642642
}
643643

644644
#[stable(feature = "nonnull", since = "1.25.0")]
645-
impl<T: ?Sized> Clone for NonNull<T> {
645+
#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
646+
impl<T: ?Sized> const Clone for NonNull<T> {
646647
#[inline]
647648
fn clone(&self) -> Self {
648649
*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
@@ -1801,7 +1801,12 @@ fn unwrap_failed<T>(_msg: &str, _error: &T) -> ! {
18011801
/////////////////////////////////////////////////////////////////////////////
18021802

18031803
#[stable(feature = "rust1", since = "1.0.0")]
1804-
impl<T: Clone, E: Clone> Clone for Result<T, E> {
1804+
#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
1805+
impl<T, E> const Clone for Result<T, E>
1806+
where
1807+
T: ~const Clone + ~const Drop,
1808+
E: ~const Clone + ~const Drop,
1809+
{
18051810
#[inline]
18061811
fn clone(&self) -> Self {
18071812
match self {

0 commit comments

Comments
 (0)
Please sign in to comment.