]> git.lizzy.rs Git - rust.git/commitdiff
Make `Clone` a lang item and generate builtin impls.
authorscalexm <martin.alex32@hotmail.fr>
Fri, 4 Aug 2017 12:44:12 +0000 (14:44 +0200)
committerscalexm <martin.alex32@hotmail.fr>
Mon, 14 Aug 2017 13:07:21 +0000 (15:07 +0200)
Fixes #28229.
Fixes #24000.

17 files changed:
src/libcore/array.rs
src/libcore/clone.rs
src/libcore/ptr.rs
src/libcore/tuple.rs
src/librustc/ich/impls_ty.rs
src/librustc/middle/lang_items.rs
src/librustc/traits/mod.rs
src/librustc/traits/select.rs
src/librustc/traits/structural_impls.rs
src/librustc/ty/instance.rs
src/librustc/ty/mod.rs
src/librustc_mir/shim.rs
src/librustc_trans/collector.rs
src/librustc_trans/monomorphize.rs
src/librustc_trans/partitioning.rs
src/librustc_typeck/check/method/probe.rs
src/test/run-pass/issue-37725.rs

index 37bd57034a7b6ed7647f13950730facff135cc82..0d9f9d16d82e76180954162f98680df6f28f5d83 100644 (file)
@@ -124,6 +124,7 @@ fn borrow_mut(&mut self) -> &mut [T] {
             }
 
             #[stable(feature = "rust1", since = "1.0.0")]
