stop using monomorphize::resolve()

This commit is contained in:
Douglas Campos 2017-09-27 23:23:16 -04:00
parent 8a5800e1d4
commit 081b29c1f6
7 changed files with 49 additions and 128 deletions

View File

@ -118,7 +118,10 @@ impl<'a, 'b, 'tcx> Instance<'tcx> {
/// The point where linking happens. Resolve a (def_id, substs)
/// pair to an instance.
pub fn resolve(tcx: TyCtxt<'a, 'tcx, 'tcx>, param_env: ty::ParamEnv<'tcx>, def_id: DefId, substs: &'tcx Substs<'tcx>) -> Option<Instance<'tcx>> {
pub fn resolve(tcx: TyCtxt<'a, 'tcx, 'tcx>,
param_env: ty::ParamEnv<'tcx>,
def_id: DefId,
substs: &'tcx Substs<'tcx>) -> Option<Instance<'tcx>> {
debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
let result = if let Some(trait_def_id) = tcx.trait_of_item(def_id) {
debug!(" => associated item, attempting to find impl");
@ -154,7 +157,7 @@ impl<'a, 'b, 'tcx> Instance<'tcx> {
}
}
};
Some(Instance {
Some(Instance {
def: def,
substs: substs
})

View File

@ -91,7 +91,10 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> {
let caller_def_id = self.tcx.hir.local_def_id(caller_id);
let param_env = self.tcx.param_env(caller_def_id);
if let Some(instance) = Instance::resolve(self.tcx, param_env, callee_def_id, substs) {
if let Some(instance) = Instance::resolve(self.tcx,
param_env,
callee_def_id,
substs) {
callsites.push_back(CallSite {
callee: instance.def_id(),
substs: instance.substs,

View File

@ -19,9 +19,10 @@ use common::{self, CrateContext};
use consts;
use declare;
use llvm::{self, ValueRef};
use monomorphize::{self, Instance};
use monomorphize::Instance;
use rustc::hir::def_id::DefId;
use rustc::ty::TypeFoldable;
use rustc::ty::{self, TypeFoldable};
use rustc::traits;
use rustc::ty::subst::Substs;
use type_of;
@ -179,5 +180,8 @@ pub fn resolve_and_get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
substs: &'tcx Substs<'tcx>)
-> ValueRef
{
get_fn(ccx, monomorphize::resolve(ccx.tcx(), def_id, substs))
get_fn(ccx, ty::Instance::resolve(ccx.tcx(),
ty::ParamEnv::empty(traits::Reveal::All),
def_id,
substs).unwrap())
}

View File

@ -566,7 +566,10 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
if let ConstVal::Unevaluated(def_id, substs) = constant.val {
let substs = self.tcx.trans_apply_param_substs(self.param_substs,
&substs);
let instance = monomorphize::resolve(self.tcx, def_id, substs);
let instance = ty::Instance::resolve(self.tcx,
ty::ParamEnv::empty(traits::Reveal::All),
def_id,
substs).unwrap();
collect_neighbours(self.tcx, instance, true, self.output);
}
@ -587,7 +590,11 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
let constness = match (self.const_context, &callee_ty.sty) {
(true, &ty::TyFnDef(def_id, substs)) if self.tcx.is_const_fn(def_id) => {
let instance = monomorphize::resolve(self.tcx, def_id, substs);
let instance =
ty::Instance::resolve(self.tcx,
ty::ParamEnv::empty(traits::Reveal::All),
def_id,
substs).unwrap();
Some(instance)
}
_ => None
@ -657,7 +664,10 @@ fn visit_fn_use<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
output: &mut Vec<TransItem<'tcx>>)
{
if let ty::TyFnDef(def_id, substs) = ty.sty {
let instance = monomorphize::resolve(tcx, def_id, substs);
let instance = ty::Instance::resolve(tcx,
ty::ParamEnv::empty(traits::Reveal::All),
def_id,
substs).unwrap();
visit_instance_use(tcx, instance, is_direct_call, output);
}
}
@ -845,7 +855,11 @@ fn create_trans_items_for_vtable_methods<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
// Walk all methods of the trait, including those of its supertraits
let methods = traits::get_vtable_methods(tcx, poly_trait_ref);
let methods = methods.filter_map(|method| method)
.map(|(def_id, substs)| monomorphize::resolve(tcx, def_id, substs))
.map(|(def_id, substs)| ty::Instance::resolve(
tcx,
ty::ParamEnv::empty(traits::Reveal::All),
def_id,
substs).unwrap())
.filter(|&instance| should_trans_locally(tcx, &instance))
.map(|instance| create_fn_trans_item(instance));
output.extend(methods);
@ -1000,8 +1014,10 @@ fn create_trans_items_for_default_impls<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
continue;
}
let instance =
monomorphize::resolve(tcx, method.def_id, callee_substs);
let instance = ty::Instance::resolve(tcx,
ty::ParamEnv::empty(traits::Reveal::All),
method.def_id,
callee_substs).unwrap();
let trans_item = create_fn_trans_item(instance);
if trans_item.is_instantiable(tcx) && should_trans_locally(tcx, &instance) {

View File

@ -13,6 +13,7 @@ use rustc::middle::lang_items;
use rustc::middle::const_val::{ConstEvalErr, ConstInt, ErrKind};
use rustc::ty::{self, Ty, TypeFoldable};
use rustc::ty::layout::{self, LayoutTyper};
use rustc::traits;
use rustc::mir;
use abi::{Abi, FnType, ArgType};
use adt;
@ -429,7 +430,10 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
let (instance, mut llfn) = match callee.ty.sty {
ty::TyFnDef(def_id, substs) => {
(Some(monomorphize::resolve(bcx.ccx.tcx(), def_id, substs)),
(Some(ty::Instance::resolve(bcx.ccx.tcx(),
ty::ParamEnv::empty(traits::Reveal::All),
def_id,
substs).unwrap()),
None)
}
ty::TyFnPtr(_) => {

View File

@ -14,6 +14,7 @@ use rustc_const_math::ConstInt::*;
use rustc_const_math::{ConstInt, ConstMathErr};
use rustc::hir::def_id::DefId;
use rustc::infer::TransNormalize;
use rustc::traits;
use rustc::mir;
use rustc::mir::tcx::LvalueTy;
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
@ -30,7 +31,6 @@ use common::{C_array, C_bool, C_bytes, C_int, C_uint, C_big_integral, C_u32, C_u
use common::{C_null, C_struct, C_str_slice, C_undef, C_usize, C_vector, is_undef};
use common::const_to_opt_u128;
use consts;
use monomorphize;
use type_of;
use type_::Type;
use value::Value;
@ -261,7 +261,10 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
substs: &'tcx Substs<'tcx>,
args: IndexVec<mir::Local, Result<Const<'tcx>, ConstEvalErr<'tcx>>>)
-> Result<Const<'tcx>, ConstEvalErr<'tcx>> {
let instance = monomorphize::resolve(ccx.tcx(), def_id, substs);
let instance = ty::Instance::resolve(ccx.tcx(),
ty::ParamEnv::empty(traits::Reveal::All),
def_id,
substs).unwrap();
let mir = ccx.tcx().instance_mir(instance.def);
MirConstContext::new(ccx, &mir, instance.substs, args).trans()
}

View File

@ -8,9 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use abi::Abi;
use common::*;
use rustc::hir::def_id::DefId;
use rustc::middle::lang_items::DropInPlaceFnLangItem;
use rustc::traits;
@ -99,115 +96,6 @@ pub fn resolve_closure<'a, 'tcx> (
}
}
fn resolve_associated_item<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
trait_item: &ty::AssociatedItem,
trait_id: DefId,
rcvr_substs: &'tcx Substs<'tcx>
) -> Instance<'tcx> {
let def_id = trait_item.def_id;
debug!("resolve_associated_item(trait_item={:?}, \
trait_id={:?}, \
rcvr_substs={:?})",
def_id, trait_id, rcvr_substs);
let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_substs);
let vtbl = tcx.trans_fulfill_obligation(
DUMMY_SP, ty::ParamEnv::empty(traits::Reveal::All), ty::Binder(trait_ref));
// Now that we know which impl is being used, we can dispatch to
// the actual function:
match vtbl {
traits::VtableImpl(impl_data) => {
let (def_id, substs) = traits::find_associated_item(
tcx, trait_item, rcvr_substs, &impl_data);
let substs = tcx.erase_regions(&substs);
ty::Instance::new(def_id, substs)
}
traits::VtableGenerator(closure_data) => {
Instance {
def: ty::InstanceDef::Item(closure_data.closure_def_id),
substs: closure_data.substs.substs
}
}
traits::VtableClosure(closure_data) => {
let trait_closure_kind = tcx.lang_items().fn_trait_kind(trait_id).unwrap();
resolve_closure(tcx, closure_data.closure_def_id, closure_data.substs,
trait_closure_kind)
}
traits::VtableFnPointer(ref data) => {
Instance {
def: ty::InstanceDef::FnPtrShim(trait_item.def_id, data.fn_ty),
substs: rcvr_substs
}
}
traits::VtableObject(ref data) => {
let index = tcx.get_vtable_index_of_object_method(data, def_id);
Instance {
def: ty::InstanceDef::Virtual(def_id, index),
substs: rcvr_substs
}
}
traits::VtableBuiltin(..) if Some(trait_id) == tcx.lang_items().clone_trait() => {
Instance {
def: ty::InstanceDef::CloneShim(def_id, trait_ref.self_ty()),
substs: rcvr_substs
}
}
_ => {
bug!("static call to invalid vtable: {:?}", vtbl)
}
}
}
/// The point where linking happens. Resolve a (def_id, substs)
/// pair to an instance.
pub fn resolve<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId,
substs: &'tcx Substs<'tcx>
) -> Instance<'tcx> {
debug!("resolve(def_id={:?}, substs={:?})",
def_id, substs);
let result = if let Some(trait_def_id) = tcx.trait_of_item(def_id) {
debug!(" => associated item, attempting to find impl");
let item = tcx.associated_item(def_id);
resolve_associated_item(tcx, &item, trait_def_id, substs)
} else {
let item_type = def_ty(tcx, def_id, substs);
let def = match item_type.sty {
ty::TyFnDef(..) if {
let f = item_type.fn_sig(tcx);
f.abi() == Abi::RustIntrinsic ||
f.abi() == Abi::PlatformIntrinsic
} =>
{
debug!(" => intrinsic");
ty::InstanceDef::Intrinsic(def_id)
}
_ => {
if Some(def_id) == tcx.lang_items().drop_in_place_fn() {
let ty = substs.type_at(0);
if type_needs_drop(tcx, ty) {
debug!(" => nontrivial drop glue");
ty::InstanceDef::DropGlue(def_id, Some(ty))
} else {
debug!(" => trivial drop glue");
ty::InstanceDef::DropGlue(def_id, None)
}
} else {
debug!(" => free item");
ty::InstanceDef::Item(def_id)
}
}
};
Instance { def, substs }
};
debug!("resolve(def_id={:?}, substs={:?}) = {}",
def_id, substs, result);
result
}
pub fn resolve_drop_in_place<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
ty: Ty<'tcx>)
@ -215,7 +103,7 @@ pub fn resolve_drop_in_place<'a, 'tcx>(
{
let def_id = tcx.require_lang_item(DropInPlaceFnLangItem);
let substs = tcx.intern_substs(&[Kind::from(ty)]);
resolve(tcx, def_id, substs)
Instance::resolve(tcx, ty::ParamEnv::empty(traits::Reveal::All), def_id, substs).unwrap()
}
pub fn custom_coerce_unsize_info<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,