]> git.lizzy.rs Git - rust.git/blob - src/librustc_ty/instance.rs
Auto merge of #67885 - tobithiel:fix_group_lint_allow_override, r=Mark-Simulacrum
[rust.git] / src / librustc_ty / instance.rs
1 use rustc::traits;
2 use rustc::ty::subst::SubstsRef;
3 use rustc::ty::{self, Instance, TyCtxt, TypeFoldable};
4 use rustc_hir::def_id::DefId;
5 use rustc_target::spec::abi::Abi;
6
7 use log::debug;
8
9 pub fn resolve_instance<'tcx>(
10     tcx: TyCtxt<'tcx>,
11     param_env: ty::ParamEnv<'tcx>,
12     def_id: DefId,
13     substs: SubstsRef<'tcx>,
14 ) -> Option<Instance<'tcx>> {
15     debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
16     let result = if let Some(trait_def_id) = tcx.trait_of_item(def_id) {
17         debug!(" => associated item, attempting to find impl in param_env {:#?}", param_env);
18         let item = tcx.associated_item(def_id);
19         resolve_associated_item(tcx, &item, param_env, trait_def_id, substs)
20     } else {
21         let ty = tcx.type_of(def_id);
22         let item_type = tcx.subst_and_normalize_erasing_regions(substs, param_env, &ty);
23
24         let def = match item_type.kind {
25             ty::FnDef(..)
26                 if {
27                     let f = item_type.fn_sig(tcx);
28                     f.abi() == Abi::RustIntrinsic || f.abi() == Abi::PlatformIntrinsic
29                 } =>
30             {
31                 debug!(" => intrinsic");
32                 ty::InstanceDef::Intrinsic(def_id)
33             }
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)
43                     }
44                 } else {
45                     debug!(" => free item");
46                     ty::InstanceDef::Item(def_id)
47                 }
48             }
49         };
50         Some(Instance { def: def, substs: substs })
51     };
52     debug!("resolve(def_id={:?}, substs={:?}) = {:?}", def_id, substs, result);
53     result
54 }
55
56 fn resolve_associated_item<'tcx>(
57     tcx: TyCtxt<'tcx>,
58     trait_item: &ty::AssocItem,
59     param_env: ty::ParamEnv<'tcx>,
60     trait_id: DefId,
61     rcvr_substs: SubstsRef<'tcx>,
62 ) -> Option<Instance<'tcx>> {
63     let def_id = trait_item.def_id;
64     debug!(
65         "resolve_associated_item(trait_item={:?}, \
66             param_env={:?}, \
67             trait_id={:?}, \
68             rcvr_substs={:?})",
69         def_id, param_env, trait_id, rcvr_substs
70     );
71
72     let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_substs);
73     let vtbl = tcx.codegen_fulfill_obligation((param_env, ty::Binder::bind(trait_ref)));
74
75     // Now that we know which impl is being used, we can dispatch to
76     // the actual function:
77     match vtbl {
78         traits::VtableImpl(impl_data) => {
79             let (def_id, substs) =
80                 traits::find_associated_item(tcx, param_env, trait_item, rcvr_substs, &impl_data);
81
82             let resolved_item = tcx.associated_item(def_id);
83
84             // Since this is a trait item, we need to see if the item is either a trait default item
85             // or a specialization because we can't resolve those unless we can `Reveal::All`.
86             // NOTE: This should be kept in sync with the similar code in
87             // `rustc::traits::project::assemble_candidates_from_impls()`.
88             let eligible = if !resolved_item.defaultness.is_default() {
89                 true
90             } else if param_env.reveal == traits::Reveal::All {
91                 !trait_ref.needs_subst()
92             } else {
93                 false
94             };
95
96             if !eligible {
97                 return None;
98             }
99
100             let substs = tcx.erase_regions(&substs);
101             Some(ty::Instance::new(def_id, substs))
102         }
103         traits::VtableGenerator(generator_data) => Some(Instance {
104             def: ty::InstanceDef::Item(generator_data.generator_def_id),
105             substs: generator_data.substs,
106         }),
107         traits::VtableClosure(closure_data) => {
108             let trait_closure_kind = tcx.fn_trait_kind_from_lang_item(trait_id).unwrap();
109             Some(Instance::resolve_closure(
110                 tcx,
111                 closure_data.closure_def_id,
112                 closure_data.substs,
113                 trait_closure_kind,
114             ))
115         }
116         traits::VtableFnPointer(ref data) => Some(Instance {
117             def: ty::InstanceDef::FnPtrShim(trait_item.def_id, data.fn_ty),
118             substs: rcvr_substs,
119         }),
120         traits::VtableObject(ref data) => {
121             let index = traits::get_vtable_index_of_object_method(tcx, data, def_id);
122             Some(Instance { def: ty::InstanceDef::Virtual(def_id, index), substs: rcvr_substs })
123         }
124         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                 })
130             } else {
131                 None
132             }
133         }
134         traits::VtableAutoImpl(..) | traits::VtableParam(..) | traits::VtableTraitAlias(..) => None,
135     }
136 }