+            #[cfg(stage0)]
             impl<T:Copy> Clone for [T; $N] {
                 fn clone(&self) -> [T; $N] {
                     *self
index 8856d0b8cb9de5c8ee41066d3b65c0b6680161b3..2dc51718b97b23e0e0a7a491dd091f340585efbb 100644 (file)
@@ -88,6 +88,7 @@
 /// }
 /// ```
 #[stable(feature = "rust1", since = "1.0.0")]
+#[cfg_attr(not(stage0), lang = "clone")]
 pub trait Clone : Sized {
     /// Returns a copy of the value.
     ///
@@ -131,6 +132,7 @@ pub struct AssertParamIsClone<T: Clone + ?Sized> { _field: ::marker::PhantomData
 pub struct AssertParamIsCopy<T: Copy + ?Sized> { _field: ::marker::PhantomData<T> }
 
 #[stable(feature = "rust1", since = "1.0.0")]
+#[cfg(stage0)]
 impl<'a, T: ?Sized> Clone for &'a T {
     /// Returns a shallow copy of the reference.
     #[inline]
@@ -140,6 +142,7 @@ fn clone(&self) -> &'a T { *self }
 macro_rules! clone_impl {
     ($t:ty) => {
         #[stable(feature = "rust1", since = "1.0.0")]
+        #[cfg(stage0)]
         impl Clone for $t {
             /// Returns a deep copy of the value.
             #[inline]
index e35777d222c0657475fb1f490a7c7093359c3245..63e9373e936068cc2085e6f8750abfbdcf9159ec 100644 (file)
@@ -876,6 +876,7 @@ pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool {
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
+#[cfg(stage0)]
 impl<T: ?Sized> Clone for *const T {
     #[inline]
     fn clone(&self) -> *const T {
@@ -884,6 +885,7 @@ fn clone(&self) -> *const T {
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
+#[cfg(stage0)]
 impl<T: ?Sized> Clone for *mut T {
     #[inline]
     fn clone(&self) -> *mut T {
@@ -895,6 +897,7 @@ fn clone(&self) -> *mut T {
 macro_rules! fnptr_impls_safety_abi {
     ($FnTy: ty, $($Arg: ident),*) => {
         #[stable(feature = "rust1", since = "1.0.0")]
+        #[cfg(stage0)]
         impl<Ret, $($Arg),*> Clone for $FnTy {
             #[inline]
             fn clone(&self) -> Self {
index 47e9c7c903880ca053555e1a91731551077e6160..555843dba418ecaeebb88d3c36015865edfb46fd 100644 (file)
@@ -22,6 +22,7 @@ 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(),)+)
index 8a37d7bab4445c9314b23a49ce04aeb20132aa78..5b75ce7864f8d6fad964c52e0f434b3cc8aebf5c 100644 (file)
@@ -721,6 +721,10 @@ fn hash_stable<W: StableHasherResult>(&self,
                 def_id.hash_stable(hcx, hasher);
                 t.hash_stable(hcx, hasher);
             }
+            ty::InstanceDef::BuiltinShim(def_id, t) => {
+                def_id.hash_stable(hcx, hasher);
+                t.hash_stable(hcx, hasher);
+            }
         }
     }
 }
index 01ed79096b10196e107efe4f9d8c8fc0b4faf64c..8cee88bd39b7ff45f5023b89e9266e48afdf343e 100644 (file)
@@ -274,6 +274,7 @@ pub fn collect_language_items(session: &Session,
     SizedTraitLangItem,              "sized",                   sized_trait;
     UnsizeTraitLangItem,             "unsize",                  unsize_trait;
     CopyTraitLangItem,               "copy",                    copy_trait;
+    CloneTraitLangItem,              "clone",                   clone_trait;
     SyncTraitLangItem,               "sync",                    sync_trait;
     FreezeTraitLangItem,             "freeze",                  freeze_trait;
 
@@ -320,6 +321,9 @@ pub fn collect_language_items(session: &Session,
 
     StrEqFnLangItem,                 "str_eq",                  str_eq_fn;
 
+    CloneMethodLangItem,             "clone_method",            clone_method;
+    CloneFromMethodLangItem,         "clone_from_method",       clone_from_method;
+
     // A number of panic-related lang items. The `panic` item corresponds to
     // divide-by-zero and various panic cases with `match`. The
     // `panic_bounds_check` item is for indexing arrays.
index d1938197e652965dbf338ac1c233141f36c9e909..1c6d75ace52e654af8aaf283dadafdc0fb70ebc9 100644 (file)
@@ -301,7 +301,7 @@ pub enum Vtable<'tcx, N> {
     VtableObject(VtableObjectData<'tcx, N>),
 
     /// Successful resolution for a builtin trait.
-    VtableBuiltin(VtableBuiltinData<N>),
+    VtableBuiltin(VtableBuiltinData<'tcx, N>),
 
     /// Vtable automatically generated for a closure. The def ID is the ID
     /// of the closure expression. This is a `VtableImpl` in spirit, but the
@@ -345,7 +345,9 @@ pub struct VtableDefaultImplData<N> {
 }
 
 #[derive(Clone)]
-pub struct VtableBuiltinData<N> {
+pub struct VtableBuiltinData<'tcx, N> {
+    /// `ty` can be used for generating shim for builtin implementations like `Clone::clone`.
+    pub ty: ty::Ty<'tcx>,
     pub nested: Vec<N>
 }
 
@@ -769,6 +771,7 @@ pub fn map<M, F>(self, f: F) -> Vtable<'tcx, M> where F: FnMut(N) -> M {
             }),
             VtableParam(n) => VtableParam(n.into_iter().map(f).collect()),
             VtableBuiltin(i) => VtableBuiltin(VtableBuiltinData {
+                ty: i.ty,
                 nested: i.nested.into_iter().map(f).collect(),
             }),
             VtableObject(o) => VtableObject(VtableObjectData {
index 3e39d5921351f3c05c983e4dcb6c80ebebcd2447..88cca70993e9a5a71422e271296a2161793bbb4a 100644 (file)
@@ -1296,6 +1296,14 @@ fn assemble_candidates<'o>(&mut self,
          } else if self.tcx().lang_items.unsize_trait() == Some(def_id) {
              self.assemble_candidates_for_unsizing(obligation, &mut candidates);
          } 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`.
+                let clone_conditions = self.copy_conditions(obligation);
+                self.assemble_builtin_bound_candidates(clone_conditions, &mut candidates)?;
+            }
+
              self.assemble_closure_candidates(obligation, &mut candidates)?;
              self.assemble_fn_pointer_candidates(obligation, &mut candidates)?;
              self.assemble_candidates_from_impls(obligation, &mut candidates)?;
@@ -2164,8 +2172,8 @@ fn confirm_candidate(&mut self,
 
         match candidate {
             BuiltinCandidate { has_nested } => {
-                Ok(VtableBuiltin(
-                    self.confirm_builtin_candidate(obligation, has_nested)))
+                let data = self.confirm_builtin_candidate(obligation, has_nested);
+                Ok(VtableBuiltin(data))
             }
 
             ParamCandidate(param) => {
@@ -2257,7 +2265,7 @@ fn confirm_param_candidate(&mut self,
     fn confirm_builtin_candidate(&mut self,
                                  obligation: &TraitObligation<'tcx>,
                                  has_nested: bool)
-                                 -> VtableBuiltinData<PredicateObligation<'tcx>>
+                                 -> VtableBuiltinData<'tcx, PredicateObligation<'tcx>>
     {
         debug!("confirm_builtin_candidate({:?}, {:?})",
                obligation, has_nested);
@@ -2271,6 +2279,9 @@ fn confirm_builtin_candidate(&mut self,
                 _ 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 {
@@ -2291,7 +2302,9 @@ fn confirm_builtin_candidate(&mut self,
 
         debug!("confirm_builtin_candidate: obligations={:?}",
                obligations);
-        VtableBuiltinData { nested: obligations }
+
+        let self_ty = self.infcx.shallow_resolve(obligation.predicate.skip_binder().self_ty());
+        VtableBuiltinData { ty: self_ty, nested: obligations }
     }
 
     /// This handles the case where a `impl Foo for ..` impl is being used.
@@ -2598,8 +2611,8 @@ fn confirm_poly_trait_refs(&mut self,
 
     fn confirm_builtin_unsize_candidate(&mut self,
                                         obligation: &TraitObligation<'tcx>,)
-                                        -> Result<VtableBuiltinData<PredicateObligation<'tcx>>,
-                                                  SelectionError<'tcx>> {
+        -> Result<VtableBuiltinData<'tcx, PredicateObligation<'tcx>>, SelectionError<'tcx>>
+    {
         let tcx = self.tcx();
 
         // assemble_candidates_for_unsizing should ensure there are no late bound
@@ -2801,7 +2814,7 @@ fn confirm_builtin_unsize_candidate(&mut self,
             _ => bug!()
         };
 
-        Ok(VtableBuiltinData { nested: nested })
+        Ok(VtableBuiltinData { ty: source, nested: nested })
     }
 
     ///////////////////////////////////////////////////////////////////////////
index f1c176561ea4875d908cc67689c2fc4166d02308..a83849898f5875b4dc4212406627bb42ec4107c5 100644 (file)
@@ -86,9 +86,9 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
     }
 }
 
-impl<'tcx, N: fmt::Debug> fmt::Debug for traits::VtableBuiltinData<N> {
+impl<'tcx, N: fmt::Debug> fmt::Debug for traits::VtableBuiltinData<'tcx, N> {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f, "VtableBuiltin(nested={:?})", self.nested)
+        write!(f, "VtableBuiltin(ty={:?}, nested={:?})", self.ty, self.nested)
     }
 }
 
@@ -300,7 +300,14 @@ fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lif
                 })
             }
             traits::VtableParam(n) => Some(traits::VtableParam(n)),
-            traits::VtableBuiltin(d) => Some(traits::VtableBuiltin(d)),
+            traits::VtableBuiltin(traits::VtableBuiltinData { ty, nested }) => {
+                tcx.lift(&ty).map(|ty| {
+                    traits::VtableBuiltin(traits::VtableBuiltinData {
+                        ty,
+                        nested,
+                    })
+                })
+            }
             traits::VtableObject(traits::VtableObjectData {
                 upcast_trait_ref,
                 vtable_base,
@@ -378,9 +385,10 @@ fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
     }
 }
 
-impl<'tcx, N: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::VtableBuiltinData<N> {
+impl<'tcx, N: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::VtableBuiltinData<'tcx, N> {
     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
         traits::VtableBuiltinData {
+            ty: self.ty.fold_with(folder),
             nested: self.nested.fold_with(folder),
         }
     }
index 32063a2dda603f1b7fb742c6806122008e1711a6..e8daf7c09f4f2aa79a858274082d6e80ea0c9f3b 100644 (file)
@@ -24,15 +24,22 @@ pub struct Instance<'tcx> {
 pub enum InstanceDef<'tcx> {
     Item(DefId),
     Intrinsic(DefId),
-    // <fn() as FnTrait>::call_*
-    // def-id is FnTrait::call_*
+
+    /// <fn() as FnTrait>::call_*
+    /// def-id is FnTrait::call_*
     FnPtrShim(DefId, Ty<'tcx>),
-    // <Trait as Trait>::fn
+
+    /// <Trait as Trait>::fn
     Virtual(DefId, usize),
-    // <[mut closure] as FnOnce>::call_once
+
+    /// <[mut closure] as FnOnce>::call_once
     ClosureOnceShim { call_once: DefId },
-    // drop_in_place::<T>; None for empty drop glue.
+
+    /// drop_in_place::<T>; None for empty drop glue.
     DropGlue(DefId, Option<Ty<'tcx>>),
+
+    /// Builtin method implementation, e.g. `Clone::clone`.
+    BuiltinShim(DefId, Ty<'tcx>),
 }
 
 impl<'tcx> InstanceDef<'tcx> {
@@ -43,9 +50,9 @@ pub fn def_id(&self) -> DefId {
             InstanceDef::FnPtrShim(def_id, _) |
             InstanceDef::Virtual(def_id, _) |
             InstanceDef::Intrinsic(def_id, ) |
-            InstanceDef::ClosureOnceShim { call_once: def_id }
-                => def_id,
-            InstanceDef::DropGlue(def_id, _) => def_id
+            InstanceDef::ClosureOnceShim { call_once: def_id } |
+            InstanceDef::DropGlue(def_id, _) |
+            InstanceDef::BuiltinShim(def_id, _) => def_id
         }
     }
 
@@ -80,6 +87,9 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
             InstanceDef::DropGlue(_, ty) => {
                 write!(f, " - shim({:?})", ty)
             }
+            InstanceDef::BuiltinShim(_, ty) => {
+                write!(f, " - shim({:?})", ty)
+            }
         }
     }
 }
index 28a73f4a4d387dcbe36170619619c236cd78baa0..81a0af63942ed930dccff2a7cee8960ac660e4ee 100644 (file)
@@ -2227,7 +2227,8 @@ pub fn instance_mir(self, instance: ty::InstanceDef<'gcx>)
             ty::InstanceDef::FnPtrShim(..) |
             ty::InstanceDef::Virtual(..) |
             ty::InstanceDef::ClosureOnceShim { .. } |
-            ty::InstanceDef::DropGlue(..) => {
+            ty::InstanceDef::DropGlue(..) |
+            ty::InstanceDef::BuiltinShim(..) => {
                 self.mir_shims(instance)
             }
         }
index 62e762be93a71938a7a3f93ccfdc40d1186a43f7..cb1a2f6b1077e69a83378aef80864ba0f611810a 100644 (file)
@@ -17,6 +17,7 @@
 use rustc::ty::{self, Ty};
 use rustc::ty::subst::{Kind, Subst, Substs};
 use rustc::ty::maps::Providers;
+use rustc_const_math::{ConstInt, ConstUsize};
 
 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
 
@@ -98,14 +99,26 @@ fn make_shim<'a, 'tcx>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
         ty::InstanceDef::DropGlue(def_id, ty) => {
             build_drop_shim(tcx, def_id, ty)
         }
+        ty::InstanceDef::BuiltinShim(def_id, ty) => {
+            let name = tcx.item_name(def_id).as_str();
+            let trait_id = tcx.trait_of_item(def_id);
+            if trait_id == tcx.lang_items.clone_trait() && name == "clone" {
+                build_clone_shim(tcx, def_id, ty)
+            } else if trait_id == tcx.lang_items.clone_trait() && name == "clone_from" {
+                debug!("make_shim({:?}: using default trait implementation", instance);
+                return tcx.optimized_mir(def_id);
+            } else {
+                bug!("builtin shim {:?} not supported", instance)
+            }
+        }
         ty::InstanceDef::Intrinsic(_) => {
             bug!("creating shims from intrinsics ({:?}) is unsupported", instance)
         }
     };
-        debug!("make_shim({:?}) = untransformed {:?}", instance, result);
-        no_landing_pads::no_landing_pads(tcx, &mut result);
-        simplify::simplify_cfg(&mut result);
-        add_call_guards::CriticalCallEdges.add_call_guards(&mut result);
+    debug!("make_shim({:?}) = untransformed {:?}", instance, result);
+    no_landing_pads::no_landing_pads(tcx, &mut result);
+    simplify::simplify_cfg(&mut result);
+    add_call_guards::CriticalCallEdges.add_call_guards(&mut result);
     debug!("make_shim({:?}) = {:?}", instance, result);
 
     tcx.alloc_mir(result)
@@ -259,6 +272,164 @@ fn downcast_subpath(&self, _path: Self::Path, _variant: usize) -> Option<Self::P
     }
 }
 
+/// Build a `Clone::clone` shim for `recvr_ty`. Here, `def_id` is `Clone::clone`.
+fn build_clone_shim<'a, 'tcx>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
+                              def_id: DefId,
+                              recvr_ty: ty::Ty<'tcx>)
+                              -> Mir<'tcx>
+{
+    let sig = tcx.fn_sig(def_id);
+    let sig = tcx.erase_late_bound_regions(&sig);
+    let span = tcx.def_span(def_id);
+
+    debug!("build_clone_shim(def_id={:?})", def_id);
+
+    let mut local_decls = local_decls_for_sig(&sig, span);
+    let source_info = SourceInfo { span, scope: ARGUMENT_VISIBILITY_SCOPE };
+
+    let mut blocks = IndexVec::new();
+    let block = |blocks: &mut IndexVec<_, _>, statement, kind| {
+        blocks.push(BasicBlockData {
+            statements: vec![statement],
+            terminator: Some(Terminator { source_info, kind }),
+            is_cleanup: false
+        })
+    };
+
+    let rcvr = Lvalue::Local(Local::new(1+0)).deref();
+
+    let call_clone = |i, ty, rcvr_field, blocks: &mut _, local_decls: &mut IndexVec<_, _>| {
+        // `func == Clone::clone(&ty) -> ty`
+        let substs = Substs::for_item(tcx, def_id, |_, _| tcx.types.re_erased, |_, _| ty);
+        let func = Operand::Constant(box Constant {
+            span: span,
+            ty: tcx.mk_fn_def(def_id, substs),
+            literal: Literal::Value {
+                value: ConstVal::Function(def_id, substs),
+            },
+        });
+
+        let ref_loc = Lvalue::Local(
+            local_decls.push(temp_decl(
+                Mutability::Not,
+                tcx.mk_ref(tcx.types.re_erased, ty::TypeAndMut {
+                    ty,
+                    mutbl: hir::Mutability::MutImmutable,
+                }),
+                span
+            ))
+        );
+
+        let loc = Lvalue::Local(
+            local_decls.push(temp_decl(
+                Mutability::Not,
+                ty,
+                span
+            ))
+        );
+
+        // `let ref_loc: &ty = &rcvr_field;`
+        let statement = Statement {
+            source_info: source_info,
+            kind: StatementKind::Assign(
+                ref_loc.clone(),
+                Rvalue::Ref(tcx.types.re_erased, BorrowKind::Shared, rcvr_field)
+            )
+        };
+
+        // `let loc = Clone::clone(ref_loc);`
+        block(blocks, statement, TerminatorKind::Call {
+            func,
+            args: vec![Operand::Consume(ref_loc)],
+            destination: Some((loc.clone(), BasicBlock::new(i + 1))),
+            cleanup: None,
+        });
+
+        loc
+    };
+
+    match recvr_ty.sty {
+        ty::TyArray(ty, len) => {
+            let mut returns = Vec::new();
+            for i in 0..len {
+                let index = ConstUsize::new(i as u64, tcx.sess.target.uint_type).unwrap();
+                let rcvr_field = rcvr.clone().index(
+                    Operand::Constant(box Constant {
+                        span,
+                        ty: tcx.types.usize,
+                        literal: Literal::Value {
+                            value: ConstVal::Integral(ConstInt::Usize(index))
+                        }
+                    })
+                );
+
+                // `returns[i] = Clone::clone(&rcvr[i]);`
+                returns.push(call_clone(i, ty, rcvr_field, &mut blocks, &mut local_decls));
+            }
+
+            // `return [returns[0], returns[1], ..., returns[len - 1]];`
+            let statement = Statement {
+                source_info: source_info,
+                kind: StatementKind::Assign(
+                    Lvalue::Local(RETURN_POINTER),
+                    Rvalue::Aggregate(
+                        box AggregateKind::Array(ty),
+                        returns.into_iter().map(|loc| Operand::Consume(loc)).collect()
+                    )
+                )
+            };
+            block(&mut blocks, statement, TerminatorKind::Return);
+        }
+        ty::TyTuple(tys, _) => {
+            let mut returns = Vec::new();
+            for (i, ity) in tys.iter().enumerate() {
+                let rcvr_field = rcvr.clone().field(Field::new(i), *ity);
+
+                // `returns[i] = Clone::clone(&rcvr.i);`
+                returns.push(call_clone(i, *ity, rcvr_field, &mut blocks, &mut local_decls));
+            }
+
+            // `return (returns[0], returns[1], ..., returns[tys.len() - 1]);`
+            let statement = Statement {
+                source_info: source_info,
+                kind: StatementKind::Assign(
+                    Lvalue::Local(RETURN_POINTER),
+                    Rvalue::Aggregate(
+                        box AggregateKind::Tuple,
+                        returns.into_iter().map(|loc| Operand::Consume(loc)).collect()
+                    )
+                )
+            };
+            block(&mut blocks, statement, TerminatorKind::Return);
+        }
+        _ => {
+            // `return *self;`
+            let statement = Statement {
+                source_info: source_info,
+                kind: StatementKind::Assign(
+                    Lvalue::Local(RETURN_POINTER),
+                    Rvalue::Use(Operand::Consume(rcvr))
+                )
+            };
+            block(&mut blocks, statement, TerminatorKind::Return);
+        }
+    };
+
+    let mir = Mir::new(
+        blocks,
+        IndexVec::from_elem_n(
+            VisibilityScopeData { span: span, parent_scope: None }, 1
+        ),
+        IndexVec::new(),
+        sig.output(),
+        local_decls,
+        sig.inputs().len(),
+        vec![],
+        span
+    );
+    mir
+}
+
 /// Build a "call" shim for `def_id`. The shim calls the
 /// function specified by `call_kind`, first adjusting its first
 /// argument according to `rcvr_adjustment`.
index b31295f4022ed3580b333b8c0682712e413d537a..4e02810b040cab696bb5e818f29fe6e5d71e9a07 100644 (file)
@@ -699,7 +699,8 @@ fn visit_instance_use<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
         }
         ty::InstanceDef::ClosureOnceShim { .. } |
         ty::InstanceDef::Item(..) |
-        ty::InstanceDef::FnPtrShim(..) => {
+        ty::InstanceDef::FnPtrShim(..) |
+        ty::InstanceDef::BuiltinShim(..) => {
             output.push(create_fn_trans_item(instance));
         }
     }
@@ -716,7 +717,8 @@ fn should_trans_locally<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: &Instan
         ty::InstanceDef::Virtual(..) |
         ty::InstanceDef::FnPtrShim(..) |
         ty::InstanceDef::DropGlue(..) |
-        ty::InstanceDef::Intrinsic(_) => return true
+        ty::InstanceDef::Intrinsic(_) |
+        ty::InstanceDef::BuiltinShim(..) => return true
     };
     match tcx.hir.get_if_local(def_id) {
         Some(hir_map::NodeForeignItem(..)) => {
index 1f6a262162d3950021550cba8a44dce92be8777d..1936775df0ab3d6e61733e542ec176e026917f12 100644 (file)
@@ -143,6 +143,12 @@ fn resolve_associated_item<'a, 'tcx>(
                 substs: rcvr_substs
             }
         }
+        traits::VtableBuiltin(ref data) => {
+            Instance {
+                def: ty::InstanceDef::BuiltinShim(def_id, data.ty),
+                substs: rcvr_substs
+            }
+        }
         _ => {
             bug!("static call to invalid vtable: {:?}", vtbl)
         }
index 63c7b18e8d29054687c954031986ca60fa15a8b0..0149714d0b79935ba99545d4df4e873c0a60ddfa 100644 (file)
@@ -361,7 +361,8 @@ fn place_root_translation_items<'a, 'tcx, I>(scx: &SharedCrateContext<'a, 'tcx>,
                                 InstanceDef::Virtual(..) |
                                 InstanceDef::Intrinsic(..) |
                                 InstanceDef::ClosureOnceShim { .. } |
-                                InstanceDef::DropGlue(..) => {
+                                InstanceDef::DropGlue(..) |
+                                InstanceDef::BuiltinShim(..) => {
                                     bug!("partitioning: Encountered unexpected
                                           root translation item: {:?}",
                                           trans_item)
@@ -603,7 +604,8 @@ fn characteristic_def_id_of_trans_item<'a, 'tcx>(scx: &SharedCrateContext<'a, 't
                 ty::InstanceDef::ClosureOnceShim { .. } |
                 ty::InstanceDef::Intrinsic(..) |
                 ty::InstanceDef::DropGlue(..) |
-                ty::InstanceDef::Virtual(..) => return None
+                ty::InstanceDef::Virtual(..) |
+                ty::InstanceDef::BuiltinShim(..) => return None
             };
 
             // If this is a method, we want to put it into the same module as
index 7bf671d5e9f9376de47ef543d5842d935f692457..a0985102f1ccbe729ee85ef599b387b68731f7e6 100644 (file)
@@ -719,6 +719,8 @@ fn assemble_extension_candidates_for_trait(&mut self,
                 continue;
             }
 
+            self.assemble_builtin_candidates(import_id, trait_def_id, item.clone());
+
             self.assemble_extension_candidates_for_trait_impls(import_id, trait_def_id,
                                                                item.clone());
 
@@ -732,6 +734,49 @@ fn assemble_extension_candidates_for_trait(&mut self,
         Ok(())
     }
 
+    fn assemble_builtin_candidates(&mut self,
+                                   import_id: Option<ast::NodeId>,
+                                   trait_def_id: DefId,
+                                   item: ty::AssociatedItem) {
+        if Some(trait_def_id) == self.tcx.lang_items.clone_trait() {
+            self.assemble_builtin_clone_candidates(import_id, trait_def_id, item);
+        }
+    }
+
+    fn assemble_builtin_clone_candidates(&mut self,
+                                         import_id: Option<ast::NodeId>,
+                                         trait_def_id: DefId,
+                                         item: ty::AssociatedItem) {
+        for step in Rc::clone(&self.steps).iter() {
+            match step.self_ty.sty {
+                ty::TyInfer(ty::IntVar(_)) | ty::TyInfer(ty::FloatVar(_)) |
+                ty::TyUint(_) | ty::TyInt(_) | ty::TyBool | ty::TyFloat(_) |
+                ty::TyFnDef(..) | ty::TyFnPtr(_) | ty::TyChar |
+                ty::TyRawPtr(..) | ty::TyError | ty::TyNever |
+                ty::TyRef(_, ty::TypeAndMut { ty: _, mutbl: hir::MutImmutable }) |
+                ty::TyArray(..) | ty::TyTuple(..) => {
+                    ()
+                }
+
+                _ => continue,
+            };
+
+            let substs = Substs::for_item(self.tcx,
+                                          trait_def_id,
+                                          |def, _| self.region_var_for_def(self.span, def),
+                                          |def, substs| {
+                if def.index == 0 {
+                    step.self_ty
+                } else {
+                    self.type_var_for_def(self.span, def, substs)
+                }
+            });
+
+            let xform_self_ty = self.xform_self_ty(&item, step.self_ty, substs);
+            self.push_inherent_candidate(xform_self_ty, item, TraitCandidate, import_id);
+        }
+    }
+
     fn assemble_extension_candidates_for_trait_impls(&mut self,
                                                      import_id: Option<ast::NodeId>,
                                                      trait_def_id: DefId,
index 5ed1295c85c76c69021fc7f89277adad71a30ee6..a8fb11f9c62bd58b4f94c71dcaa1cf142e1b0b9a 100644 (file)
@@ -8,7 +8,11 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-pub fn foo<'a>(s: &'a mut ()) where &'a mut (): Clone {
-    s.clone();
+trait Foo {
+    fn foo(&self);
+}
+
+fn foo<'a>(s: &'a mut ()) where &'a mut (): Foo {
+    s.foo();
 }
 fn main() {}