1 use rustc_hir::def_id::DefId;
2 use rustc_infer::infer::TyCtxtInferExt;
3 use rustc_middle::ty::subst::SubstsRef;
4 use rustc_middle::ty::{self, Instance, TyCtxt, TypeFoldable};
6 use rustc_target::spec::abi::Abi;
7 use rustc_trait_selection::traits;
8 use traits::{translate_substs, Reveal};
12 pub fn resolve_instance<'tcx>(
14 (param_env, def_id, substs): (ty::ParamEnv<'tcx>, DefId, SubstsRef<'tcx>),
15 ) -> Option<Instance<'tcx>> {
16 debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
17 let result = if let Some(trait_def_id) = tcx.trait_of_item(def_id) {
18 debug!(" => associated item, attempting to find impl in param_env {:#?}", param_env);
19 let item = tcx.associated_item(def_id);
20 resolve_associated_item(tcx, &item, param_env, trait_def_id, substs)
22 let ty = tcx.type_of(def_id);
23 let item_type = tcx.subst_and_normalize_erasing_regions(substs, param_env, &ty);
25 let def = match item_type.kind {
28 let f = item_type.fn_sig(tcx);
29 f.abi() == Abi::RustIntrinsic || f.abi() == Abi::PlatformIntrinsic
32 debug!(" => intrinsic");
33 ty::InstanceDef::Intrinsic(def_id)
35 ty::FnDef(def_id, substs) if Some(def_id) == tcx.lang_items().drop_in_place_fn() => {
36 let ty = substs.type_at(0);
38 if ty.needs_drop(tcx, param_env) {
39 // `DropGlue` requires a monomorphic aka concrete type.
44 debug!(" => nontrivial drop glue");
45 ty::InstanceDef::DropGlue(def_id, Some(ty))
47 debug!(" => trivial drop glue");
48 ty::InstanceDef::DropGlue(def_id, None)
52 debug!(" => free item");
53 ty::InstanceDef::Item(def_id)
56 Some(Instance { def, substs })
58 debug!("resolve(def_id={:?}, substs={:?}) = {:?}", def_id, substs, result);
62 fn resolve_associated_item<'tcx>(
64 trait_item: &ty::AssocItem,
65 param_env: ty::ParamEnv<'tcx>,
67 rcvr_substs: SubstsRef<'tcx>,
68 ) -> Option<Instance<'tcx>> {
69 let def_id = trait_item.def_id;
71 "resolve_associated_item(trait_item={:?}, \
75 def_id, param_env, trait_id, rcvr_substs
78 let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_substs);
79 let vtbl = tcx.codegen_fulfill_obligation((param_env, ty::Binder::bind(trait_ref)))?;
81 // Now that we know which impl is being used, we can dispatch to
82 // the actual function:
84 traits::VtableImpl(impl_data) => {
86 "resolving VtableImpl: {:?}, {:?}, {:?}, {:?}",
87 param_env, trait_item, rcvr_substs, impl_data
89 assert!(!rcvr_substs.needs_infer());
90 assert!(!trait_ref.needs_infer());
92 let trait_def_id = tcx.trait_id_of_impl(impl_data.impl_def_id).unwrap();
93 let trait_def = tcx.trait_def(trait_def_id);
94 let leaf_def = trait_def
95 .ancestors(tcx, impl_data.impl_def_id)
97 .leaf_def(tcx, trait_item.ident, trait_item.kind)
99 bug!("{:?} not found in {:?}", trait_item, impl_data.impl_def_id);
101 let def_id = leaf_def.item.def_id;
103 let substs = tcx.infer_ctxt().enter(|infcx| {
104 let param_env = param_env.with_reveal_all();
105 let substs = rcvr_substs.rebase_onto(tcx, trait_def_id, impl_data.substs);
106 let substs = translate_substs(
109 impl_data.impl_def_id,
111 leaf_def.defining_node,
113 infcx.tcx.erase_regions(&substs)
116 // Since this is a trait item, we need to see if the item is either a trait default item
117 // or a specialization because we can't resolve those unless we can `Reveal::All`.
118 // NOTE: This should be kept in sync with the similar code in
119 // `rustc_trait_selection::traits::project::assemble_candidates_from_impls()`.
120 let eligible = if leaf_def.is_final() {
121 // Non-specializable items are always projectable.
124 // Only reveal a specializable default if we're past type-checking
125 // and the obligation is monomorphic, otherwise passes such as
126 // transmute checking and polymorphic MIR optimizations could
127 // get a result which isn't correct for all monomorphizations.
128 if param_env.reveal == Reveal::All {
129 !trait_ref.still_further_specializable()
139 let substs = tcx.erase_regions(&substs);
140 Some(ty::Instance::new(def_id, substs))
142 traits::VtableGenerator(generator_data) => Some(Instance {
143 def: ty::InstanceDef::Item(generator_data.generator_def_id),
144 substs: generator_data.substs,
146 traits::VtableClosure(closure_data) => {
147 let trait_closure_kind = tcx.fn_trait_kind_from_lang_item(trait_id).unwrap();
148 Some(Instance::resolve_closure(
150 closure_data.closure_def_id,
155 traits::VtableFnPointer(ref data) => {
156 // `FnPtrShim` requires a monomorphic aka concrete type.
157 if data.fn_ty.needs_subst() {
162 def: ty::InstanceDef::FnPtrShim(trait_item.def_id, data.fn_ty),
166 traits::VtableObject(ref data) => {
167 let index = traits::get_vtable_index_of_object_method(tcx, data, def_id);
168 Some(Instance { def: ty::InstanceDef::Virtual(def_id, index), substs: rcvr_substs })
170 traits::VtableBuiltin(..) => {
171 if Some(trait_ref.def_id) == tcx.lang_items().clone_trait() {
172 // FIXME(eddyb) use lang items for methods instead of names.
173 let name = tcx.item_name(def_id);
174 if name == sym::clone {
175 let self_ty = trait_ref.self_ty();
177 // `CloneShim` requires a monomorphic aka concrete type.
178 if self_ty.needs_subst() {
183 def: ty::InstanceDef::CloneShim(def_id, self_ty),
187 assert_eq!(name, sym::clone_from);
189 // Use the default `fn clone_from` from `trait Clone`.
190 let substs = tcx.erase_regions(&rcvr_substs);
191 Some(ty::Instance::new(def_id, substs))
197 traits::VtableAutoImpl(..) | traits::VtableParam(..) | traits::VtableTraitAlias(..) => None,
201 pub fn provide(providers: &mut ty::query::Providers<'_>) {
202 *providers = ty::query::Providers { resolve_instance, ..*providers };