1 use rustc_hir::def_id::DefId;
2 use rustc_middle::ty::subst::SubstsRef;
3 use rustc_middle::ty::{self, Instance, TyCtxt, TypeFoldable};
4 use rustc_infer::infer::TyCtxtInferExt;
6 use rustc_target::spec::abi::Abi;
7 use rustc_trait_selection::traits;
8 use traits::{Reveal, translate_substs};
12 pub fn resolve_instance<'tcx>(
14 param_env: ty::ParamEnv<'tcx>,
16 substs: SubstsRef<'tcx>,
17 ) -> Option<Instance<'tcx>> {
18 debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
19 let result = if let Some(trait_def_id) = tcx.trait_of_item(def_id) {
20 debug!(" => associated item, attempting to find impl in param_env {:#?}", param_env);
21 let item = tcx.associated_item(def_id);
22 resolve_associated_item(tcx, &item, param_env, trait_def_id, substs)
24 let ty = tcx.type_of(def_id);
25 let item_type = tcx.subst_and_normalize_erasing_regions(substs, param_env, &ty);
27 let def = match item_type.kind {
30 let f = item_type.fn_sig(tcx);
31 f.abi() == Abi::RustIntrinsic || f.abi() == Abi::PlatformIntrinsic
34 debug!(" => intrinsic");
35 ty::InstanceDef::Intrinsic(def_id)
37 ty::FnDef(def_id, substs) if Some(def_id) == tcx.lang_items().drop_in_place_fn() => {
38 let ty = substs.type_at(0);
40 if ty.needs_drop(tcx, param_env) {
41 // `DropGlue` requires a monomorphic aka concrete type.
46 debug!(" => nontrivial drop glue");
47 ty::InstanceDef::DropGlue(def_id, Some(ty))
49 debug!(" => trivial drop glue");
50 ty::InstanceDef::DropGlue(def_id, None)
54 debug!(" => free item");
55 ty::InstanceDef::Item(def_id)
58 Some(Instance { def, substs })
60 debug!("resolve(def_id={:?}, substs={:?}) = {:?}", def_id, substs, result);
64 fn resolve_associated_item<'tcx>(
66 trait_item: &ty::AssocItem,
67 param_env: ty::ParamEnv<'tcx>,
69 rcvr_substs: SubstsRef<'tcx>,
70 ) -> Option<Instance<'tcx>> {
71 let def_id = trait_item.def_id;
73 "resolve_associated_item(trait_item={:?}, \
77 def_id, param_env, trait_id, rcvr_substs
80 let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_substs);
81 let vtbl = tcx.codegen_fulfill_obligation((param_env, ty::Binder::bind(trait_ref)))?;
83 // Now that we know which impl is being used, we can dispatch to
84 // the actual function:
86 traits::VtableImpl(impl_data) => {
88 "resolving VtableImpl: {:?}, {:?}, {:?}, {:?}",
89 param_env, trait_item, rcvr_substs, impl_data
91 assert!(!rcvr_substs.needs_infer());
92 assert!(!trait_ref.needs_infer());
94 let trait_def_id = tcx.trait_id_of_impl(impl_data.impl_def_id).unwrap();
95 let trait_def = tcx.trait_def(trait_def_id);
96 let leaf_def = trait_def
97 .ancestors(tcx, impl_data.impl_def_id)
99 .leaf_def(tcx, trait_item.ident, trait_item.kind)
101 bug!("{:?} not found in {:?}", trait_item, impl_data.impl_def_id);
103 let def_id = leaf_def.item.def_id;
105 let substs = tcx.infer_ctxt().enter(|infcx| {
106 let param_env = param_env.with_reveal_all();
107 let substs = rcvr_substs.rebase_onto(tcx, trait_def_id, impl_data.substs);
108 let substs = translate_substs(
111 impl_data.impl_def_id,
113 leaf_def.defining_node,
115 infcx.tcx.erase_regions(&substs)
118 // Since this is a trait item, we need to see if the item is either a trait default item
119 // or a specialization because we can't resolve those unless we can `Reveal::All`.
120 // NOTE: This should be kept in sync with the similar code in
121 // `rustc_middle::traits::project::assemble_candidates_from_impls()`.
122 let eligible = if leaf_def.is_final() {
123 // Non-specializable items are always projectable.
126 // Only reveal a specializable default if we're past type-checking
127 // and the obligation is monomorphic, otherwise passes such as
128 // transmute checking and polymorphic MIR optimizations could
129 // get a result which isn't correct for all monomorphizations.
130 if param_env.reveal == Reveal::All { !trait_ref.needs_subst() } else { false }
137 let substs = tcx.erase_regions(&substs);
138 Some(ty::Instance::new(def_id, substs))
140 traits::VtableGenerator(generator_data) => Some(Instance {
141 def: ty::InstanceDef::Item(generator_data.generator_def_id),
142 substs: generator_data.substs,
144 traits::VtableClosure(closure_data) => {
145 let trait_closure_kind = tcx.fn_trait_kind_from_lang_item(trait_id).unwrap();
146 Some(Instance::resolve_closure(
148 closure_data.closure_def_id,
153 traits::VtableFnPointer(ref data) => {
154 // `FnPtrShim` requires a monomorphic aka concrete type.
155 if data.fn_ty.needs_subst() {
160 def: ty::InstanceDef::FnPtrShim(trait_item.def_id, data.fn_ty),
164 traits::VtableObject(ref data) => {
165 let index = traits::get_vtable_index_of_object_method(tcx, data, def_id);
166 Some(Instance { def: ty::InstanceDef::Virtual(def_id, index), substs: rcvr_substs })
168 traits::VtableBuiltin(..) => {
169 if Some(trait_ref.def_id) == tcx.lang_items().clone_trait() {
170 // FIXME(eddyb) use lang items for methods instead of names.
171 let name = tcx.item_name(def_id);
172 if name == sym::clone {
173 let self_ty = trait_ref.self_ty();
175 // `CloneShim` requires a monomorphic aka concrete type.
176 if self_ty.needs_subst() {
181 def: ty::InstanceDef::CloneShim(def_id, self_ty),
185 assert_eq!(name, sym::clone_from);
187 // Use the default `fn clone_from` from `trait Clone`.
188 let substs = tcx.erase_regions(&rcvr_substs);
189 Some(ty::Instance::new(def_id, substs))
195 traits::VtableAutoImpl(..) | traits::VtableParam(..) | traits::VtableTraitAlias(..) => None,