Skip to content

Implement Clone only for Copy types. #44196

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

Closed
wants to merge 2 commits into from
Closed
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
1 change: 0 additions & 1 deletion src/libcore/tuple.rs
Original file line number Diff line number Diff line change
@@ -22,7 +22,6 @@ macro_rules! tuple_impls {
)+) => {
$(
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg(stage0)]
impl<$($T:Clone),+> Clone for ($($T,)+) {
fn clone(&self) -> ($($T,)+) {
($(self.$idx.clone(),)+)
3 changes: 1 addition & 2 deletions src/librustc/ich/impls_ty.rs
Original file line number Diff line number Diff line change
@@ -677,9 +677,8 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for ty::In
def_id.hash_stable(hcx, hasher);
t.hash_stable(hcx, hasher);
}
ty::InstanceDef::CloneShim(def_id, t) => {
ty::InstanceDef::CloneShim(def_id) => {
def_id.hash_stable(hcx, hasher);
t.hash_stable(hcx, hasher);
}
}
}
22 changes: 15 additions & 7 deletions src/librustc/traits/select.rs
Original file line number Diff line number Diff line change
@@ -1258,8 +1258,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
} else {
if self.tcx().lang_items.clone_trait() == Some(def_id) {
// Same builtin conditions as `Copy`, i.e. every type which has builtin support
// for `Copy` also has builtin support for `Clone`, + tuples and arrays of `Clone`
// types have builtin support for `Clone`.
// for `Copy` also has builtin support for `Clone`.
let clone_conditions = self.copy_conditions(obligation);
self.assemble_builtin_bound_candidates(clone_conditions, &mut candidates)?;
}
@@ -1872,6 +1871,11 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {

false
},
BuiltinCandidate { .. } => {
// Hack: we always prefer a builtin impl over another one. Should apply
// ONLY for `Clone` on tuples.
other.evaluation == EvaluatedToOk
}
_ => false
}
}
@@ -2157,7 +2161,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
fn confirm_candidate(&mut self,
obligation: &TraitObligation<'tcx>,
candidate: SelectionCandidate<'tcx>)
-> Result<Selection<'tcx>,SelectionError<'tcx>>
-> Result<Selection<'tcx>, SelectionError<'tcx>>
{
debug!("confirm_candidate({:?}, {:?})",
obligation,
@@ -2268,17 +2272,21 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
obligation, has_nested);

let obligations = if has_nested {
let trait_def = obligation.predicate.def_id();
let mut trait_def = obligation.predicate.def_id();

// Hack: for having a builtin `Clone` impl, the nested types need to
// implement `Copy`.
if Some(trait_def) == self.tcx().lang_items.clone_trait() {
trait_def = self.tcx().lang_items.copy_trait().unwrap();
}

let conditions = match trait_def {
_ if Some(trait_def) == self.tcx().lang_items.sized_trait() => {
self.sized_conditions(obligation)
}
_ if Some(trait_def) == self.tcx().lang_items.copy_trait() => {
self.copy_conditions(obligation)
}
_ if Some(trait_def) == self.tcx().lang_items.clone_trait() => {
self.copy_conditions(obligation)
}
_ => bug!("unexpected builtin trait {:?}", trait_def)
};
let nested = match conditions {
12 changes: 5 additions & 7 deletions src/librustc/ty/instance.rs
Original file line number Diff line number Diff line change
@@ -38,8 +38,8 @@ pub enum InstanceDef<'tcx> {
/// drop_in_place::<T>; None for empty drop glue.
DropGlue(DefId, Option<Ty<'tcx>>),

/// Builtin method implementation, e.g. `Clone::clone`.
CloneShim(DefId, Ty<'tcx>),
/// Builtin `<T as Clone>::clone` implementation.
CloneShim(DefId),
}

impl<'tcx> InstanceDef<'tcx> {
@@ -52,7 +52,7 @@ impl<'tcx> InstanceDef<'tcx> {
InstanceDef::Intrinsic(def_id, ) |
InstanceDef::ClosureOnceShim { call_once: def_id } |
InstanceDef::DropGlue(def_id, _) |
InstanceDef::CloneShim(def_id, _) => def_id
InstanceDef::CloneShim(def_id) => def_id
}
}

@@ -81,15 +81,13 @@ impl<'tcx> fmt::Display for Instance<'tcx> {
InstanceDef::FnPtrShim(_, ty) => {
write!(f, " - shim({:?})", ty)
}
InstanceDef::ClosureOnceShim { .. } => {
InstanceDef::ClosureOnceShim { .. } |
InstanceDef::CloneShim(_) => {
write!(f, " - shim")
}
InstanceDef::DropGlue(_, ty) => {
write!(f, " - shim({:?})", ty)
}
InstanceDef::CloneShim(_, ty) => {
write!(f, " - shim({:?})", ty)
}
}
}
}
Loading