rustc: move TypeParamDef's fields into GenericParamDefKind::Type.

This commit is contained in:
Eduard-Mihai Burtescu 2018-05-16 13:03:04 +03:00
parent ba2c5c5288
commit 73f62106ad
25 changed files with 94 additions and 91 deletions

View File

@ -739,8 +739,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for ty::Generics {
ref parent_count,
ref params,
// Reverse map to each `TypeParamDef`'s `index` field, from
// `def_id.index` (`def_id.krate` is the same as the item's).
// Reverse map to each param's `index` field, from its `def_id`.
param_def_id_to_index: _, // Don't hash this
has_self,
has_late_bound_regions,
@ -754,11 +753,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for ty::Generics {
}
}
impl_stable_hash_for!(enum ty::GenericParamDefKind {
Lifetime,
Type(ty)
});
impl_stable_hash_for!(struct ty::GenericParamDef {
name,
def_id,
@ -767,11 +761,25 @@ impl_stable_hash_for!(struct ty::GenericParamDef {
kind
});
impl_stable_hash_for!(struct ty::TypeParamDef {
has_default,
object_lifetime_default,
synthetic
});
impl<'a> HashStable<StableHashingContext<'a>> for ty::GenericParamDefKind {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
mem::discriminant(self).hash_stable(hcx, hasher);
match *self {
ty::GenericParamDefKind::Lifetime => {}
ty::GenericParamDefKind::Type {
has_default,
ref object_lifetime_default,
ref synthetic,
} => {
has_default.hash_stable(hcx, hasher);
object_lifetime_default.hash_stable(hcx, hasher);
synthetic.hash_stable(hcx, hasher);
}
}
}
}
impl<'a, 'gcx, T> HashStable<StableHashingContext<'a>>
for ::middle::resolve_lifetime::Set1<T>

View File

@ -915,7 +915,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
// region parameter definition.
self.next_region_var(EarlyBoundRegion(span, param.name)).into()
}
GenericParamDefKind::Type(_) => {
GenericParamDefKind::Type {..} => {
// Create a type inference variable for the given
// type parameter definition. The substitutions are
// for actual parameters that may be referred to by

View File

@ -1658,18 +1658,14 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
self.xcrate_object_lifetime_defaults
.entry(def_id)
.or_insert_with(|| {
tcx.generics_of(def_id)
.params
.iter()
.filter_map(|param| {
match param.kind {
GenericParamDefKind::Type(ty) => {
Some(ty.object_lifetime_default)
}
GenericParamDefKind::Lifetime => None,
tcx.generics_of(def_id).params.iter().filter_map(|param| {
match param.kind {
GenericParamDefKind::Type { object_lifetime_default, .. } => {
Some(object_lifetime_default)
}
})
.collect()
GenericParamDefKind::Lifetime => None,
}
}).collect()
})
};
unsubst

View File

@ -383,7 +383,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
for param in generics.params.iter() {
let value = match param.kind {
GenericParamDefKind::Type(_) => {
GenericParamDefKind::Type {..} => {
trait_ref.substs[param.index as usize].to_string()
},
GenericParamDefKind::Lifetime => continue,

View File

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

View File

@ -289,7 +289,7 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedFormatString {
let generics = tcx.generics_of(trait_ref.def_id);
let generic_map = generics.params.iter().filter_map(|param| {
let value = match param.kind {
GenericParamDefKind::Type(_) => {
GenericParamDefKind::Type {..} => {
trait_ref.substs[param.index as usize].to_string()
},
GenericParamDefKind::Lifetime => return None

View File

@ -2329,11 +2329,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
let substs = Substs::for_item(self, def_id, |param, substs| {
match param.kind {
GenericParamDefKind::Lifetime => bug!(),
GenericParamDefKind::Type(ty_param) => {
GenericParamDefKind::Type { has_default, .. } => {
if param.index == 0 {
ty.into()
} else {
assert!(ty_param.has_default);
assert!(has_default);
self.type_of(param.def_id).subst(self, substs).into()
}
}
@ -2477,7 +2477,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
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(),
GenericParamDefKind::Type {..} => self.mk_ty_param(param.index, param.name).into(),
}
}

View File

@ -714,13 +714,6 @@ pub enum IntVarValue {
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct FloatVarValue(pub ast::FloatTy);
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
pub struct TypeParamDef {
pub has_default: bool,
pub object_lifetime_default: ObjectLifetimeDefault,
pub synthetic: Option<hir::SyntheticTyParamKind>,
}
impl ty::EarlyBoundRegion {
pub fn to_bound_region(&self) -> ty::BoundRegion {
ty::BoundRegion::BrNamed(self.def_id, self.name)
@ -730,7 +723,11 @@ impl ty::EarlyBoundRegion {
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
pub enum GenericParamDefKind {
Lifetime,
Type(TypeParamDef),
Type {
has_default: bool,
object_lifetime_default: ObjectLifetimeDefault,
synthetic: Option<hir::SyntheticTyParamKind>,
}
}
#[derive(Clone, RustcEncodable, RustcDecodable)]
@ -811,7 +808,7 @@ impl<'a, 'gcx, 'tcx> Generics {
for param in &self.params {
match param.kind {
GenericParamDefKind::Lifetime => own_counts.lifetimes += 1,
GenericParamDefKind::Type(_) => own_counts.types += 1,
GenericParamDefKind::Type {..} => own_counts.types += 1,
};
}
@ -821,7 +818,7 @@ impl<'a, 'gcx, 'tcx> Generics {
pub fn requires_monomorphization(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> bool {
for param in &self.params {
match param.kind {
GenericParamDefKind::Type(_) => return true,
GenericParamDefKind::Type {..} => return true,
GenericParamDefKind::Lifetime => {}
}
}
@ -850,7 +847,7 @@ impl<'a, 'gcx, 'tcx> Generics {
}
}
/// Returns the `TypeParamDef` associated with this `ParamTy`.
/// Returns the `GenericParamDef` associated with this `ParamTy`.
pub fn type_param(&'tcx self,
param: &ParamTy,
tcx: TyCtxt<'a, 'gcx, 'tcx>)
@ -858,7 +855,7 @@ impl<'a, 'gcx, 'tcx> Generics {
if let Some(index) = param.idx.checked_sub(self.parent_count as u32) {
let param = &self.params[index as usize];
match param.kind {
ty::GenericParamDefKind::Type(_) => param,
ty::GenericParamDefKind::Type {..} => param,
_ => bug!("expected type parameter, but found another generic parameter")
}
} else {

View File

@ -572,7 +572,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
Substs::for_item(self, item_def_id, |param, _| {
match param.kind {
GenericParamDefKind::Lifetime => self.types.re_erased.into(),
GenericParamDefKind::Type(_) => {
GenericParamDefKind::Type {..} => {
bug!("empty_substs_for_def_id: {:?} has type parameters", item_def_id)
}
}

View File

@ -337,7 +337,9 @@ impl PrintContext {
let mut type_params =
generics.params.iter().rev().filter_map(|param| {
match param.kind {
GenericParamDefKind::Type(ty) => Some((param.def_id, ty.has_default)),
GenericParamDefKind::Type { has_default, .. } => {
Some((param.def_id, has_default))
}
GenericParamDefKind::Lifetime => None,
}
}).peekable();
@ -604,7 +606,7 @@ impl fmt::Debug for ty::GenericParamDef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let type_name = match self.kind {
ty::GenericParamDefKind::Lifetime => "Lifetime",
ty::GenericParamDefKind::Type(_) => "Type",
ty::GenericParamDefKind::Type {..} => "Type",
};
write!(f, "{}({}, {:?}, {})",
type_name,

View File

@ -1115,7 +1115,7 @@ fn create_mono_items_for_default_impls<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let substs = Substs::for_item(tcx, method.def_id, |param, _| {
match param.kind {
GenericParamDefKind::Lifetime => tcx.types.re_erased.into(),
GenericParamDefKind::Type(_) => {
GenericParamDefKind::Type {..} => {
trait_ref.substs[param.index as usize]
}
}

View File

@ -430,7 +430,7 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
let substs = Substs::for_item(tcx, self.def_id, |param, _| {
match param.kind {
GenericParamDefKind::Lifetime => tcx.types.re_erased.into(),
GenericParamDefKind::Type(_) => ty.into(),
GenericParamDefKind::Type {..} => ty.into(),
}
});

View File

@ -401,8 +401,8 @@ impl<'b, 'a, 'tcx> ReachEverythingInTheInterfaceVisitor<'b, 'a, 'tcx> {
fn generics(&mut self) -> &mut Self {
for param in &self.ev.tcx.generics_of(self.item_def_id).params {
match param.kind {
GenericParamDefKind::Type(ty) => {
if ty.has_default {
GenericParamDefKind::Type { has_default, .. } => {
if has_default {
self.ev.tcx.type_of(param.def_id).visit_with(self);
}
}
@ -1342,8 +1342,8 @@ impl<'a, 'tcx: 'a> SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
fn generics(&mut self) -> &mut Self {
for param in &self.tcx.generics_of(self.item_def_id).params {
match param.kind {
GenericParamDefKind::Type(ty) => {
if ty.has_default {
GenericParamDefKind::Type { has_default, .. } => {
if has_default {
self.tcx.type_of(param.def_id).visit_with(self);
}
}

View File

@ -224,9 +224,9 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
GenericParamDefKind::Lifetime => {
lt_accepted += 1;
}
GenericParamDefKind::Type(ty) => {
GenericParamDefKind::Type { has_default, .. } => {
ty_params.accepted += 1;
if !ty.has_default {
if !has_default {
ty_params.required += 1;
}
}
@ -251,8 +251,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
let is_object = self_ty.map_or(false, |ty| ty.sty == TRAIT_OBJECT_DUMMY_SELF);
let default_needs_object_self = |param: &ty::GenericParamDef| {
if let GenericParamDefKind::Type(ty) = param.kind {
if is_object && ty.has_default {
if let GenericParamDefKind::Type { has_default, .. } = param.kind {
if is_object && has_default {
if tcx.at(span).type_of(param.def_id).has_self_ty() {
// There is no suitable inference default for a type parameter
// that references self, in an object type.
@ -275,7 +275,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
tcx.types.re_static.into()
}
}
GenericParamDefKind::Type(ty) => {
GenericParamDefKind::Type { has_default, .. } => {
let i = param.index as usize;
// Handle Self first, so we can adjust the index to match the AST.
@ -294,7 +294,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
} else {
self.ty_infer(span).into()
}
} else if ty.has_default {
} else if has_default {
// No type parameter provided, but a default exists.
// If we are converting an object type, then the

View File

@ -109,7 +109,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
GenericParamDefKind::Lifetime => {
span_bug!(expr.span, "closure has region param")
}
GenericParamDefKind::Type(_) => {
GenericParamDefKind::Type {..} => {
self.infcx
.next_ty_var(TypeVariableOrigin::ClosureSynthetic(expr.span)).into()
}

View File

@ -730,13 +730,13 @@ fn compare_synthetic_generics<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let trait_m_generics = tcx.generics_of(trait_m.def_id);
let impl_m_type_params = impl_m_generics.params.iter().filter_map(|param| {
match param.kind {
GenericParamDefKind::Type(ty) => Some((param.def_id, ty.synthetic)),
GenericParamDefKind::Type { synthetic, .. } => Some((param.def_id, synthetic)),
GenericParamDefKind::Lifetime => None,
}
});
let trait_m_type_params = trait_m_generics.params.iter().filter_map(|param| {
match param.kind {
GenericParamDefKind::Type(ty) => Some((param.def_id, ty.synthetic)),
GenericParamDefKind::Type { synthetic, .. } => Some((param.def_id, synthetic)),
GenericParamDefKind::Lifetime => None,
}
});

View File

@ -331,7 +331,7 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
self.fcx, lifetime, Some(param)).into();
}
}
GenericParamDefKind::Type(_) => {
GenericParamDefKind::Type {..} => {
if let Some(ast_ty) = provided.as_ref().and_then(|p| {
p.types.get(i - parent_substs.len() - own_counts.lifetimes)
}) {

View File

@ -257,7 +257,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let substs = Substs::for_item(self.tcx, trait_def_id, |param, _| {
match param.kind {
GenericParamDefKind::Lifetime => {}
GenericParamDefKind::Type(_) => {
GenericParamDefKind::Type {..} => {
if param.index == 0 {
return self_ty.into();
} else if let Some(ref input_types) = opt_input_types {

View File

@ -1399,7 +1399,7 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
// `impl_self_ty()` for an explanation.
self.tcx.types.re_erased.into()
}
GenericParamDefKind::Type(_) => self.var_for_def(self.span, param),
GenericParamDefKind::Type {..} => self.var_for_def(self.span, param),
}
}
});
@ -1416,7 +1416,7 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
Substs::for_item(self.tcx, def_id, |param, _| {
match param.kind {
GenericParamDefKind::Lifetime => self.tcx.types.re_erased.into(),
GenericParamDefKind::Type(_) => {
GenericParamDefKind::Type {..} => {
self.next_ty_var(TypeVariableOrigin::SubstitutionPlaceholder(
self.tcx.def_span(def_id))).into()
}

View File

@ -4751,7 +4751,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let mut i = param.index as usize;
let segment = if i < fn_start {
if let GenericParamDefKind::Type(_) = param.kind {
if let GenericParamDefKind::Type {..} = param.kind {
// Handle Self first, so we can adjust the index to match the AST.
if has_self && i == 0 {
return opt_self_ty.map(|ty| ty.into()).unwrap_or_else(|| {
@ -4778,7 +4778,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
self.re_infer(span, Some(param)).unwrap().into()
}
}
GenericParamDefKind::Type(_) => {
GenericParamDefKind::Type {..} => {
let (types, infer_types) = segment.map_or((&[][..], true), |(s, _)| {
(s.parameters.as_ref().map_or(&[][..], |p| &p.types[..]), s.infer_types)
});
@ -4789,7 +4789,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
let has_default = match param.kind {
GenericParamDefKind::Type(ty) => ty.has_default,
GenericParamDefKind::Type { has_default, .. } => has_default,
_ => unreachable!()
};
@ -4925,9 +4925,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
GenericParamDefKind::Lifetime => {
lt_accepted += 1;
}
GenericParamDefKind::Type(ty) => {
GenericParamDefKind::Type { has_default, .. } => {
ty_params.accepted += 1;
if !ty.has_default {
if !has_default {
ty_params.required += 1;
}
}
@ -5024,12 +5024,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let segment = segment.map(|(path_segment, generics)| {
let explicit = !path_segment.infer_types;
let impl_trait = generics.params.iter().any(|param| {
if let ty::GenericParamDefKind::Type(ty) = param.kind {
if let Some(hir::SyntheticTyParamKind::ImplTrait) = ty.synthetic {
return true;
}
match param.kind {
ty::GenericParamDefKind::Type {
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait), ..
} => true,
_ => false,
}
false
});
if explicit && impl_trait {

View File

@ -377,8 +377,8 @@ fn check_where_clauses<'a, 'gcx, 'fcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>,
let generics = tcx.generics_of(def_id);
let is_our_default = |def: &ty::GenericParamDef| {
match def.kind {
GenericParamDefKind::Type(ty) => {
ty.has_default && def.index >= generics.parent_count as u32
GenericParamDefKind::Type { has_default, .. } => {
has_default && def.index >= generics.parent_count as u32
}
_ => unreachable!()
}
@ -389,7 +389,7 @@ fn check_where_clauses<'a, 'gcx, 'fcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>,
// struct Foo<T = Vec<[u32]>> { .. }
// Here the default `Vec<[u32]>` is not WF because `[u32]: Sized` does not hold.
for param in &generics.params {
if let GenericParamDefKind::Type(_) = param.kind {
if let GenericParamDefKind::Type {..} = param.kind {
if is_our_default(&param) {
let ty = fcx.tcx.type_of(param.def_id);
// ignore dependent defaults -- that is, where the default of one type
@ -417,7 +417,7 @@ fn check_where_clauses<'a, 'gcx, 'fcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>,
// All regions are identity.
fcx.tcx.mk_param_from_def(param)
}
GenericParamDefKind::Type(_) => {
GenericParamDefKind::Type {..} => {
// If the param has a default,
if is_our_default(param) {
let default_ty = fcx.tcx.type_of(param.def_id);
@ -668,7 +668,7 @@ fn reject_shadowing_parameters(tcx: TyCtxt, def_id: DefId) {
.flat_map(|param| {
match param.kind {
GenericParamDefKind::Lifetime => None,
GenericParamDefKind::Type(_) => Some((param.name, param.def_id)),
GenericParamDefKind::Type {..} => Some((param.name, param.def_id)),
}
})
.collect();

View File

@ -845,11 +845,11 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
name: keywords::SelfType.name().as_interned_str(),
def_id: tcx.hir.local_def_id(param_id),
pure_wrt_drop: false,
kind: ty::GenericParamDefKind::Type(ty::TypeParamDef {
kind: ty::GenericParamDefKind::Type {
has_default: false,
object_lifetime_default: rl::Set1::Empty,
synthetic: None,
}),
},
});
allow_defaults = true;
@ -925,12 +925,12 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
name: p.name.as_interned_str(),
def_id: tcx.hir.local_def_id(p.id),
pure_wrt_drop: p.pure_wrt_drop,
kind: ty::GenericParamDefKind::Type(ty::TypeParamDef {
kind: ty::GenericParamDefKind::Type {
has_default: p.default.is_some(),
object_lifetime_default:
object_lifetime_defaults.as_ref().map_or(rl::Set1::Empty, |o| o[i]),
synthetic: p.synthetic,
}),
},
}
}));
@ -950,11 +950,11 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
name: Symbol::intern(arg).as_interned_str(),
def_id,
pure_wrt_drop: false,
kind: ty::GenericParamDefKind::Type(ty::TypeParamDef {
kind: ty::GenericParamDefKind::Type {
has_default: false,
object_lifetime_default: rl::Set1::Empty,
synthetic: None,
}),
},
});
}
@ -965,11 +965,11 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
name: Symbol::intern("<upvar>").as_interned_str(),
def_id,
pure_wrt_drop: false,
kind: ty::GenericParamDefKind::Type(ty::TypeParamDef {
kind: ty::GenericParamDefKind::Type {
has_default: false,
object_lifetime_default: rl::Set1::Empty,
synthetic: None,
}),
},
}
}));
});

View File

@ -116,7 +116,7 @@ fn enforce_impl_params_are_constrained<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
for param in &impl_generics.params {
match param.kind {
// Disallow ANY unconstrained type parameters.
ty::GenericParamDefKind::Type(_) => {
ty::GenericParamDefKind::Type {..} => {
let param_ty = ty::ParamTy::for_def(param);
if !input_parameters.contains(&ctp::Parameter::from(param_ty)) {
report_unused_parameter(tcx,

View File

@ -263,7 +263,7 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
name,
});
}
ty::GenericParamDefKind::Type(_) => {
ty::GenericParamDefKind::Type {..} => {
types.push(P(self.ty_param_to_ty(param.clone())));
}
}

View File

@ -1340,7 +1340,7 @@ impl<'tcx> Clean<TyParam> for ty::GenericParamDef {
fn clean(&self, cx: &DocContext) -> TyParam {
cx.renderinfo.borrow_mut().external_typarams.insert(self.def_id, self.name.clean(cx));
let has_default = match self.kind {
ty::GenericParamDefKind::Type(ty) => ty.has_default,
ty::GenericParamDefKind::Type { has_default, .. } => has_default,
_ => panic!("tried to convert a non-type GenericParamDef as a type")
};
TyParam {
@ -1827,7 +1827,7 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics,
// predicates field (see rustc_typeck::collect::ty_generics), so remove
// them.
let stripped_typarams = gens.params.iter().filter_map(|param| {
if let ty::GenericParamDefKind::Type(_) = param.kind {
if let ty::GenericParamDefKind::Type {..} = param.kind {
if param.name == keywords::SelfType.name().as_str() {
assert_eq!(param.index, 0);
None