|
1 | 1 | use rustc::ty::subst::SubstsRef;
|
2 | 2 | use rustc::ty::{self, Instance, TyCtxt, TypeFoldable};
|
3 | 3 | use rustc_hir::def_id::DefId;
|
| 4 | +use rustc_span::sym; |
4 | 5 | use rustc_target::spec::abi::Abi;
|
5 | 6 | use rustc_trait_selection::traits;
|
6 | 7 |
|
@@ -31,21 +32,26 @@ pub fn resolve_instance<'tcx>(
|
31 | 32 | debug!(" => intrinsic");
|
32 | 33 | ty::InstanceDef::Intrinsic(def_id)
|
33 | 34 | }
|
34 |
| - _ => { |
35 |
| - if Some(def_id) == tcx.lang_items().drop_in_place_fn() { |
36 |
| - let ty = substs.type_at(0); |
37 |
| - if ty.needs_drop(tcx, param_env.with_reveal_all()) { |
38 |
| - debug!(" => nontrivial drop glue"); |
39 |
| - ty::InstanceDef::DropGlue(def_id, Some(ty)) |
40 |
| - } else { |
41 |
| - debug!(" => trivial drop glue"); |
42 |
| - ty::InstanceDef::DropGlue(def_id, None) |
| 35 | + ty::FnDef(def_id, substs) if Some(def_id) == tcx.lang_items().drop_in_place_fn() => { |
| 36 | + let ty = substs.type_at(0); |
| 37 | + |
| 38 | + if ty.needs_drop(tcx, param_env) { |
| 39 | + // `DropGlue` requires a monomorphic aka concrete type. |
| 40 | + if ty.needs_subst() { |
| 41 | + return None; |
43 | 42 | }
|
| 43 | + |
| 44 | + debug!(" => nontrivial drop glue"); |
| 45 | + ty::InstanceDef::DropGlue(def_id, Some(ty)) |
44 | 46 | } else {
|
45 |
| - debug!(" => free item"); |
46 |
| - ty::InstanceDef::Item(def_id) |
| 47 | + debug!(" => trivial drop glue"); |
| 48 | + ty::InstanceDef::DropGlue(def_id, None) |
47 | 49 | }
|
48 | 50 | }
|
| 51 | + _ => { |
| 52 | + debug!(" => free item"); |
| 53 | + ty::InstanceDef::Item(def_id) |
| 54 | + } |
49 | 55 | };
|
50 | 56 | Some(Instance { def, substs })
|
51 | 57 | };
|
@@ -113,20 +119,44 @@ fn resolve_associated_item<'tcx>(
|
113 | 119 | trait_closure_kind,
|
114 | 120 | ))
|
115 | 121 | }
|
116 |
| - traits::VtableFnPointer(ref data) => Some(Instance { |
117 |
| - def: ty::InstanceDef::FnPtrShim(trait_item.def_id, data.fn_ty), |
118 |
| - substs: rcvr_substs, |
119 |
| - }), |
| 122 | + traits::VtableFnPointer(ref data) => { |
| 123 | + // `FnPtrShim` requires a monomorphic aka concrete type. |
| 124 | + if data.fn_ty.needs_subst() { |
| 125 | + return None; |
| 126 | + } |
| 127 | + |
| 128 | + Some(Instance { |
| 129 | + def: ty::InstanceDef::FnPtrShim(trait_item.def_id, data.fn_ty), |
| 130 | + substs: rcvr_substs, |
| 131 | + }) |
| 132 | + } |
120 | 133 | traits::VtableObject(ref data) => {
|
121 | 134 | let index = traits::get_vtable_index_of_object_method(tcx, data, def_id);
|
122 | 135 | Some(Instance { def: ty::InstanceDef::Virtual(def_id, index), substs: rcvr_substs })
|
123 | 136 | }
|
124 | 137 | traits::VtableBuiltin(..) => {
|
125 |
| - if tcx.lang_items().clone_trait().is_some() { |
126 |
| - Some(Instance { |
127 |
| - def: ty::InstanceDef::CloneShim(def_id, trait_ref.self_ty()), |
128 |
| - substs: rcvr_substs, |
129 |
| - }) |
| 138 | + if Some(trait_ref.def_id) == tcx.lang_items().clone_trait() { |
| 139 | + // FIXME(eddyb) use lang items for methods instead of names. |
| 140 | + let name = tcx.item_name(def_id); |
| 141 | + if name == sym::clone { |
| 142 | + let self_ty = trait_ref.self_ty(); |
| 143 | + |
| 144 | + // `CloneShim` requires a monomorphic aka concrete type. |
| 145 | + if self_ty.needs_subst() { |
| 146 | + return None; |
| 147 | + } |
| 148 | + |
| 149 | + Some(Instance { |
| 150 | + def: ty::InstanceDef::CloneShim(def_id, self_ty), |
| 151 | + substs: rcvr_substs, |
| 152 | + }) |
| 153 | + } else { |
| 154 | + assert_eq!(name, sym::clone_from); |
| 155 | + |
| 156 | + // Use the default `fn clone_from` from `trait Clone`. |
| 157 | + let substs = tcx.erase_regions(&rcvr_substs); |
| 158 | + Some(ty::Instance::new(def_id, substs)) |
| 159 | + } |
130 | 160 | } else {
|
131 | 161 | None
|
132 | 162 | }
|
|
0 commit comments