Skip to content

Commit

Permalink
rustc: use Vec<Kind> in Substs, where Kind is a &TyS | &Region tagged…
Browse files Browse the repository at this point in the history
… pointer.
  • Loading branch information
eddyb committed Aug 26, 2016
1 parent dffd238 commit 7a8d482
Show file tree
Hide file tree
Showing 57 changed files with 532 additions and 409 deletions.
1 change: 1 addition & 0 deletions src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(collections)]
#![feature(conservative_impl_trait)]
#![feature(const_fn)]
#![feature(core_intrinsics)]
#![feature(enumset)]
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub trait CrateStore<'tcx> {
fn closure_kind(&self, def_id: DefId) -> ty::ClosureKind;
fn closure_ty<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
-> ty::ClosureTy<'tcx>;
fn item_variances(&self, def: DefId) -> ty::ItemVariances;
fn item_variances(&self, def: DefId) -> Vec<ty::Variance>;
fn repr_attrs(&self, def: DefId) -> Vec<attr::ReprAttr>;
fn item_type<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
-> Ty<'tcx>;
Expand Down Expand Up @@ -328,7 +328,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
fn closure_kind(&self, def_id: DefId) -> ty::ClosureKind { bug!("closure_kind") }
fn closure_ty<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
-> ty::ClosureTy<'tcx> { bug!("closure_ty") }
fn item_variances(&self, def: DefId) -> ty::ItemVariances { bug!("item_variances") }
fn item_variances(&self, def: DefId) -> Vec<ty::Variance> { bug!("item_variances") }
fn repr_attrs(&self, def: DefId) -> Vec<attr::ReprAttr> { bug!("repr_attrs") }
fn item_type<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
-> Ty<'tcx> { bug!("item_type") }
Expand Down
54 changes: 35 additions & 19 deletions src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,12 @@ struct LifetimeContext<'a, 'tcx: 'a> {

