Skip to content

Commit

Permalink
rustc: remove ParamSpace from Substs.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Aug 17, 2016
1 parent 6f5e455 commit 9453d9b
Show file tree
Hide file tree
Showing 60 changed files with 462 additions and 857 deletions.
4 changes: 2 additions & 2 deletions src/librustc/middle/dead.rs
Expand Up @@ -18,7 +18,7 @@ use hir::{self, pat_util, PatKind};
use hir::intravisit::{self, Visitor};

use middle::privacy;
use ty::{self, subst, TyCtxt};
use ty::{self, TyCtxt};
use hir::def::Def;
use hir::def_id::{DefId};
use lint;
Expand Down Expand Up @@ -95,7 +95,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
Def::AssociatedTy(..) | Def::Method(_) | Def::AssociatedConst(_)
if self.tcx.trait_of_item(def.def_id()).is_some() => {
if let Some(substs) = self.tcx.tables.borrow().item_substs.get(&id) {
match substs.substs.types.get(subst::TypeSpace, 0).sty {
match substs.substs.types[0].sty {
TyEnum(tyid, _) | TyStruct(tyid, _) => {
self.check_def_id(tyid.did)
}
Expand Down
60 changes: 24 additions & 36 deletions src/librustc/middle/resolve_lifetime.rs
Expand Up @@ -24,9 +24,7 @@ use session::Session;
use hir::def::{Def, DefMap};
use hir::def_id::DefId;
use middle::region;
use ty::subst;
use ty;
use std::fmt;
use std::mem::replace;
use syntax::ast;
use syntax::parse::token::keywords;
Expand All @@ -41,8 +39,7 @@ use hir::intravisit::{self, Visitor, FnKind};
#[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug)]
pub enum DefRegion {
DefStaticRegion,
DefEarlyBoundRegion(/* space */ subst::ParamSpace,
/* index */ u32,
DefEarlyBoundRegion(/* index */ u32,
/* lifetime decl */ ast::NodeId),
DefLateBoundRegion(ty::DebruijnIndex,
/* lifetime decl */ ast::NodeId),
Expand Down Expand Up @@ -90,10 +87,11 @@ struct LifetimeContext<'a, 'tcx: 'a> {
labels_in_fn: Vec<(ast::Name, Span)>,
}

#[derive(PartialEq, Debug)]
enum ScopeChain<'a> {
/// EarlyScope(i, ['a, 'b, ...], s) extends s with early-bound
/// lifetimes, assigning indexes 'a => i, 'b => i+1, ... etc.
EarlyScope(subst::ParamSpace, &'a [hir::LifetimeDef], Scope<'a>),
/// EarlyScope(['a, 'b, ...], s) extends s with early-bound
/// lifetimes.
EarlyScope(&'a [hir::LifetimeDef], 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 @@ -159,8 +157,7 @@ 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;
let early_scope = EarlyScope(subst::TypeSpace, lifetimes, &ROOT_SCOPE);
this.with(early_scope, |old_scope, this| {
this.with(EarlyScope(lifetimes, &ROOT_SCOPE), |old_scope, this| {
this.check_lifetime_defs(old_scope, lifetimes);
intravisit::walk_item(this, item);
});
Expand All @@ -181,11 +178,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for LifetimeContext<'a, 'tcx> {
self.with(RootScope, |_, this| {
match item.node {
hir::ForeignItemFn(ref decl, ref generics) => {
this.visit_early_late(item.id,
subst::FnSpace,
decl,
generics,
|this| {
this.visit_early_late(item.id, decl, generics, |this| {
intravisit::walk_foreign_item(this, item);
})
}
Expand All @@ -203,14 +196,13 @@ impl<'a, 'tcx, 'v> Visitor<'v> for LifetimeContext<'a, 'tcx> {
b: &'v hir::Block, s: Span, fn_id: ast::NodeId) {
match fk {
FnKind::ItemFn(_, generics, _, _, _, _, _) => {
self.visit_early_late(fn_id, subst::FnSpace, decl, generics, |this| {
self.visit_early_late(fn_id,decl, generics, |this| {
this.add_scope_and_walk_fn(fk, decl, b, s, fn_id)
})
}
FnKind::Method(_, sig, _, _) => {
self.visit_early_late(
fn_id,
subst::FnSpace,
decl,
&sig.generics,
|this| this.add_scope_and_walk_fn(fk, decl, b, s, fn_id));
Expand Down Expand Up @@ -263,7 +255,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for LifetimeContext<'a, 'tcx> {

if let hir::MethodTraitItem(ref sig, None) = trait_item.node {
self.visit_early_late(
trait_item.id, subst::FnSpace,
trait_item.id,
&sig.decl, &sig.generics,
|this| intravisit::walk_trait_item(this, trait_item))
} else {
Expand Down Expand Up @@ -469,7 +461,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 @@ -557,7 +549,6 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
/// ordering is not important there.
fn visit_early_late<F>(&mut self,
fn_id: ast::NodeId,
early_space: subst::ParamSpace,
decl: &hir::FnDecl,
generics: &hir::Generics,
walk: F) where
Expand All @@ -576,7 +567,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
.partition(|l| self.map.late_bound.contains_key(&l.lifetime.id));

let this = self;
this.with(EarlyScope(early_space, &early, this.scope), move |old_scope, this| {
this.with(EarlyScope(&early, 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 @@ -606,11 +597,19 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
break;
}

EarlyScope(space, lifetimes, s) => {
EarlyScope(lifetimes, s) => {
match search_lifetimes(lifetimes, lifetime_ref) {
Some((index, lifetime_def)) => {
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);

let decl_id = lifetime_def.id;
let def = DefEarlyBoundRegion(space, index, decl_id);
let def = DefEarlyBoundRegion(index, decl_id);
self.insert_lifetime(lifetime_ref, def);
return;
}
Expand Down Expand Up @@ -672,7 +671,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 @@ -768,7 +767,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 Expand Up @@ -963,14 +962,3 @@ fn insert_late_bound_lifetimes(map: &mut NamedRegionMap,
}
}
}

impl<'a> fmt::Debug for ScopeChain<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
EarlyScope(space, defs, _) => write!(fmt, "EarlyScope({:?}, {:?})", space, defs),
LateScope(defs, _) => write!(fmt, "LateScope({:?})", defs),
FnScope { fn_id, body_id, s: _ } => write!(fmt, "FnScope({:?}, {:?})", fn_id, body_id),
RootScope => write!(fmt, "RootScope"),
}
}
}
6 changes: 3 additions & 3 deletions src/librustc/traits/error_reporting.rs
Expand Up @@ -31,7 +31,7 @@ use ty::{self, ToPredicate, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable};
use ty::error::ExpectedFound;
use ty::fast_reject;
use ty::fold::TypeFolder;
use ty::subst::{Subst, TypeSpace};
use ty::subst::Subst;
use util::nodemap::{FnvHashMap, FnvHashSet};

use std::cmp;
Expand Down Expand Up @@ -232,8 +232,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
if let Ok(..) = self.can_equate(&trait_self_ty, &impl_self_ty) {
self_match_impls.push(def_id);

if trait_ref.substs.types.get_slice(TypeSpace)[1..].iter()
.zip(&impl_trait_ref.substs.types.get_slice(TypeSpace)[1..])
if trait_ref.substs.types[1..].iter()
.zip(&impl_trait_ref.substs.types[1..])
.all(|(u,v)| self.fuzzy_match_tys(u, v))
{
fuzzy_match_impls.push(def_id);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/fulfill.rs
Expand Up @@ -142,7 +142,7 @@ impl<'a, 'gcx, 'tcx> DeferredObligation<'tcx> {
// Auto trait obligations on `impl Trait`.
if tcx.trait_has_default_impl(predicate.def_id()) {
let substs = predicate.skip_binder().trait_ref.substs;
if substs.types.as_full_slice().len() == 1 && substs.regions.is_empty() {
if substs.types.len() == 1 && substs.regions.is_empty() {
if let ty::TyAnon(..) = predicate.skip_binder().self_ty().sty {
return true;
}
Expand Down
17 changes: 8 additions & 9 deletions src/librustc/traits/select.rs
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, TypeSpace};
use ty::subst::{Subst, Substs};
use ty::{self, ToPredicate, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable};
use traits;
use ty::fast_reject;
Expand Down Expand Up @@ -1936,7 +1936,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.as_full_slice().to_vec()
substs.types.to_vec()
}

ty::TyStruct(def, substs) | ty::TyEnum(def, substs) => {
Expand Down Expand Up @@ -2585,11 +2585,10 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
} else {
return Err(Unimplemented);
};
let mut ty_params = BitVector::new(substs_a.types.len(TypeSpace));
let mut ty_params = BitVector::new(substs_a.types.len());
let mut found = false;
for ty in field.walk() {
if let ty::TyParam(p) = ty.sty {
assert!(p.space == TypeSpace);
ty_params.insert(p.idx as usize);
found = true;
}
Expand All @@ -2602,13 +2601,13 @@ 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.map_enumerated(|(_, i, ty)| {
let types = substs_a.types.iter().enumerate().map(|(i, ty)| {
if ty_params.contains(i) {
tcx.types.err
} else {
ty
}
});
}).collect();
let substs = Substs::new(tcx, types, substs_a.regions.clone());
for &ty in fields.split_last().unwrap().1 {
if ty.subst(tcx, substs).references_error() {
Expand All @@ -2622,13 +2621,13 @@ 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.map_enumerated(|(_, i, ty)| {
let types = substs_a.types.iter().enumerate().map(|(i, ty)| {
if ty_params.contains(i) {
*substs_b.types.get(TypeSpace, i)
substs_b.types[i]
} else {
ty
}
});
}).collect();
let substs = Substs::new(tcx, types, substs_a.regions.clone());
let new_struct = tcx.mk_struct(def, substs);
let origin = TypeOrigin::Misc(obligation.cause.span);
Expand Down
9 changes: 4 additions & 5 deletions src/librustc/traits/specialize/mod.rs
Expand Up @@ -44,11 +44,10 @@ pub struct OverlapError {
/// When we have selected one impl, but are actually using item definitions from
/// a parent impl providing a default, we need a way to translate between the
/// type parameters of the two impls. Here the `source_impl` is the one we've
/// selected, and `source_substs` is a substitution of its generics (and
/// possibly some relevant `FnSpace` variables as well). And `target_node` is
/// the impl/trait we're actually going to get the definition from. The resulting
/// substitution will map from `target_node`'s generics to `source_impl`'s
/// generics as instantiated by `source_subst`.
/// selected, and `source_substs` is a substitution of its generics.
/// And `target_node` is the impl/trait we're actually going to get the
/// definition from. The resulting substitution will map from `target_node`'s
/// generics to `source_impl`'s generics as instantiated by `source_subst`.
///
/// For example, consider the following scenario:
///
Expand Down
9 changes: 4 additions & 5 deletions src/librustc/ty/context.rs
Expand Up @@ -23,7 +23,7 @@ use middle::free_region::FreeRegionMap;
use middle::region::RegionMaps;
use middle::resolve_lifetime;
use middle::stability;
use ty::subst::{self, Substs};
use ty::subst::Substs;
use traits;
use ty::{self, TraitRef, Ty, TypeAndMut};
use ty::{TyS, TypeVariants};
Expand Down Expand Up @@ -1346,18 +1346,17 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}

pub fn mk_param(self,
space: subst::ParamSpace,
index: u32,
name: Name) -> Ty<'tcx> {
self.mk_ty(TyParam(ParamTy { space: space, idx: index, name: name }))
self.mk_ty(TyParam(ParamTy { idx: index, name: name }))
}

pub fn mk_self_type(self) -> Ty<'tcx> {
self.mk_param(subst::TypeSpace, 0, keywords::SelfType.name())
self.mk_param(0, keywords::SelfType.name())
}

pub fn mk_param_from_def(self, def: &ty::TypeParameterDef) -> Ty<'tcx> {
self.mk_param(def.space, def.index, def.name)
self.mk_param(def.index, def.name)
}

pub fn mk_anon(self, def_id: DefId, substs: &'tcx Substs<'tcx>) -> Ty<'tcx> {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/flags.rs
Expand Up @@ -208,8 +208,8 @@ impl FlagComputation {
}

fn add_substs(&mut self, substs: &Substs) {
self.add_tys(substs.types.as_full_slice());
for &r in substs.regions.as_full_slice() {
self.add_tys(&substs.types);
for &r in &substs.regions {
self.add_region(r);
}
}
Expand Down

0 comments on commit 9453d9b

Please sign in to comment.