Skip to content

Commit 7a8d482

Browse files
committed
rustc: use Vec<Kind> in Substs, where Kind is a &TyS | &Region tagged pointer.
1 parent dffd238 commit 7a8d482

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+532
-409
lines changed

Diff for: src/librustc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#![feature(box_patterns)]
2828
#![feature(box_syntax)]
2929
#![feature(collections)]
30+
#![feature(conservative_impl_trait)]
3031
#![feature(const_fn)]
3132
#![feature(core_intrinsics)]
3233
#![feature(enumset)]

Diff for: src/librustc/middle/cstore.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pub trait CrateStore<'tcx> {
149149
fn closure_kind(&self, def_id: DefId) -> ty::ClosureKind;
150150
fn closure_ty<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
151151
-> ty::ClosureTy<'tcx>;
152-
fn item_variances(&self, def: DefId) -> ty::ItemVariances;
152+
fn item_variances(&self, def: DefId) -> Vec<ty::Variance>;
153153
fn repr_attrs(&self, def: DefId) -> Vec<attr::ReprAttr>;
154154
fn item_type<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
155155
-> Ty<'tcx>;
@@ -328,7 +328,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
328328
fn closure_kind(&self, def_id: DefId) -> ty::ClosureKind { bug!("closure_kind") }
329329
fn closure_ty<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
330330
-> ty::ClosureTy<'tcx> { bug!("closure_ty") }
331-
fn item_variances(&self, def: DefId) -> ty::ItemVariances { bug!("item_variances") }
331+
fn item_variances(&self, def: DefId) -> Vec<ty::Variance> { bug!("item_variances") }
332332
fn repr_attrs(&self, def: DefId) -> Vec<attr::ReprAttr> { bug!("repr_attrs") }
333333
fn item_type<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
334334
-> Ty<'tcx> { bug!("item_type") }

Diff for: src/librustc/middle/resolve_lifetime.rs

+35-19
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,12 @@ struct LifetimeContext<'a, 'tcx: 'a> {
8989

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

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

