Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick refactoring around Substs & friends. #50801

Merged
merged 7 commits into from
May 21, 2018
Merged
32 changes: 20 additions & 12 deletions src/librustc/ich/impls_ty.rs
Original file line number Diff line number Diff line change
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
10 changes: 5 additions & 5 deletions src/librustc/infer/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,11 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {

CanonicalTyVarKind::Float => self.tcx.mk_float_var(self.next_float_var_id()),
};
Kind::from(ty)
ty.into()
}

CanonicalVarKind::Region => {
Kind::from(self.next_region_var(RegionVariableOrigin::MiscVariable(span)))
self.next_region_var(RegionVariableOrigin::MiscVariable(span)).into()
}
}
}
Expand Down Expand Up @@ -555,7 +555,7 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for Canonicalizer<'cx, 'gcx, 'tcx>
opportunistically resolved to {:?}",
vid, r
);
let cvar = self.canonical_var(info, Kind::from(r));
let cvar = self.canonical_var(info, r.into());
self.tcx().mk_region(ty::ReCanonical(cvar))
}

Expand All @@ -570,7 +570,7 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for Canonicalizer<'cx, 'gcx, 'tcx>
let info = CanonicalVarInfo {
kind: CanonicalVarKind::Region,
};
let cvar = self.canonical_var(info, Kind::from(r));
let cvar = self.canonical_var(info, r.into());
self.tcx().mk_region(ty::ReCanonical(cvar))
} else {
r
Expand Down Expand Up @@ -750,7 +750,7 @@ impl<'cx, 'gcx, 'tcx> Canonicalizer<'cx, 'gcx, 'tcx> {
let info = CanonicalVarInfo {
kind: CanonicalVarKind::Ty(ty_kind),
};
let cvar = self.canonical_var(info, Kind::from(ty_var));
let cvar = self.canonical_var(info, ty_var.into());
self.tcx().mk_infer(ty::InferTy::CanonicalTy(cvar))
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/infer/mod.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
14 changes: 5 additions & 9 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
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 Expand Up @@ -652,14 +652,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
&& fallback_has_occurred
{
let predicate = trait_predicate.map_bound(|mut trait_pred| {
{
let trait_ref = &mut trait_pred.trait_ref;
let never_substs = trait_ref.substs;
let mut unit_substs = Vec::with_capacity(never_substs.len());
unit_substs.push(self.tcx.mk_nil().into());
unit_substs.extend(&never_substs[1..]);
trait_ref.substs = self.tcx.intern_substs(&unit_substs);
}
trait_pred.trait_ref.substs = self.tcx.mk_substs_trait(
self.tcx.mk_nil(),
&trait_pred.trait_ref.substs[1..],
);
trait_pred
});
let unit_obligation = Obligation {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/mod.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
14 changes: 7 additions & 7 deletions src/librustc/traits/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use dep_graph::{DepNodeIndex, DepKind};
use hir::def_id::DefId;
use infer;
use infer::{InferCtxt, InferOk, TypeFreshener};
use ty::subst::{Kind, Subst, Substs};
use ty::subst::{Subst, Substs};
use ty::{self, ToPredicate, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable};
use ty::fast_reject;
use ty::relate::TypeRelation;
Expand Down Expand Up @@ -3019,7 +3019,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
// with a potentially unsized trailing field.
let params = substs_a.iter().enumerate().map(|(i, &k)| {
if ty_params.contains(i) {
Kind::from(tcx.types.err)
tcx.types.err.into()
} else {
k
}
Expand Down Expand Up @@ -3058,24 +3058,24 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
obligation.predicate.def_id(),
obligation.recursion_depth + 1,
inner_source,
&[inner_target]));
&[inner_target.into()]));
}

// (.., T) -> (.., U).
(&ty::TyTuple(tys_a), &ty::TyTuple(tys_b)) => {
assert_eq!(tys_a.len(), tys_b.len());

// The last field of the tuple has to exist.
let (a_last, a_mid) = if let Some(x) = tys_a.split_last() {
let (&a_last, a_mid) = if let Some(x) = tys_a.split_last() {
x
} else {
return Err(Unimplemented);
};
let b_last = tys_b.last().unwrap();
let &b_last = tys_b.last().unwrap();

// Check that the source tuple with the target's
// last element is equal to the target.
let new_tuple = tcx.mk_tup(a_mid.iter().chain(Some(b_last)));
let new_tuple = tcx.mk_tup(a_mid.iter().cloned().chain(iter::once(b_last)));
let InferOk { obligations, .. } =
self.infcx.at(&obligation.cause, obligation.param_env)
.eq(target, new_tuple)
Expand All @@ -3089,7 +3089,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
obligation.predicate.def_id(),
obligation.recursion_depth + 1,
a_last,
&[b_last]));
&[b_last.into()]));
}

