From 662024478deb04a8c71d779b66b4bedb0bf91dbe Mon Sep 17 00:00:00 2001 From: woppopo Date: Sun, 12 Dec 2021 04:19:23 +0900 Subject: [PATCH 1/1] Make some `Clone` impls `const` --- library/core/src/clone.rs | 21 +++++++++++++++------ library/core/src/convert/mod.rs | 3 ++- library/core/src/lib.rs | 1 + library/core/src/option.rs | 6 +++++- library/core/src/ptr/non_null.rs | 3 ++- library/core/src/ptr/unique.rs | 3 ++- library/core/src/result.rs | 7 ++++++- 7 files changed, 33 insertions(+), 11 deletions(-) diff --git a/library/core/src/clone.rs b/library/core/src/clone.rs index 6f9579043c3..1912694412b 100644 --- a/library/core/src/clone.rs +++ b/library/core/src/clone.rs @@ -127,7 +127,11 @@ pub trait Clone: Sized { /// allocations. #[inline] #[stable(feature = "rust1", since = "1.0.0")] - fn clone_from(&mut self, source: &Self) { + #[default_method_body_is_const] + fn clone_from(&mut self, source: &Self) + where + Self: ~const Drop, + { *self = source.clone() } } @@ -178,7 +182,8 @@ macro_rules! impl_clone { ($($t:ty)*) => { $( #[stable(feature = "rust1", since = "1.0.0")] - impl Clone for $t { + #[rustc_const_unstable(feature = "const_clone", issue = "91805")] + impl const Clone for $t { #[inline] fn clone(&self) -> Self { *self @@ -196,7 +201,8 @@ fn clone(&self) -> Self { } #[unstable(feature = "never_type", issue = "35121")] - impl Clone for ! { + #[rustc_const_unstable(feature = "const_clone", issue = "91805")] + impl const Clone for ! { #[inline] fn clone(&self) -> Self { *self @@ -204,7 +210,8 @@ fn clone(&self) -> Self { } #[stable(feature = "rust1", since = "1.0.0")] - impl Clone for *const T { + #[rustc_const_unstable(feature = "const_clone", issue = "91805")] + impl const Clone for *const T { #[inline] fn clone(&self) -> Self { *self @@ -212,7 +219,8 @@ fn clone(&self) -> Self { } #[stable(feature = "rust1", since = "1.0.0")] - impl Clone for *mut T { + #[rustc_const_unstable(feature = "const_clone", issue = "91805")] + impl const Clone for *mut T { #[inline] fn clone(&self) -> Self { *self @@ -221,7 +229,8 @@ fn clone(&self) -> Self { /// Shared references can be cloned, but mutable references *cannot*! #[stable(feature = "rust1", since = "1.0.0")] - impl Clone for &T { + #[rustc_const_unstable(feature = "const_clone", issue = "91805")] + impl const Clone for &T { #[inline] #[rustc_diagnostic_item = "noop_method_clone"] fn clone(&self) -> Self { diff --git a/library/core/src/convert/mod.rs b/library/core/src/convert/mod.rs index 5aa53deee34..a50b8904b59 100644 --- a/library/core/src/convert/mod.rs +++ b/library/core/src/convert/mod.rs @@ -683,7 +683,8 @@ fn as_mut(&mut self) -> &mut str { pub enum Infallible {} #[stable(feature = "convert_infallible", since = "1.34.0")] -impl Clone for Infallible { +#[rustc_const_unstable(feature = "const_clone", issue = "91805")] +impl const Clone for Infallible { fn clone(&self) -> Infallible { match *self {} } diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 78383b54c5d..4ef4740799a 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -106,6 +106,7 @@ #![feature(const_caller_location)] #![feature(const_cell_into_inner)] #![feature(const_char_convert)] +#![feature(const_clone)] #![feature(const_discriminant)] #![feature(const_eval_select)] #![feature(const_float_bits_conv)] diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 7b9c6e43960..941d7e07926 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -1668,7 +1668,11 @@ const fn expect_failed(msg: &str) -> ! { ///////////////////////////////////////////////////////////////////////////// #[stable(feature = "rust1", since = "1.0.0")] -impl Clone for Option { +#[rustc_const_unstable(feature = "const_clone", issue = "91805")] +impl const Clone for Option +where + T: ~const Clone + ~const Drop, +{ #[inline] fn clone(&self) -> Self { match self { diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs index 58110b06809..cb268abc8ee 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -635,7 +635,8 @@ pub unsafe fn get_unchecked_mut(self, index: I) -> NonNull } #[stable(feature = "nonnull", since = "1.25.0")] -impl Clone for NonNull { +#[rustc_const_unstable(feature = "const_clone", issue = "91805")] +impl const Clone for NonNull { #[inline] fn clone(&self) -> Self { *self diff --git a/library/core/src/ptr/unique.rs b/library/core/src/ptr/unique.rs index d650a6f974b..ee984b7b5a4 100644 --- a/library/core/src/ptr/unique.rs +++ b/library/core/src/ptr/unique.rs @@ -146,7 +146,8 @@ pub const fn cast(self) -> Unique { } #[unstable(feature = "ptr_internals", issue = "none")] -impl Clone for Unique { +#[rustc_const_unstable(feature = "const_clone", issue = "91805")] +impl const Clone for Unique { #[inline] fn clone(&self) -> Self { *self diff --git a/library/core/src/result.rs b/library/core/src/result.rs index e6b8c8ec338..d639c7188c1 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1665,7 +1665,12 @@ fn unwrap_failed(msg: &str, error: &dyn fmt::Debug) -> ! { ///////////////////////////////////////////////////////////////////////////// #[stable(feature = "rust1", since = "1.0.0")] -impl Clone for Result { +#[rustc_const_unstable(feature = "const_clone", issue = "91805")] +impl const Clone for Result +where + T: ~const Clone + ~const Drop, + E: ~const Clone + ~const Drop, +{ #[inline] fn clone(&self) -> Self { match self { -- 2.44.0