Add mk_param_from_def

This commit is contained in:
varkor 2018-05-15 13:35:53 +01:00
parent e9c28b2564
commit 0a9371ab77
8 changed files with 24 additions and 37 deletions

View file

@ -845,7 +845,7 @@ fn vtable_methods<'a, 'tcx>(
match param.kind {
GenericParamDefKind::Lifetime => tcx.types.re_erased.into(),
GenericParamDefKind::Type(_) => {
trait_ref.substs.type_for_def(param).into()
trait_ref.substs[param.index as usize]
}
}
})

View file

@ -2472,8 +2472,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
self.mk_ty_param(0, keywords::SelfType.name().as_interned_str())
}
pub fn mk_ty_param_from_def(self, def: &ty::GenericParamDef) -> Ty<'tcx> {
self.mk_ty_param(def.index, def.name)
pub fn mk_param_from_def(self, param: &ty::GenericParamDef) -> Kind<'tcx> {
match param.kind {
GenericParamDefKind::Lifetime => {
self.mk_region(ty::ReEarlyBound(param.to_early_bound_region_data())).into()
}
GenericParamDefKind::Type(_) => self.mk_ty_param(param.index, param.name).into(),
}
}
pub fn mk_anon(self, def_id: DefId, substs: &'tcx Substs<'tcx>) -> Ty<'tcx> {

View file

@ -11,7 +11,7 @@
// Type substitutions.
use hir::def_id::DefId;
use ty::{self, Lift, Slice, Region, Ty, TyCtxt, GenericParamDefKind};
use ty::{self, Lift, Slice, Region, Ty, TyCtxt};
use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
use serialize::{self, Encodable, Encoder, Decodable, Decoder};
@ -182,12 +182,7 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
pub fn identity_for_item(tcx: TyCtxt<'a, 'gcx, 'tcx>, def_id: DefId)
-> &'tcx Substs<'tcx> {
Substs::for_item(tcx, def_id, |param, _| {
match param.kind {
GenericParamDefKind::Lifetime => {
tcx.mk_region(ty::ReEarlyBound(param.to_early_bound_region_data())).into()
}
GenericParamDefKind::Type(_) => tcx.mk_ty_param_from_def(param).into(),
}
tcx.mk_param_from_def(param)
})
}
@ -293,13 +288,8 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
}
#[inline]
pub fn type_for_def(&self, ty_param_def: &ty::GenericParamDef) -> Ty<'tcx> {
self.type_at(ty_param_def.index as usize)
}
#[inline]
pub fn region_for_def(&self, def: &ty::GenericParamDef) -> ty::Region<'tcx> {
self.region_at(def.index as usize)
pub fn type_for_def(&self, def: &ty::GenericParamDef) -> Kind<'tcx> {
self.type_at(def.index as usize).into()
}
/// Transform from substitutions for a child of `source_ancestor`

View file

@ -1116,7 +1116,7 @@ fn create_mono_items_for_default_impls<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
match param.kind {
GenericParamDefKind::Lifetime => tcx.types.re_erased.into(),
GenericParamDefKind::Type(_) => {
trait_ref.substs.type_for_def(param).into()
trait_ref.substs[param.index as usize]
}
}
});

View file

@ -14,7 +14,7 @@ use rustc::traits::{FulfillmentContext, Normalized, ObligationCause};
use rustc::traits::query::{CanonicalTyGoal, NoSolution};
use rustc::traits::query::dropck_outlives::{DtorckConstraint, DropckOutlivesResult};
use rustc::ty::{self, ParamEnvAnd, Ty, TyCtxt};
use rustc::ty::subst::Subst;
use rustc::ty::subst::{UnpackedKind, Subst};
use rustc::util::nodemap::FxHashSet;
use rustc_data_structures::sync::Lrc;
use syntax::codemap::{Span, DUMMY_SP};
@ -278,11 +278,16 @@ crate fn adt_dtorck_constraint<'a, 'tcx>(
debug!("dtorck_constraint: {:?}", def);
if def.is_phantom_data() {
// The first generic parameter here is guaranteed to be a type because it's `PhantomData`.
// The first generic parameter here is guaranteed to be a type because it's
// `PhantomData`.
let param = &tcx.generics_of(def_id).params[0];
let ty = match tcx.mk_param_from_def(param).unpack() {
UnpackedKind::Type(ty) => ty,
_ => unreachable!(),
};
let result = DtorckConstraint {
outlives: vec![],
dtorck_types: vec![tcx.mk_ty_param_from_def(param)],
dtorck_types: vec![ty],
overflows: vec![],
};
debug!("dtorck_constraint: {:?} => {:?}", def, result);

View file

@ -1158,13 +1158,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
if let Some(parent_id) = generics.parent {
let parent_generics = tcx.generics_of(parent_id);
Substs::fill_item(&mut substs, tcx, parent_generics, &mut |param, _| {
match param.kind {
GenericParamDefKind::Lifetime => {
tcx.mk_region(
ty::ReEarlyBound(param.to_early_bound_region_data())).into()
}
GenericParamDefKind::Type(_) => tcx.mk_ty_param_from_def(param).into(),
}
tcx.mk_param_from_def(param)
});
// Replace all lifetimes with 'static

View file

@ -409,7 +409,7 @@ fn check_where_clauses<'a, 'gcx, 'fcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>,
match param.kind {
GenericParamDefKind::Lifetime => {
// All regions are identity.
fcx.tcx.mk_region(ty::ReEarlyBound(param.to_early_bound_region_data())).into()
fcx.tcx.mk_param_from_def(param)
}
GenericParamDefKind::Type(_) => {
// If the param has a default,

View file

@ -31,7 +31,6 @@ use middle::lang_items::SizedTraitLangItem;
use middle::resolve_lifetime as rl;
use rustc::mir::mono::Linkage;
use rustc::ty::subst::Substs;
use rustc::ty::GenericParamDefKind;
use rustc::ty::{ToPredicate, ReprOptions};
use rustc::ty::{self, AdtKind, ToPolyTraitRef, Ty, TyCtxt};
use rustc::ty::maps::Providers;
@ -1098,13 +1097,7 @@ fn type_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let substs = ty::ClosureSubsts {
substs: Substs::for_item(tcx, def_id, |param, _| {
match param.kind {
GenericParamDefKind::Lifetime => {
let region = param.to_early_bound_region_data();
tcx.mk_region(ty::ReEarlyBound(region)).into()
}
GenericParamDefKind::Type(_) => tcx.mk_ty_param_from_def(param).into(),
}
tcx.mk_param_from_def(param)
})
};