Skip to content

Commit

Permalink
rustc: move TypeParamDef's fields into GenericParamDefKind::Type.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed May 21, 2018
1 parent ba2c5c5 commit 73f6210
Show file tree
Hide file tree
Showing 25 changed files with 94 additions and 91 deletions.
32 changes: 20 additions & 12 deletions src/librustc/ich/impls_ty.rs
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/infer/mod.rs
Expand Up @@ -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
Expand Down
18 changes: 7 additions & 11 deletions src/librustc/middle/resolve_lifetime.rs
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/error_reporting.rs
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/mod.rs
Expand Up @@ -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]
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/on_unimplemented.rs
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/ty/context.rs
Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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(),
}
}

Expand Down
21 changes: 9 additions & 12 deletions src/librustc/ty/mod.rs
Expand Up @@ -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)
Expand All @@ -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)]
Expand Down Expand Up @@ -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,
};
}

Expand All @@ -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 => {}
}
}
Expand Down Expand Up @@ -850,15 +847,15 @@ 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>)
-> &'tcx GenericParamDef {
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 {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/util.rs
Expand Up @@ -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)
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/librustc/util/ppaux.rs
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/monomorphize/collector.rs
Expand Up @@ -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]
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/shim.rs
Expand Up @@ -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(),
}
});

Expand Down
8 changes: 4 additions & 4 deletions src/librustc_privacy/lib.rs
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/librustc_typeck/astconv.rs
Expand Up @@ -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;
}
}
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/closure.rs
Expand Up @@ -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()
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/compare_method.rs
Expand Up @@ -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,
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/method/confirm.rs
Expand Up @@ -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)
}) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/method/mod.rs
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/method/probe.rs
Expand Up @@ -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),
}
}
});
Expand All @@ -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()
}
Expand Down

0 comments on commit 73f6210

Please sign in to comment.