Skip to content

Commit 8e05e7e

Browse files
authored
Auto merge of rust-lang#37100 - dikaiosune:master, r=eddyb
Change Substs to type alias for Slice<Kind> for interning This changes the definition of `librustc::ty::subst::Substs` to be a type alias to `Slice<Kind>`. `Substs` was already interned, but can now make use of the efficient `PartialEq` and `Hash` impls on `librustc::ty::Slice`. I'm working on collecting some timing data for this, will update when it's done. I chose to leave the impls on `Substs<'tcx>` even though it's now just a type alias to `Slice<Kind<'tcx>>` because it has the smallest footprint on other portions of the compiler which depend on its API. It turns out to be a pretty huge diff if you change where Substs's methods live :smile:. That said, I'm not necessarily sure it's the *best* implementation but it's probably the easiest/smallest to review. Many thanks to @eddyb for both suggesting this as a project for learning more about the compiler, and the tireless ~~handholding~~ mentorship he provided.
2 parents 030bc49 + 48b3dd1 commit 8e05e7e

File tree

7 files changed

+70
-100
lines changed

7 files changed

+70
-100
lines changed

src/librustc/infer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use middle::mem_categorization as mc;
2525
use middle::mem_categorization::McResult;
2626
use middle::region::CodeExtent;
2727
use mir::tcx::LvalueTy;
28-
use ty::subst::{Subst, Substs};
28+
use ty::subst::{Kind, Subst, Substs};
2929
use ty::adjustment;
3030
use ty::{TyVid, IntVid, FloatVid};
3131
use ty::{self, Ty, TyCtxt};
@@ -1208,7 +1208,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
12081208
pub fn type_var_for_def(&self,
12091209
span: Span,
12101210
def: &ty::TypeParameterDef<'tcx>,
1211-
substs: &Substs<'tcx>)
1211+
substs: &[Kind<'tcx>])
12121212
-> Ty<'tcx> {
12131213
let default = def.default.map(|default| {
12141214
type_variable::Default {

src/librustc/ty/context.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use middle::free_region::FreeRegionMap;
2222
use middle::region::RegionMaps;
2323
use middle::resolve_lifetime;
2424
use middle::stability;
25-
use ty::subst::Substs;
25+
use ty::subst::{Kind, Substs};
2626
use traits;
2727
use ty::{self, TraitRef, Ty, TypeAndMut};
2828
use ty::{TyS, TypeVariants, Slice};
@@ -55,7 +55,7 @@ pub struct CtxtArenas<'tcx> {
5555
// internings
5656
type_: TypedArena<TyS<'tcx>>,
5757
type_list: TypedArena<Vec<Ty<'tcx>>>,
58-
substs: TypedArena<Substs<'tcx>>,
58+
substs: TypedArena<Vec<Kind<'tcx>>>,
5959
bare_fn: TypedArena<BareFnTy<'tcx>>,
6060
region: TypedArena<Region>,
6161
stability: TypedArena<attr::Stability>,
@@ -824,7 +824,7 @@ impl<'a, 'tcx> Lift<'tcx> for Ty<'a> {
824824
impl<'a, 'tcx> Lift<'tcx> for &'a Substs<'a> {
825825
type Lifted = &'tcx Substs<'tcx>;
826826
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<&'tcx Substs<'tcx>> {
827-
if let Some(&Interned(substs)) = tcx.interners.substs.borrow().get(*self) {
827+
if let Some(&Interned(substs)) = tcx.interners.substs.borrow().get(&self[..]) {
828828
if *self as *const _ == substs as *const _ {
829829
return Some(substs);
830830
}
@@ -1097,9 +1097,9 @@ impl<'tcx: 'lcx, 'lcx> Borrow<[Ty<'lcx>]> for Interned<'tcx, Slice<Ty<'tcx>>> {
10971097
}
10981098
}
10991099

1100-
impl<'tcx: 'lcx, 'lcx> Borrow<Substs<'lcx>> for Interned<'tcx, Substs<'tcx>> {
1101-
fn borrow<'a>(&'a self) -> &'a Substs<'lcx> {
1102-
self.0
1100+
impl<'tcx: 'lcx, 'lcx> Borrow<[Kind<'lcx>]> for Interned<'tcx, Substs<'tcx>> {
1101+
fn borrow<'a>(&'a self) -> &'a [Kind<'lcx>] {
1102+
&self.0[..]
11031103
}
11041104
}
11051105

@@ -1189,9 +1189,6 @@ fn keep_local<'tcx, T: ty::TypeFoldable<'tcx>>(x: &T) -> bool {
11891189
}
11901190

11911191
direct_interners!('tcx,
1192-
substs: mk_substs(|substs: &Substs| {
1193-
substs.params().iter().any(keep_local)
1194-
}) -> Substs<'tcx>,
11951192
bare_fn: mk_bare_fn(|fty: &BareFnTy| {
11961193
keep_local(&fty.sig)
11971194
}) -> BareFnTy<'tcx>,
@@ -1209,6 +1206,12 @@ intern_method!('tcx,
12091206
}, keep_local) -> Slice<Ty<'tcx>>
12101207
);
12111208

1209+
intern_method!('tcx,
1210+
substs: mk_substs(Vec<Kind<'tcx>>, Deref::deref, |xs: &[Kind]| -> &Slice<Kind> {
1211+
unsafe { mem::transmute(xs) }
1212+
}, keep_local) -> Slice<Kind<'tcx>>
1213+
);
1214+
12121215
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12131216
/// Create an unsafe fn ty based on a safe fn ty.
12141217
pub fn safe_to_unsafe_fn_ty(self, bare_fn: &BareFnTy<'tcx>) -> Ty<'tcx> {

src/librustc/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ pub type Ty<'tcx> = &'tcx TyS<'tcx>;
521521
impl<'tcx> serialize::UseSpecializedEncodable for Ty<'tcx> {}
522522
impl<'tcx> serialize::UseSpecializedDecodable for Ty<'tcx> {}
523523

524-
/// A wrapper for slices with the additioanl invariant
524+
/// A wrapper for slices with the additional invariant
525525
/// that the slice is interned and no other slice with
526526
/// the same contents can exist in the same context.
527527
/// This means we can use pointer + length for both

src/librustc/ty/subst.rs

+47-59
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// Type substitutions.
1212

1313
use hir::def_id::DefId;
14-
use ty::{self, Ty, TyCtxt};
14+
use ty::{self, Slice, Ty, TyCtxt};
1515
use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
1616

1717
use serialize::{self, Encodable, Encoder, Decodable, Decoder};
@@ -161,26 +161,19 @@ impl<'tcx> Decodable for Kind<'tcx> {
161161
}
162162

163163
/// A substitution mapping type/region parameters to new values.
164-
#[derive(Clone, PartialEq, Eq, Debug, Hash, RustcEncodable, RustcDecodable)]
165-
pub struct Substs<'tcx> {
166-
params: Vec<Kind<'tcx>>
167-
}
164+
pub type Substs<'tcx> = Slice<Kind<'tcx>>;
168165

169166
impl<'a, 'gcx, 'tcx> Substs<'tcx> {
170167
pub fn new<I>(tcx: TyCtxt<'a, 'gcx, 'tcx>, params: I)
171168
-> &'tcx Substs<'tcx>
172169
where I: IntoIterator<Item=Kind<'tcx>> {
173-
tcx.mk_substs(Substs {
174-
params: params.into_iter().collect()
175-
})
170+
tcx.mk_substs(params.into_iter().collect())
176171
}
177172

178173
pub fn maybe_new<I, E>(tcx: TyCtxt<'a, 'gcx, 'tcx>, params: I)
179174
-> Result<&'tcx Substs<'tcx>, E>
180175
where I: IntoIterator<Item=Result<Kind<'tcx>, E>> {
181-
Ok(tcx.mk_substs(Substs {
182-
params: params.into_iter().collect::<Result<_, _>>()?
183-
}))
176+
Ok(Substs::new(tcx, params.into_iter().collect::<Result<Vec<_>, _>>()?))
184177
}
185178

186179
pub fn new_trait(tcx: TyCtxt<'a, 'gcx, 'tcx>,
@@ -193,7 +186,7 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
193186
}
194187

195188
pub fn empty(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> &'tcx Substs<'tcx> {
196-
Substs::new(tcx, vec![])
189+
Substs::new(tcx, iter::empty())
197190
}
198191

199192
/// Creates a Substs for generic parameter definitions,
@@ -206,82 +199,82 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
206199
mut mk_region: FR,
207200
mut mk_type: FT)
208201
-> &'tcx Substs<'tcx>
209-
where FR: FnMut(&ty::RegionParameterDef, &Substs<'tcx>) -> &'tcx ty::Region,
210-
FT: FnMut(&ty::TypeParameterDef<'tcx>, &Substs<'tcx>) -> Ty<'tcx> {
202+
where FR: FnMut(&ty::RegionParameterDef, &[Kind<'tcx>]) -> &'tcx ty::Region,
203+
FT: FnMut(&ty::TypeParameterDef<'tcx>, &[Kind<'tcx>]) -> Ty<'tcx> {
211204
let defs = tcx.lookup_generics(def_id);
212-
let mut substs = Substs {
213-
params: Vec::with_capacity(defs.count())
214-
};
205+
let mut substs = Vec::with_capacity(defs.count());
215206

216-
substs.fill_item(tcx, defs, &mut mk_region, &mut mk_type);
207+
Substs::fill_item(&mut substs, tcx, defs, &mut mk_region, &mut mk_type);
217208

218-
tcx.mk_substs(substs)
209+
Substs::new(tcx, substs)
219210
}
220211

221-
fn fill_item<FR, FT>(&mut self,
212+
fn fill_item<FR, FT>(substs: &mut Vec<Kind<'tcx>>,
222213
tcx: TyCtxt<'a, 'gcx, 'tcx>,
223214
defs: &ty::Generics<'tcx>,
224215
mk_region: &mut FR,
225216
mk_type: &mut FT)
226-
where FR: FnMut(&ty::RegionParameterDef, &Substs<'tcx>) -> &'tcx ty::Region,
227-
FT: FnMut(&ty::TypeParameterDef<'tcx>, &Substs<'tcx>) -> Ty<'tcx> {
217+
where FR: FnMut(&ty::RegionParameterDef, &[Kind<'tcx>]) -> &'tcx ty::Region,
218+
FT: FnMut(&ty::TypeParameterDef<'tcx>, &[Kind<'tcx>]) -> Ty<'tcx> {
219+
228220
if let Some(def_id) = defs.parent {
229221
let parent_defs = tcx.lookup_generics(def_id);
230-
self.fill_item(tcx, parent_defs, mk_region, mk_type);
222+
Substs::fill_item(substs, tcx, parent_defs, mk_region, mk_type);
231223
}
232224

233225
// Handle Self first, before all regions.
234226
let mut types = defs.types.iter();
235227
if defs.parent.is_none() && defs.has_self {
236228
let def = types.next().unwrap();
237-
let ty = mk_type(def, self);
238-
assert_eq!(def.index as usize, self.params.len());
239-
self.params.push(Kind::from(ty));
229+
let ty = mk_type(def, substs);
230+
assert_eq!(def.index as usize, substs.len());
231+
substs.push(Kind::from(ty));
240232
}
241233

242234
for def in &defs.regions {
243-
let region = mk_region(def, self);
244-
assert_eq!(def.index as usize, self.params.len());
245-
self.params.push(Kind::from(region));
235+
let region = mk_region(def, substs);
236+
assert_eq!(def.index as usize, substs.len());
237+
substs.push(Kind::from(region));
246238
}
247239

248240
for def in types {
249-
let ty = mk_type(def, self);
250-
assert_eq!(def.index as usize, self.params.len());
251-
self.params.push(Kind::from(ty));
241+
let ty = mk_type(def, substs);
242+
assert_eq!(def.index as usize, substs.len());
243+
substs.push(Kind::from(ty));
252244
}
253245
}
254246

255247
pub fn is_noop(&self) -> bool {
256-
self.params.is_empty()
248+
self.is_empty()
257249
}
258250

259251
#[inline]
260252
pub fn params(&self) -> &[Kind<'tcx>] {
261-
&self.params
253+
// FIXME (dikaiosune) this should be removed, and corresponding compilation errors fixed
254+
self
262255
}
263256

264257
#[inline]
265258
pub fn types(&'a self) -> impl DoubleEndedIterator<Item=Ty<'tcx>> + 'a {
266-
self.params.iter().filter_map(|k| k.as_type())
259+
self.iter().filter_map(|k| k.as_type())
267260
}
268261

269262
#[inline]
270263
pub fn regions(&'a self) -> impl DoubleEndedIterator<Item=&'tcx ty::Region> + 'a {
271-
self.params.iter().filter_map(|k| k.as_region())
264+
self.iter().filter_map(|k| k.as_region())
272265
}
273266

274267
#[inline]
275268
pub fn type_at(&self, i: usize) -> Ty<'tcx> {
276-
self.params[i].as_type().unwrap_or_else(|| {
277-
bug!("expected type for param #{} in {:?}", i, self.params);
269+
self[i].as_type().unwrap_or_else(|| {
270+
bug!("expected type for param #{} in {:?}", i, self);
278271
})
279272
}
280273

281274
#[inline]
282275
pub fn region_at(&self, i: usize) -> &'tcx ty::Region {
283-
self.params[i].as_region().unwrap_or_else(|| {
284-
bug!("expected region for param #{} in {:?}", i, self.params);
276+
self[i].as_region().unwrap_or_else(|| {
277+
bug!("expected region for param #{} in {:?}", i, self);
285278
})
286279
}
287280

@@ -305,27 +298,22 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
305298
target_substs: &Substs<'tcx>)
306299
-> &'tcx Substs<'tcx> {
307300
let defs = tcx.lookup_generics(source_ancestor);
308-
tcx.mk_substs(Substs {
309-
params: target_substs.params.iter()
310-
.chain(&self.params[defs.own_count()..]).cloned().collect()
311-
})
301+
Substs::new(tcx, target_substs.iter().chain(&self[defs.own_count()..]).cloned())
312302
}
313303
}
314304

315305
impl<'tcx> TypeFoldable<'tcx> for &'tcx Substs<'tcx> {
316306
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
317-
let params = self.params.iter().map(|k| k.fold_with(folder)).collect();
318-
folder.tcx().mk_substs(Substs {
319-
params: params
320-
})
307+
let params = self.iter().map(|k| k.fold_with(folder)).collect();
308+
folder.tcx().mk_substs(params)
321309
}
322310

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

327315
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
328-
self.params.visit_with(visitor)
316+
self.iter().any(|t| t.visit_with(visitor))
329317
}
330318
}
331319

@@ -340,19 +328,19 @@ impl<'tcx> serialize::UseSpecializedDecodable for &'tcx Substs<'tcx> {}
340328

341329
pub trait Subst<'tcx> : Sized {
342330
fn subst<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>,
343-
substs: &Substs<'tcx>) -> Self {
331+
substs: &[Kind<'tcx>]) -> Self {
344332
self.subst_spanned(tcx, substs, None)
345333
}
346334

347335
fn subst_spanned<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>,
348-
substs: &Substs<'tcx>,
336+
substs: &[Kind<'tcx>],
349337
span: Option<Span>)
350338
-> Self;
351339
}
352340

353341
impl<'tcx, T:TypeFoldable<'tcx>> Subst<'tcx> for T {
354342
fn subst_spanned<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>,
355-
substs: &Substs<'tcx>,
343+
substs: &[Kind<'tcx>],
356344
span: Option<Span>)
357345
-> T
358346
{
@@ -371,7 +359,7 @@ impl<'tcx, T:TypeFoldable<'tcx>> Subst<'tcx> for T {
371359

372360
struct SubstFolder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
373361
tcx: TyCtxt<'a, 'gcx, 'tcx>,
374-
substs: &'a Substs<'tcx>,
362+
substs: &'a [Kind<'tcx>],
375363

376364
// The location for which the substitution is performed, if available.
377365
span: Option<Span>,
@@ -404,7 +392,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for SubstFolder<'a, 'gcx, 'tcx> {
404392
// the specialized routine `ty::replace_late_regions()`.
405393
match *r {
406394
ty::ReEarlyBound(data) => {
407-
let r = self.substs.params.get(data.index as usize)
395+
let r = self.substs.get(data.index as usize)
408396
.and_then(|k| k.as_region());
409397
match r {
410398
Some(r) => {
@@ -461,7 +449,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for SubstFolder<'a, 'gcx, 'tcx> {
461449
impl<'a, 'gcx, 'tcx> SubstFolder<'a, 'gcx, 'tcx> {
462450
fn ty_for_param(&self, p: ty::ParamTy, source_ty: Ty<'tcx>) -> Ty<'tcx> {
463451
// Look up the type in the substitutions. It really should be in there.
464-
let opt_ty = self.substs.params.get(p.idx as usize)
452+
let opt_ty = self.substs.get(p.idx as usize)
465453
.and_then(|k| k.as_type());
466454
let ty = match opt_ty {
467455
Some(t) => t,
@@ -475,7 +463,7 @@ impl<'a, 'gcx, 'tcx> SubstFolder<'a, 'gcx, 'tcx> {
475463
source_ty,
476464
p.idx,
477465
self.root_ty,
478-
self.substs.params);
466+
self.substs);
479467
}
480468
};
481469

@@ -552,7 +540,7 @@ impl<'a, 'gcx, 'tcx> ty::TraitRef<'tcx> {
552540
-> ty::TraitRef<'tcx> {
553541
let defs = tcx.lookup_generics(trait_id);
554542

555-
let params = substs.params[..defs.own_count()].iter().cloned();
543+
let params = substs[..defs.own_count()].iter().cloned();
556544
ty::TraitRef {
557545
def_id: trait_id,
558546
substs: Substs::new(tcx, params)
@@ -567,7 +555,7 @@ impl<'a, 'gcx, 'tcx> ty::ExistentialTraitRef<'tcx> {
567555
// Assert there is a Self.
568556
trait_ref.substs.type_at(0);
569557

570-
let params = trait_ref.substs.params[1..].iter().cloned();
558+
let params = trait_ref.substs[1..].iter().cloned();
571559
ty::ExistentialTraitRef {
572560
def_id: trait_ref.def_id,
573561
substs: Substs::new(tcx, params)
@@ -587,7 +575,7 @@ impl<'a, 'gcx, 'tcx> ty::PolyExistentialTraitRef<'tcx> {
587575
assert!(!self_ty.has_escaping_regions());
588576

589577
self.map_bound(|trait_ref| {
590-
let params = trait_ref.substs.params.iter().cloned();
578+
let params = trait_ref.substs.iter().cloned();
591579
let params = iter::once(Kind::from(self_ty)).chain(params);
592580
ty::TraitRef {
593581
def_id: trait_ref.def_id,

0 commit comments

Comments
 (0)