_ => bug!()
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/traits/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// except according to those terms.

use hir::def_id::DefId;
use ty::subst::{Subst, Substs};
use ty::subst::{Kind, Subst, Substs};
use ty::{self, Ty, TyCtxt, ToPredicate, ToPolyTraitRef};
use ty::outlives::Component;
use util::nodemap::FxHashSet;
Expand Down Expand Up @@ -430,13 +430,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
cause: ObligationCause<'tcx>,
trait_def_id: DefId,
recursion_depth: usize,
param_ty: Ty<'tcx>,
ty_params: &[Ty<'tcx>])
self_ty: Ty<'tcx>,
params: &[Kind<'tcx>])
-> PredicateObligation<'tcx>
{
let trait_ref = ty::TraitRef {
def_id: trait_def_id,
substs: self.mk_substs_trait(param_ty, ty_params)
substs: self.mk_substs_trait(self_ty, params)
};
predicate_for_trait_ref(cause, param_env, trait_ref, recursion_depth)
}
Expand Down Expand Up @@ -512,7 +512,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
};
let trait_ref = ty::TraitRef {
def_id: fn_trait_def_id,
substs: self.mk_substs_trait(self_ty, &[arguments_tuple]),
substs: self.mk_substs_trait(self_ty, &[arguments_tuple.into()]),
};
ty::Binder::bind((trait_ref, sig.skip_binder().output()))
}
Expand Down
14 changes: 7 additions & 7 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
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 Expand Up @@ -2584,11 +2584,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}

pub fn mk_substs_trait(self,
s: Ty<'tcx>,
t: &[Ty<'tcx>])
self_ty: Ty<'tcx>,
rest: &[Kind<'tcx>])
-> &'tcx Substs<'tcx>
{
self.mk_substs(iter::once(s).chain(t.into_iter().cloned()).map(Kind::from))
self.mk_substs(iter::once(self_ty.into()).chain(rest.iter().cloned()))
}

pub fn mk_clauses<I: InternAs<[Clause<'tcx>], Clauses<'tcx>>>(self, iter: I) -> I::Output {
Expand All @@ -2600,7 +2600,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}

pub fn mk_goal(self, goal: Goal<'tcx>) -> &'tcx Goal {
&self.mk_goals(iter::once(goal))[0]
&self.intern_goals(&[goal])[0]
}

pub fn lint_node<S: Into<MultiSpan>>(self,
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/ty/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use hir::def_id::DefId;
use ty::{self, Ty, TypeFoldable, Substs, TyCtxt};
use ty::subst::Kind;
use traits;
use rustc_target::spec::abi::Abi;
use util::ppaux;
Expand Down Expand Up @@ -361,7 +360,7 @@ fn fn_once_adapter_instance<'a, 'tcx>(
let sig = substs.closure_sig(closure_did, tcx);
let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
assert_eq!(sig.inputs().len(), 1);
let substs = tcx.mk_substs([Kind::from(self_ty), sig.inputs()[0].into()].iter().cloned());
let substs = tcx.mk_substs_trait(self_ty, &[sig.inputs()[0].into()]);

debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig);
Instance { def, substs }
Expand Down
Loading