Skip to content

Commit

Permalink
rustc: use accessors for Substs::{types,regions}.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Aug 26, 2016
1 parent eaf71f8 commit 5222fa5
Show file tree
Hide file tree
Showing 43 changed files with 250 additions and 229 deletions.
2 changes: 1 addition & 1 deletion src/librustc/middle/dead.rs
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[0].sty {
match substs.substs.type_at(0).sty {
TyEnum(tyid, _) | TyStruct(tyid, _) => {
self.check_def_id(tyid.did)
}
Expand Down
7 changes: 3 additions & 4 deletions src/librustc/traits/error_reporting.rs
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[1..].iter()
.zip(&impl_trait_ref.substs.types[1..])
if trait_ref.substs.types().skip(1)
.zip(impl_trait_ref.substs.types().skip(1))
.all(|(u,v)| self.fuzzy_match_tys(u, v))
{
fuzzy_match_impls.push(def_id);
Expand Down Expand Up @@ -738,8 +738,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
ty::Predicate::Trait(ref data) => {
let trait_ref = data.to_poly_trait_ref();
let self_ty = trait_ref.self_ty();
let all_types = &trait_ref.substs().types;
if all_types.references_error() {
if predicate.references_error() {
} else {
// Typically, this ambiguity should only happen if
// there are unresolved type inference variables
Expand Down
3 changes: 1 addition & 2 deletions 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.len() == 1 && substs.regions.is_empty() {
if substs.types().count() == 1 && substs.regions().next().is_none() {
if let ty::TyAnon(..) = predicate.skip_binder().self_ty().sty {
return true;
}
Expand Down Expand Up @@ -440,7 +440,6 @@ 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()
.iter()
.map(|t| selcx.infcx().resolve_type_vars_if_possible(t))
.filter(|t| t.has_infer_types())
.flat_map(|t| t.walk())
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/object_safety.rs
Expand Up @@ -145,7 +145,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
match predicate {
ty::Predicate::Trait(ref data) => {
// In the case of a trait predicate, we can skip the "self" type.
data.0.trait_ref.input_types()[1..].iter().any(|t| t.has_self_ty())
data.skip_binder().input_types().skip(1).any(|t| t.has_self_ty())
}
ty::Predicate::Projection(..) |
ty::Predicate::WellFormed(..) |
Expand Down
35 changes: 16 additions & 19 deletions src/librustc/traits/select.rs
Expand Up @@ -644,8 +644,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
// This suffices to allow chains like `FnMut` implemented in
// terms of `Fn` etc, but we could probably make this more
// precise still.
let input_types = stack.fresh_trait_ref.0.input_types();
let unbound_input_types = input_types.iter().any(|ty| ty.is_fresh());
let unbound_input_types = stack.fresh_trait_ref.input_types().any(|ty| ty.is_fresh());
if unbound_input_types && self.intercrate {
debug!("evaluate_stack({:?}) --> unbound argument, intercrate --> ambiguous",
stack.fresh_trait_ref);
Expand Down Expand Up @@ -1064,9 +1063,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {

match *candidate {
Ok(Some(_)) | Err(_) => true,
Ok(None) => {
cache_fresh_trait_pred.0.trait_ref.substs.types.has_infer_types()
}
Ok(None) => cache_fresh_trait_pred.has_infer_types()
}
}

Expand Down Expand Up @@ -1603,7 +1600,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
return;
}
};
let target = obligation.predicate.skip_binder().input_types()[1];
let target = obligation.predicate.skip_binder().trait_ref.substs.type_at(1);

debug!("assemble_candidates_for_unsizing(source={:?}, target={:?})",
source, target);
Expand Down Expand Up @@ -1936,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.to_vec()
substs.types().cloned().collect()
}

ty::TyStruct(def, substs) | ty::TyEnum(def, substs) => {
Expand Down Expand Up @@ -2180,12 +2177,12 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
match self_ty.sty {
ty::TyTrait(ref data) => {
// OK to skip the binder, it is reintroduced below
let input_types = data.principal.skip_binder().input_types();
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.iter().cloned()
.chain(assoc_types)
.collect();
let all_types: Vec<_> = input_types.cloned()
.chain(assoc_types)
.collect();

// reintroduce the two binding levels we skipped, then flatten into one
let all_types = ty::Binder(ty::Binder(all_types));
Expand Down Expand Up @@ -2476,7 +2473,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
// regions here. See the comment there for more details.
let source = self.infcx.shallow_resolve(
tcx.no_late_bound_regions(&obligation.self_ty()).unwrap());
let target = obligation.predicate.skip_binder().input_types()[1];
let target = obligation.predicate.skip_binder().trait_ref.substs.type_at(1);
let target = self.infcx.shallow_resolve(target);

debug!("confirm_builtin_unsize_candidate(source={:?}, target={:?})",
Expand Down Expand Up @@ -2585,7 +2582,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
} else {
return Err(Unimplemented);
};
let mut ty_params = BitVector::new(substs_a.types.len());
let mut ty_params = BitVector::new(substs_a.types().count());
let mut found = false;
for ty in field.walk() {
if let ty::TyParam(p) = ty.sty {
Expand All @@ -2601,14 +2598,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.iter().enumerate().map(|(i, ty)| {
let types = substs_a.types().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());
let substs = Substs::new(tcx, types, substs_a.regions().cloned().collect());
for &ty in fields.split_last().unwrap().1 {
if ty.subst(tcx, substs).references_error() {
return Err(Unimplemented);
Expand All @@ -2621,14 +2618,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.iter().enumerate().map(|(i, ty)| {
let types = substs_a.types().enumerate().map(|(i, ty)| {
if ty_params.contains(i) {
substs_b.types[i]
substs_b.type_at(i)
} else {
ty
}
}).collect();
let substs = Substs::new(tcx, types, substs_a.regions.clone());
let substs = Substs::new(tcx, types, substs_a.regions().cloned().collect());
let new_struct = tcx.mk_struct(def, substs);
let origin = TypeOrigin::Misc(obligation.cause.span);
let InferOk { obligations, .. } =
Expand Down Expand Up @@ -2753,7 +2750,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
// substitution if we find that any of the input types, when
// simplified, do not match.

obligation.predicate.0.input_types().iter()
obligation.predicate.skip_binder().input_types()
.zip(impl_trait_ref.input_types())
.any(|(&obligation_ty, &impl_ty)| {
let simplified_obligation_ty =
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/context.rs
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| {
keep_local(&substs.types) || keep_local(&substs.regions)
substs.types().any(keep_local) || substs.regions().any(keep_local)
}) -> Substs<'tcx>,
bare_fn: mk_bare_fn(BareFnTy<'tcx>, |fty: &BareFnTy| {
keep_local(&fty.sig)
Expand Down
7 changes: 5 additions & 2 deletions src/librustc/ty/flags.rs
Expand Up @@ -208,8 +208,11 @@ impl FlagComputation {
}

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

for &r in substs.regions() {
self.add_region(r);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/item_path.rs
Expand Up @@ -264,7 +264,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
match self_ty.sty {
ty::TyStruct(adt_def, substs) |
ty::TyEnum(adt_def, substs) => {
if substs.types.is_empty() { // ignore regions
if substs.types().next().is_none() { // ignore regions
self.push_item_path(buffer, adt_def.did);
} else {
buffer.push(&format!("<{}>", self_ty));
Expand Down
16 changes: 7 additions & 9 deletions src/librustc/ty/mod.rs
Expand Up @@ -951,7 +951,6 @@ impl<'tcx> TraitPredicate<'tcx> {
// leads to more recompilation.
let def_ids: Vec<_> =
self.input_types()
.iter()
.flat_map(|t| t.walk())
.filter_map(|t| match t.sty {
ty::TyStruct(adt_def, _) |
Expand All @@ -964,8 +963,8 @@ impl<'tcx> TraitPredicate<'tcx> {
DepNode::TraitSelect(self.def_id(), def_ids)
}

pub fn input_types(&self) -> &[Ty<'tcx>] {
&self.trait_ref.substs.types
pub fn input_types(&self) -> slice::Iter<Ty<'tcx>> {
self.trait_ref.input_types()
}

pub fn self_ty(&self) -> Ty<'tcx> {
Expand Down Expand Up @@ -1107,7 +1106,7 @@ impl<'tcx> Predicate<'tcx> {
pub fn walk_tys(&self) -> IntoIter<Ty<'tcx>> {
let vec: Vec<_> = match *self {
ty::Predicate::Trait(ref data) => {
data.0.trait_ref.input_types().to_vec()
data.skip_binder().input_types().cloned().collect()
}
ty::Predicate::Rfc1592(ref data) => {
return data.walk_tys()
Expand All @@ -1123,8 +1122,7 @@ impl<'tcx> Predicate<'tcx> {
}
ty::Predicate::Projection(ref data) => {
let trait_inputs = data.0.projection_ty.trait_ref.input_types();
trait_inputs.iter()
.cloned()
trait_inputs.cloned()
.chain(Some(data.0.ty))
.collect()
}
Expand Down Expand Up @@ -1206,15 +1204,15 @@ impl<'tcx> TraitRef<'tcx> {
}

pub fn self_ty(&self) -> Ty<'tcx> {
self.substs.types[0]
self.substs.type_at(0)
}

pub fn input_types(&self) -> &[Ty<'tcx>] {
pub fn input_types(&self) -> slice::Iter<Ty<'tcx>> {
// Select only the "input types" from a trait-reference. For
// now this is all the types that appear in the
// trait-reference, but it should eventually exclude
// associated types.
&self.substs.types
self.substs.types()
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/librustc/ty/relate.rs
Expand Up @@ -147,14 +147,12 @@ pub fn relate_substs<'a, 'gcx, 'tcx, R>(relation: &mut R,
{
let tcx = relation.tcx();

let types = a_subst.types.iter().enumerate().map(|(i, a_ty)| {
let b_ty = &b_subst.types[i];
let types = a_subst.types().zip(b_subst.types()).enumerate().map(|(i, (a_ty, b_ty))| {
let variance = variances.map_or(ty::Invariant, |v| v.types[i]);
relation.relate_with_variance(variance, a_ty, b_ty)
}).collect::<Result<_, _>>()?;

let regions = a_subst.regions.iter().enumerate().map(|(i, a_r)| {
let b_r = &b_subst.regions[i];
let regions = a_subst.regions().zip(b_subst.regions()).enumerate().map(|(i, (a_r, b_r))| {
let variance = variances.map_or(ty::Invariant, |v| v.regions[i]);
relation.relate_with_variance(variance, a_r, b_r)
}).collect::<Result<_, _>>()?;
Expand Down
17 changes: 0 additions & 17 deletions src/librustc/ty/structural_impls.rs
Expand Up @@ -9,7 +9,6 @@
// except according to those terms.

use infer::type_variable;
use ty::subst::Substs;
use ty::{self, Lift, Ty, TyCtxt};
use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};

Expand Down Expand Up @@ -692,22 +691,6 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Region {
}
}

impl<'tcx> TypeFoldable<'tcx> for &'tcx Substs<'tcx> {
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
let types = self.types.fold_with(folder);
let regions = self.regions.fold_with(folder);
Substs::new(folder.tcx(), types, regions)
}

fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
folder.fold_substs(self)
}

fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
self.types.visit_with(visitor) || self.regions.visit_with(visitor)
}
}

impl<'tcx> TypeFoldable<'tcx> for ty::ClosureSubsts<'tcx> {
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
ty::ClosureSubsts {
Expand Down
19 changes: 10 additions & 9 deletions src/librustc/ty/sty.rs
Expand Up @@ -19,8 +19,9 @@ use util::common::ErrorReported;

use collections::enum_set::{self, EnumSet, CLike};
use std::fmt;
use std::ops;
use std::mem;
use std::ops;
use std::slice;
use syntax::abi;
use syntax::ast::{self, Name};
use syntax::parse::token::keywords;
Expand Down Expand Up @@ -335,7 +336,7 @@ impl<'tcx> PolyTraitRef<'tcx> {
self.0.substs
}

pub fn input_types(&self) -> &[Ty<'tcx>] {
pub fn input_types(&self) -> slice::Iter<Ty<'tcx>> {
// FIXME(#20664) every use of this fn is probably a bug, it should yield Binder<>
self.0.input_types()
}
Expand All @@ -360,12 +361,12 @@ pub struct ExistentialTraitRef<'tcx> {
}

impl<'tcx> ExistentialTraitRef<'tcx> {
pub fn input_types(&self) -> &[Ty<'tcx>] {
pub fn input_types(&self) -> slice::Iter<Ty<'tcx>>{
// Select only the "input types" from a trait-reference. For
// now this is all the types that appear in the
// trait-reference, but it should eventually exclude
// associated types.
&self.substs.types
self.substs.types()
}
}

Expand All @@ -376,7 +377,7 @@ impl<'tcx> PolyExistentialTraitRef<'tcx> {
self.0.def_id
}

pub fn input_types(&self) -> &[Ty<'tcx>] {
pub fn input_types(&self) -> slice::Iter<Ty<'tcx>> {
// FIXME(#20664) every use of this fn is probably a bug, it should yield Binder<>
self.0.input_types()
}
Expand Down Expand Up @@ -1213,19 +1214,19 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
}
TyTrait(ref obj) => {
let mut v = vec![obj.region_bound];
v.extend_from_slice(&obj.principal.skip_binder().substs.regions);
v.extend(obj.principal.skip_binder().substs.regions());
v
}
TyEnum(_, substs) |
TyStruct(_, substs) |
TyAnon(_, substs) => {
substs.regions.to_vec()
substs.regions().cloned().collect()
}
TyClosure(_, ref substs) => {
substs.func_substs.regions.to_vec()
substs.func_substs.regions().cloned().collect()
}
TyProjection(ref data) => {
data.trait_ref.substs.regions.to_vec()
data.trait_ref.substs.regions().cloned().collect()
}
TyFnDef(..) |
TyFnPtr(_) |
Expand Down

0 comments on commit 5222fa5

Please sign in to comment.