#[derive(PartialEq, Debug)]
enum ScopeChain<'a> {
/// EarlyScope(['a, 'b, ...], s) extends s with early-bound
/// lifetimes.
EarlyScope(&'a [hir::LifetimeDef], Scope<'a>),
/// EarlyScope(['a, 'b, ...], start, s) extends s with early-bound
/// lifetimes, with consecutive parameter indices from `start`.
/// That is, 'a has index `start`, 'b has index `start + 1`, etc.
/// Indices before `start` correspond to other generic parameters
/// of a parent item (trait/impl of a method), or `Self` in traits.
EarlyScope(&'a [hir::LifetimeDef], u32, Scope<'a>),
/// LateScope(['a, 'b, ...], s) extends s with late-bound
/// lifetimes introduced by the declaration binder_id.
LateScope(&'a [hir::LifetimeDef], Scope<'a>),
Expand Down Expand Up @@ -157,7 +160,12 @@ impl<'a, 'tcx, 'v> Visitor<'v> for LifetimeContext<'a, 'tcx> {
hir::ItemImpl(_, _, ref generics, _, _, _) => {
// These kinds of items have only early bound lifetime parameters.
let lifetimes = &generics.lifetimes;
this.with(EarlyScope(lifetimes, &ROOT_SCOPE), |old_scope, this| {
let start = if let hir::ItemTrait(..) = item.node {
1 // Self comes before lifetimes
} else {
0
};
this.with(EarlyScope(lifetimes, start, &ROOT_SCOPE), |old_scope, this| {
this.check_lifetime_defs(old_scope, lifetimes);
intravisit::walk_item(this, item);
});
Expand Down Expand Up @@ -461,7 +469,7 @@ fn extract_labels(ctxt: &mut LifetimeContext, b: &hir::Block) {
FnScope { s, .. } => { scope = s; }
RootScope => { return; }

EarlyScope(lifetimes, s) |
EarlyScope(lifetimes, _, s) |
LateScope(lifetimes, s) => {
for lifetime_def in lifetimes {
// FIXME (#24278): non-hygienic comparison
Expand Down Expand Up @@ -566,8 +574,24 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
.cloned()
.partition(|l| self.map.late_bound.contains_key(&l.lifetime.id));

// Find the start of nested early scopes, e.g. in methods.
let mut start = 0;
if let EarlyScope(..) = *self.scope {
let parent = self.hir_map.expect_item(self.hir_map.get_parent(fn_id));
if let hir::ItemTrait(..) = parent.node {
start += 1; // Self comes first.
}
match parent.node {
hir::ItemTrait(_, ref generics, _, _) |
hir::ItemImpl(_, _, ref generics, _, _, _) => {
start += generics.lifetimes.len() + generics.ty_params.len();
}
_ => {}
}
}

let this = self;
this.with(EarlyScope(&early, this.scope), move |old_scope, this| {
this.with(EarlyScope(&early, start as u32, this.scope), move |old_scope, this| {
this.with(LateScope(&late, this.scope), move |_, this| {
this.check_lifetime_defs(old_scope, &generics.lifetimes);
walk(this);
Expand Down Expand Up @@ -597,19 +621,11 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
break;
}

EarlyScope(lifetimes, s) => {
EarlyScope(lifetimes, start, s) => {
match search_lifetimes(lifetimes, lifetime_ref) {
Some((mut index, lifetime_def)) => {
// Adjust for nested early scopes, e.g. in methods.
let mut parent = s;
while let EarlyScope(lifetimes, s) = *parent {
index += lifetimes.len() as u32;
parent = s;
}
assert_eq!(*parent, RootScope);

Some((index, lifetime_def)) => {
let decl_id = lifetime_def.id;
let def = DefEarlyBoundRegion(index, decl_id);
let def = DefEarlyBoundRegion(start + index, decl_id);
self.insert_lifetime(lifetime_ref, def);
return;
}
Expand Down Expand Up @@ -671,7 +687,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
break;
}

EarlyScope(lifetimes, s) |
EarlyScope(lifetimes, _, s) |
LateScope(lifetimes, s) => {
search_result = search_lifetimes(lifetimes, lifetime_ref);
if search_result.is_some() {
Expand Down Expand Up @@ -767,7 +783,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
return;
}

EarlyScope(lifetimes, s) |
EarlyScope(lifetimes, _, s) |
LateScope(lifetimes, s) => {
if let Some((_, lifetime_def)) = search_lifetimes(lifetimes, lifetime) {
signal_shadowing_problem(
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/traits/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl<'a, 'gcx, 'tcx> DeferredObligation<'tcx> {
let concrete_ty = ty_scheme.ty.subst(tcx, substs);
let predicate = ty::TraitRef {
def_id: self.predicate.def_id(),
substs: Substs::new_trait(tcx, vec![], vec![], concrete_ty)
substs: Substs::new_trait(tcx, concrete_ty, &[])
}.to_predicate();

let original_obligation = Obligation::new(self.cause.clone(),
Expand Down Expand Up @@ -440,7 +440,7 @@ fn trait_ref_type_vars<'a, 'gcx, 'tcx>(selcx: &mut SelectionContext<'a, 'gcx, 't
{
t.skip_binder() // ok b/c this check doesn't care about regions
.input_types()
.map(|t| selcx.infcx().resolve_type_vars_if_possible(t))
.map(|t| selcx.infcx().resolve_type_vars_if_possible(&t))
.filter(|t| t.has_infer_types())
.flat_map(|t| t.walk())
.filter(|t| match t.sty { ty::TyInfer(_) => true, _ => false })
Expand Down
34 changes: 16 additions & 18 deletions src/librustc/traits/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use super::util;
use hir::def_id::DefId;
use infer;
use infer::{InferCtxt, InferOk, TypeFreshener, TypeOrigin};
use ty::subst::{Subst, Substs};
use ty::subst::{Kind, Subst, Substs};
use ty::{self, ToPredicate, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable};
use traits;
use ty::fast_reject;
Expand Down Expand Up @@ -1933,7 +1933,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {

// for `PhantomData<T>`, we pass `T`
ty::TyStruct(def, substs) if def.is_phantom_data() => {
substs.types().cloned().collect()
substs.types().collect()
}

ty::TyStruct(def, substs) | ty::TyEnum(def, substs) => {
Expand Down Expand Up @@ -1982,7 +1982,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
trait_def_id,
recursion_depth,
normalized_ty,
vec![]);
&[]);
obligations.push(skol_obligation);
this.infcx().plug_leaks(skol_map, snapshot, &obligations)
})
Expand Down Expand Up @@ -2180,8 +2180,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
let input_types = data.principal.input_types();
let assoc_types = data.projection_bounds.iter()
.map(|pb| pb.skip_binder().ty);
let all_types: Vec<_> = input_types.cloned()
.chain(assoc_types)
let all_types: Vec<_> = input_types.chain(assoc_types)
.collect();

// reintroduce the two binding levels we skipped, then flatten into one
Expand Down Expand Up @@ -2598,14 +2597,14 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
// TyError and ensure they do not affect any other fields.
// This could be checked after type collection for any struct
// with a potentially unsized trailing field.
let types = substs_a.types().enumerate().map(|(i, ty)| {
let params = substs_a.params().iter().enumerate().map(|(i, &k)| {
if ty_params.contains(i) {
tcx.types.err
Kind::from(tcx.types.err)
} else {
ty
k
}
}).collect();
let substs = Substs::new(tcx, types, substs_a.regions().cloned().collect());
});
let substs = Substs::new(tcx, params);
for &ty in fields.split_last().unwrap().1 {
if ty.subst(tcx, substs).references_error() {
return Err(Unimplemented);
Expand All @@ -2618,15 +2617,14 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {

// Check that the source structure with the target's
// type parameters is a subtype of the target.
let types = substs_a.types().enumerate().map(|(i, ty)| {
let params = substs_a.params().iter().enumerate().map(|(i, &k)| {
if ty_params.contains(i) {
substs_b.type_at(i)
Kind::from(substs_b.type_at(i))
} else {
ty
k
}
}).collect();
let substs = Substs::new(tcx, types, substs_a.regions().cloned().collect());
let new_struct = tcx.mk_struct(def, substs);
});
let new_struct = tcx.mk_struct(def, Substs::new(tcx, params));
let origin = TypeOrigin::Misc(obligation.cause.span);
let InferOk { obligations, .. } =
self.infcx.sub_types(false, origin, new_struct, target)
Expand All @@ -2639,7 +2637,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
obligation.predicate.def_id(),
obligation.recursion_depth + 1,
inner_source,
vec![inner_target]));
&[inner_target]));
}

_ => bug!()
Expand Down Expand Up @@ -2753,7 +2751,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {

obligation.predicate.skip_binder().input_types()
.zip(impl_trait_ref.input_types())
.any(|(&obligation_ty, &impl_ty)| {
.any(|(obligation_ty, impl_ty)| {
let simplified_obligation_ty =
fast_reject::simplify_type(self.tcx(), obligation_ty, true);
let simplified_impl_ty =
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/traits/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
Ok(def_id) => {
Ok(ty::TraitRef {
def_id: def_id,
substs: Substs::new_trait(self, vec![], vec![], param_ty)
substs: Substs::new_trait(self, param_ty, &[])
})
}
Err(e) => {
Expand All @@ -401,12 +401,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
trait_def_id: DefId,
recursion_depth: usize,
param_ty: Ty<'tcx>,
ty_params: Vec<Ty<'tcx>>)
ty_params: &[Ty<'tcx>])
-> PredicateObligation<'tcx>
{
let trait_ref = ty::TraitRef {
def_id: trait_def_id,
substs: Substs::new_trait(self, ty_params, vec![], param_ty)
substs: Substs::new_trait(self, param_ty, ty_params)
};
predicate_for_trait_ref(cause, trait_ref, recursion_depth)
}
Expand Down Expand Up @@ -496,7 +496,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
};
let trait_ref = ty::TraitRef {
def_id: fn_trait_def_id,
substs: Substs::new_trait(self, vec![arguments_tuple], vec![], self_ty),
substs: Substs::new_trait(self, self_ty, &[arguments_tuple]),
};
ty::Binder((trait_ref, sig.0.output))
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ fn keep_local<'tcx, T: ty::TypeFoldable<'tcx>>(x: &T) -> bool {
impl_interners!('tcx,
type_list: mk_type_list(Vec<Ty<'tcx>>, keep_local) -> [Ty<'tcx>],
substs: mk_substs(Substs<'tcx>, |substs: &Substs| {
substs.types().any(keep_local) || substs.regions().any(keep_local)
substs.params().iter().any(keep_local)
}) -> Substs<'tcx>,
bare_fn: mk_bare_fn(BareFnTy<'tcx>, |fty: &BareFnTy| {
keep_local(&fty.sig)
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ impl FlagComputation {
}

fn add_substs(&mut self, substs: &Substs) {
for &ty in substs.types() {
for ty in substs.types() {
self.add_ty(ty);
}

for &r in substs.regions() {
for r in substs.regions() {
self.add_region(r);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dep_map_ty! { TraitItemDefIds: TraitItemDefIds(DefId) -> Rc<Vec<ty::ImplOrTraitI
dep_map_ty! { ImplTraitRefs: ItemSignature(DefId) -> Option<ty::TraitRef<'tcx>> }
dep_map_ty! { TraitDefs: ItemSignature(DefId) -> &'tcx ty::TraitDef<'tcx> }
dep_map_ty! { AdtDefs: ItemSignature(DefId) -> ty::AdtDefMaster<'tcx> }
dep_map_ty! { ItemVariances: ItemSignature(DefId) -> Rc<ty::ItemVariances> }
dep_map_ty! { ItemVariances: ItemSignature(DefId) -> Rc<Vec<ty::Variance>> }
dep_map_ty! { InherentImpls: InherentImpls(DefId) -> Rc<Vec<DefId>> }
dep_map_ty! { ImplItems: ImplItems(DefId) -> Vec<ty::ImplOrTraitItemId> }
dep_map_ty! { TraitItems: TraitItems(DefId) -> Rc<Vec<ty::ImplOrTraitItem<'tcx>>> }
Expand Down
Loading

0 comments on commit 7a8d482

Please sign in to comment.