Skip to content

Make Clone a const_trait #142756

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

Merged
merged 1 commit into from
Jun 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions library/core/src/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

#![stable(feature = "rust1", since = "1.0.0")]

use crate::marker::PointeeSized;
use crate::marker::{Destruct, PointeeSized};

mod uninit;

Expand Down Expand Up @@ -157,6 +157,8 @@ mod uninit;
#[lang = "clone"]
#[rustc_diagnostic_item = "Clone"]
#[rustc_trivial_field_reads]
#[rustc_const_unstable(feature = "const_clone", issue = "142757")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think we need per trait gates? Seems like it could cause ppl to add lots of feature gates. And we can stabilize individual const traits without stabilizing the full set either way. Library feature gates are funny this way

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah looks like this is how we did it before, too. Let's keep doing that

#[const_trait]
pub trait Clone: Sized {
/// Returns a duplicate of the value.
///
Expand Down Expand Up @@ -208,7 +210,10 @@ pub trait Clone: Sized {
/// allocations.
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn clone_from(&mut self, source: &Self) {
fn clone_from(&mut self, source: &Self)
where
Self: ~const Destruct,
{
*self = source.clone()
}
}
Expand Down
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
#![feature(cfg_select)]
#![feature(cfg_target_has_reliable_f16_f128)]
#![feature(const_carrying_mul_add)]
#![feature(const_destruct)]
#![feature(const_eval_select)]
#![feature(core_intrinsics)]
#![feature(coverage_attribute)]
Expand Down
Loading