577+
// Find the start of nested early scopes, e.g. in methods.
578+
let mut start = 0;
579+
if let EarlyScope(..) = *self.scope {
580+
let parent = self.hir_map.expect_item(self.hir_map.get_parent(fn_id));
581+
if let hir::ItemTrait(..) = parent.node {
582+
start += 1; // Self comes first.
583+
}
584+
match parent.node {
585+
hir::ItemTrait(_, ref generics, _, _) |
586+
hir::ItemImpl(_, _, ref generics, _, _, _) => {
587+
start += generics.lifetimes.len() + generics.ty_params.len();
588+
}
589+
_ => {}
590+
}
591+
}
592+
569593
let this = self;
570-
this.with(EarlyScope(&early, this.scope), move |old_scope, this| {
594+
this.with(EarlyScope(&early, start as u32, this.scope), move |old_scope, this| {
571595
this.with(LateScope(&late, this.scope), move |_, this| {
572596
this.check_lifetime_defs(old_scope, &generics.lifetimes);
573597
walk(this);
@@ -597,19 +621,11 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
597621
break;
598622
}
599623

600-
EarlyScope(lifetimes, s) => {
624+
EarlyScope(lifetimes, start, s) => {
601625
match search_lifetimes(lifetimes, lifetime_ref) {
602-
Some((mut index, lifetime_def)) => {
603-
// Adjust for nested early scopes, e.g. in methods.
604-
let mut parent = s;
605-
while let EarlyScope(lifetimes, s) = *parent {
606-
index += lifetimes.len() as u32;
607-
parent = s;
608-
}
609-
assert_eq!(*parent, RootScope);
610-
626+
Some((index, lifetime_def)) => {
611627
let decl_id = lifetime_def.id;
612-
let def = DefEarlyBoundRegion(index, decl_id);
628+
let def = DefEarlyBoundRegion(start + index, decl_id);
613629
self.insert_lifetime(lifetime_ref, def);
614630
return;
615631
}
@@ -671,7 +687,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
671687
break;
672688
}
673689

674-
EarlyScope(lifetimes, s) |
690+
EarlyScope(lifetimes, _, s) |
675691
LateScope(lifetimes, s) => {
676692
search_result = search_lifetimes(lifetimes, lifetime_ref);
677693
if search_result.is_some() {
@@ -767,7 +783,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
767783
return;
768784
}
769785

770-
EarlyScope(lifetimes, s) |
786+
EarlyScope(lifetimes, _, s) |
771787
LateScope(lifetimes, s) => {
772788
if let Some((_, lifetime_def)) = search_lifetimes(lifetimes, lifetime) {
773789
signal_shadowing_problem(

Diff for: src/librustc/traits/fulfill.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl<'a, 'gcx, 'tcx> DeferredObligation<'tcx> {
162162
let concrete_ty = ty_scheme.ty.subst(tcx, substs);
163163
let predicate = ty::TraitRef {
164164
def_id: self.predicate.def_id(),
165-
substs: Substs::new_trait(tcx, vec![], vec![], concrete_ty)
165+
substs: Substs::new_trait(tcx, concrete_ty, &[])
166166
}.to_predicate();
167167

168168
let original_obligation = Obligation::new(self.cause.clone(),
@@ -440,7 +440,7 @@ fn trait_ref_type_vars<'a, 'gcx, 'tcx>(selcx: &mut SelectionContext<'a, 'gcx, 't
440440
{
441441
t.skip_binder() // ok b/c this check doesn't care about regions
442442
.input_types()
443-
.map(|t| selcx.infcx().resolve_type_vars_if_possible(t))
443+
.map(|t| selcx.infcx().resolve_type_vars_if_possible(&t))
444444
.filter(|t| t.has_infer_types())
445445
.flat_map(|t| t.walk())
446446
.filter(|t| match t.sty { ty::TyInfer(_) => true, _ => false })

Diff for: src/librustc/traits/select.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use super::util;
3636
use hir::def_id::DefId;
3737
use infer;
3838
use infer::{InferCtxt, InferOk, TypeFreshener, TypeOrigin};
39-
use ty::subst::{Subst, Substs};
39+
use ty::subst::{Kind, Subst, Substs};
4040
use ty::{self, ToPredicate, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable};
4141
use traits;
4242
use ty::fast_reject;
@@ -1933,7 +1933,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
19331933

19341934
// for `PhantomData<T>`, we pass `T`
19351935
ty::TyStruct(def, substs) if def.is_phantom_data() => {
1936-
substs.types().cloned().collect()
1936+
substs.types().collect()
19371937
}
19381938

19391939
ty::TyStruct(def, substs) | ty::TyEnum(def, substs) => {
@@ -1982,7 +1982,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
19821982
trait_def_id,
19831983
recursion_depth,
19841984
normalized_ty,
1985-
vec![]);
1985+
&[]);
19861986
obligations.push(skol_obligation);
19871987
this.infcx().plug_leaks(skol_map, snapshot, &obligations)
19881988
})
@@ -2180,8 +2180,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
21802180
let input_types = data.principal.input_types();
21812181
let assoc_types = data.projection_bounds.iter()
21822182
.map(|pb| pb.skip_binder().ty);
2183-
let all_types: Vec<_> = input_types.cloned()
2184-
.chain(assoc_types)
2183+
let all_types: Vec<_> = input_types.chain(assoc_types)
21852184
.collect();
21862185

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

26192618
// Check that the source structure with the target's
26202619
// type parameters is a subtype of the target.
2621-
let types = substs_a.types().enumerate().map(|(i, ty)| {
2620+
let params = substs_a.params().iter().enumerate().map(|(i, &k)| {
26222621
if ty_params.contains(i) {
2623-
substs_b.type_at(i)
2622+
Kind::from(substs_b.type_at(i))
26242623
} else {
2625-
ty
2624+
k
26262625
}
2627-
}).collect();
2628-
let substs = Substs::new(tcx, types, substs_a.regions().cloned().collect());
2629-
let new_struct = tcx.mk_struct(def, substs);
2626+
});
2627+
let new_struct = tcx.mk_struct(def, Substs::new(tcx, params));
26302628
let origin = TypeOrigin::Misc(obligation.cause.span);
26312629
let InferOk { obligations, .. } =
26322630
self.infcx.sub_types(false, origin, new_struct, target)
@@ -2639,7 +2637,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
26392637
obligation.predicate.def_id(),
26402638
obligation.recursion_depth + 1,
26412639
inner_source,
2642-
vec![inner_target]));
2640+
&[inner_target]));
26432641
}
26442642

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

27542752
obligation.predicate.skip_binder().input_types()
27552753
.zip(impl_trait_ref.input_types())
2756-
.any(|(&obligation_ty, &impl_ty)| {
2754+
.any(|(obligation_ty, impl_ty)| {
27572755
let simplified_obligation_ty =
27582756
fast_reject::simplify_type(self.tcx(), obligation_ty, true);
27592757
let simplified_impl_ty =

Diff for: src/librustc/traits/util.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
386386
Ok(def_id) => {
387387
Ok(ty::TraitRef {
388388
def_id: def_id,
389-
substs: Substs::new_trait(self, vec![], vec![], param_ty)
389+
substs: Substs::new_trait(self, param_ty, &[])
390390
})
391391
}
392392
Err(e) => {
@@ -401,12 +401,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
401401
trait_def_id: DefId,
402402
recursion_depth: usize,
403403
param_ty: Ty<'tcx>,
404-
ty_params: Vec<Ty<'tcx>>)
404+
ty_params: &[Ty<'tcx>])
405405
-> PredicateObligation<'tcx>
406406
{
407407
let trait_ref = ty::TraitRef {
408408
def_id: trait_def_id,
409-
substs: Substs::new_trait(self, ty_params, vec![], param_ty)
409+
substs: Substs::new_trait(self, param_ty, ty_params)
410410
};
411411
predicate_for_trait_ref(cause, trait_ref, recursion_depth)
412412
}
@@ -496,7 +496,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
496496
};
497497
let trait_ref = ty::TraitRef {
498498
def_id: fn_trait_def_id,
499-
substs: Substs::new_trait(self, vec![arguments_tuple], vec![], self_ty),
499+
substs: Substs::new_trait(self, self_ty, &[arguments_tuple]),
500500
};
501501
ty::Binder((trait_ref, sig.0.output))
502502
}

Diff for: src/librustc/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,7 @@ fn keep_local<'tcx, T: ty::TypeFoldable<'tcx>>(x: &T) -> bool {
11521152
impl_interners!('tcx,
11531153
type_list: mk_type_list(Vec<Ty<'tcx>>, keep_local) -> [Ty<'tcx>],
11541154
substs: mk_substs(Substs<'tcx>, |substs: &Substs| {
1155-
substs.types().any(keep_local) || substs.regions().any(keep_local)
1155+
substs.params().iter().any(keep_local)
11561156
}) -> Substs<'tcx>,
11571157
bare_fn: mk_bare_fn(BareFnTy<'tcx>, |fty: &BareFnTy| {
11581158
keep_local(&fty.sig)

Diff for: src/librustc/ty/flags.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,11 @@ impl FlagComputation {
208208
}
209209

210210
fn add_substs(&mut self, substs: &Substs) {
211-
for &ty in substs.types() {
211+
for ty in substs.types() {
212212
self.add_ty(ty);
213213
}
214214

215-
for &r in substs.regions() {
215+
for r in substs.regions() {
216216
self.add_region(r);
217217
}
218218
}

Diff for: src/librustc/ty/maps.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ dep_map_ty! { TraitItemDefIds: TraitItemDefIds(DefId) -> Rc<Vec<ty::ImplOrTraitI
3838
dep_map_ty! { ImplTraitRefs: ItemSignature(DefId) -> Option<ty::TraitRef<'tcx>> }
3939
dep_map_ty! { TraitDefs: ItemSignature(DefId) -> &'tcx ty::TraitDef<'tcx> }
4040
dep_map_ty! { AdtDefs: ItemSignature(DefId) -> ty::AdtDefMaster<'tcx> }
41-
dep_map_ty! { ItemVariances: ItemSignature(DefId) -> Rc<ty::ItemVariances> }
41+
dep_map_ty! { ItemVariances: ItemSignature(DefId) -> Rc<Vec<ty::Variance>> }
4242
dep_map_ty! { InherentImpls: InherentImpls(DefId) -> Rc<Vec<DefId>> }
4343
dep_map_ty! { ImplItems: ImplItems(DefId) -> Vec<ty::ImplOrTraitItemId> }
4444
dep_map_ty! { TraitItems: TraitItems(DefId) -> Rc<Vec<ty::ImplOrTraitItem<'tcx>>> }

0 commit comments

Comments
 (0)