Skip to content

Commit 7a2a381

Browse files
authored
Rollup merge of rust-lang#36002 - eddyb:abstract-kindness, r=nikomatsakis
Combine types and regions in Substs into one interleaved list. Previously, `Substs` would contain types and regions, in two separate vectors, for example: ```rust <X as Trait<'a, 'b, A, B>>::method::<'p, 'q, T, U> /* corresponds to */ Substs { regions: ['a, 'b, 'p, 'q], types: [X, A, B, T, U] } ``` This PR continues the work started in rust-lang#35605 by further removing the distinction. A new abstraction over types and regions is introduced in the compiler, `Kind`. Each `Kind` is a pointer (`&TyS` or `&Region`), with the lowest two bits used as a tag. Two bits were used instead of just one (type = `0`, region = `1`) to allow adding more kinds. `Substs` contain only a `Vec<Kind>`, with `Self` first, followed by regions and types (in the definition order): ```rust Substs { params: [X, 'a, 'b, A, B, 'p, 'q, T, U] } ``` The resulting interleaved list has the property of being the concatenation of parameters for the (potentially) nested generic items it describes, and can be sliced back into those components: ```rust params[0..5] = [X, 'a, 'b, A, B] // <X as Trait<'a, 'b, A, B>> params[5..9] = ['p, 'q, T, U] // <_>::method::<'p, 'q, T, U> ``` r? @nikomatsakis
2 parents 1385feb + 7a8d482 commit 7a2a381

File tree

117 files changed

+1416
-1225
lines changed

Some content is hidden

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

117 files changed

+1416
-1225
lines changed

src/librustc/infer/bivariate.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
106106
}
107107
}
108108

109-
fn regions(&mut self, a: ty::Region, _: ty::Region) -> RelateResult<'tcx, ty::Region> {
109+
fn regions(&mut self, a: &'tcx ty::Region, _: &'tcx ty::Region)
110+
-> RelateResult<'tcx, &'tcx ty::Region> {
110111
Ok(a)
111112
}
112113

src/librustc/infer/combine.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ impl<'cx, 'gcx, 'tcx> ty::fold::TypeFolder<'gcx, 'tcx> for Generalizer<'cx, 'gcx
329329
}
330330
}
331331

332-
fn fold_region(&mut self, r: ty::Region) -> ty::Region {
333-
match r {
332+
fn fold_region(&mut self, r: &'tcx ty::Region) -> &'tcx ty::Region {
333+
match *r {
334334
// Never make variables for regions bound within the type itself,
335335
// nor for erased regions.
336336
ty::ReLateBound(..) |

src/librustc/infer/equate.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
7979
}
8080
}
8181

82-
fn regions(&mut self, a: ty::Region, b: ty::Region) -> RelateResult<'tcx, ty::Region> {
82+
fn regions(&mut self, a: &'tcx ty::Region, b: &'tcx ty::Region)
83+
-> RelateResult<'tcx, &'tcx ty::Region> {
8384
debug!("{}.regions({:?}, {:?})",
8485
self.tag(),
8586
a,

src/librustc/infer/error_reporting.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
9999
pub fn note_and_explain_region(self,
100100
err: &mut DiagnosticBuilder,
101101
prefix: &str,
102-
region: ty::Region,
102+
region: &'tcx ty::Region,
103103
suffix: &str) {
104104
fn item_scope_tag(item: &hir::Item) -> &'static str {
105105
match item.node {
@@ -120,7 +120,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
120120
Some(span))
121121
}
122122

123-
let (description, span) = match region {
123+
let (description, span) = match *region {
124124
ty::ReScope(scope) => {
125125
let new_string;
126126
let unknown_scope = || {
@@ -405,12 +405,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
405405
}
406406

407407
fn free_regions_from_same_fn<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
408-
sub: Region,
409-
sup: Region)
408+
sub: &'tcx Region,
409+
sup: &'tcx Region)
410410
-> Option<FreeRegionsFromSameFn> {
411411
debug!("free_regions_from_same_fn(sub={:?}, sup={:?})", sub, sup);
412412
let (scope_id, fr1, fr2) = match (sub, sup) {
413-
(ReFree(fr1), ReFree(fr2)) => {
413+
(&ReFree(fr1), &ReFree(fr2)) => {
414414
if fr1.scope != fr2.scope {
415415
return None
416416
}
@@ -602,7 +602,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
602602
fn report_generic_bound_failure(&self,
603603
origin: SubregionOrigin<'tcx>,
604604
bound_kind: GenericKind<'tcx>,
605-
sub: Region)
605+
sub: &'tcx Region)
606606
{
607607
// FIXME: it would be better to report the first error message
608608
// with the span of the parameter itself, rather than the span
@@ -616,7 +616,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
616616
format!("the associated type `{}`", p),
617617
};
618618

619-
let mut err = match sub {
619+
let mut err = match *sub {
620620
ty::ReFree(ty::FreeRegion {bound_region: ty::BrNamed(..), ..}) => {
621621
// Does the required lifetime have a nice name we can print?
622622
let mut err = struct_span_err!(self.tcx.sess,
@@ -667,8 +667,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
667667

668668
fn report_concrete_failure(&self,
669669
origin: SubregionOrigin<'tcx>,
670-
sub: Region,
671-
sup: Region)
670+
sub: &'tcx Region,
671+
sup: &'tcx Region)
672672
-> DiagnosticBuilder<'tcx> {
673673
match origin {
674674
infer::Subtype(trace) => {
@@ -939,9 +939,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
939939
fn report_sub_sup_conflict(&self,
940940
var_origin: RegionVariableOrigin,
941941
sub_origin: SubregionOrigin<'tcx>,
942-
sub_region: Region,
942+
sub_region: &'tcx Region,
943943
sup_origin: SubregionOrigin<'tcx>,
944-
sup_region: Region) {
944+
sup_region: &'tcx Region) {
945945
let mut err = self.report_inference_failure(var_origin);
946946

947947
self.tcx.note_and_explain_region(&mut err,

src/librustc/infer/freshen.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
8383
self.infcx.tcx
8484
}
8585

86-
fn fold_region(&mut self, r: ty::Region) -> ty::Region {
87-
match r {
86+
fn fold_region(&mut self, r: &'tcx ty::Region) -> &'tcx ty::Region {
87+
match *r {
8888
ty::ReEarlyBound(..) |
8989
ty::ReLateBound(..) => {
9090
// leave bound regions alone
@@ -99,7 +99,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
9999
ty::ReEmpty |
100100
ty::ReErased => {
101101
// replace all free regions with 'erased
102-
ty::ReErased
102+
self.tcx().mk_region(ty::ReErased)
103103
}
104104
}
105105
}

src/librustc/infer/glb.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
5757
lattice::super_lattice_tys(self, a, b)
5858
}
5959

60-
fn regions(&mut self, a: ty::Region, b: ty::Region) -> RelateResult<'tcx, ty::Region> {
60+
fn regions(&mut self, a: &'tcx ty::Region, b: &'tcx ty::Region)
61+
-> RelateResult<'tcx, &'tcx ty::Region> {
6162
debug!("{}.regions({:?}, {:?})",
6263
self.tag(),
6364
a,

src/librustc/infer/higher_ranked/mod.rs

+36-30
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
164164
.map(|(&skol, &(br, ref regions))| {
165165
let representative =
166166
regions.iter()
167-
.filter(|r| !skol_resolution_map.contains_key(r))
167+
.filter(|&&r| !skol_resolution_map.contains_key(r))
168168
.cloned()
169169
.next()
170170
.unwrap_or_else(|| { // [1]
@@ -268,9 +268,9 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
268268
snapshot: &CombinedSnapshot,
269269
debruijn: ty::DebruijnIndex,
270270
new_vars: &[ty::RegionVid],
271-
a_map: &FnvHashMap<ty::BoundRegion, ty::Region>,
272-
r0: ty::Region)
273-
-> ty::Region {
271+
a_map: &FnvHashMap<ty::BoundRegion, &'tcx ty::Region>,
272+
r0: &'tcx ty::Region)
273+
-> &'tcx ty::Region {
274274
// Regions that pre-dated the LUB computation stay as they are.
275275
if !is_var_in_set(new_vars, r0) {
276276
assert!(!r0.is_bound());
@@ -301,7 +301,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
301301
debug!("generalize_region(r0={:?}): \
302302
replacing with {:?}, tainted={:?}",
303303
r0, *a_br, tainted);
304-
return ty::ReLateBound(debruijn, *a_br);
304+
return infcx.tcx.mk_region(ty::ReLateBound(debruijn, *a_br));
305305
}
306306
}
307307

@@ -364,10 +364,12 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
364364
snapshot: &CombinedSnapshot,
365365
debruijn: ty::DebruijnIndex,
366366
new_vars: &[ty::RegionVid],
367-
a_map: &FnvHashMap<ty::BoundRegion, ty::Region>,
367+
a_map: &FnvHashMap<ty::BoundRegion,
368+
&'tcx ty::Region>,
368369
a_vars: &[ty::RegionVid],
369370
b_vars: &[ty::RegionVid],
370-
r0: ty::Region) -> ty::Region {
371+
r0: &'tcx ty::Region)
372+
-> &'tcx ty::Region {
371373
if !is_var_in_set(new_vars, r0) {
372374
assert!(!r0.is_bound());
373375
return r0;
@@ -419,7 +421,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
419421

420422
if a_r.is_some() && b_r.is_some() && only_new_vars {
421423
// Related to exactly one bound variable from each fn:
422-
return rev_lookup(span, a_map, a_r.unwrap());
424+
return rev_lookup(infcx, span, a_map, a_r.unwrap());
423425
} else if a_r.is_none() && b_r.is_none() {
424426
// Not related to bound variables from either fn:
425427
assert!(!r0.is_bound());
@@ -430,13 +432,14 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
430432
}
431433
}
432434

433-
fn rev_lookup(span: Span,
434-
a_map: &FnvHashMap<ty::BoundRegion, ty::Region>,
435-
r: ty::Region) -> ty::Region
435+
fn rev_lookup<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
436+
span: Span,
437+
a_map: &FnvHashMap<ty::BoundRegion, &'tcx ty::Region>,
438+
r: &'tcx ty::Region) -> &'tcx ty::Region
436439
{
437440
for (a_br, a_r) in a_map {
438441
if *a_r == r {
439-
return ty::ReLateBound(ty::DebruijnIndex::new(1), *a_br);
442+
return infcx.tcx.mk_region(ty::ReLateBound(ty::DebruijnIndex::new(1), *a_br));
440443
}
441444
}
442445
span_bug!(
@@ -445,19 +448,21 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
445448
r);
446449
}
447450

448-
fn fresh_bound_variable(infcx: &InferCtxt, debruijn: ty::DebruijnIndex) -> ty::Region {
451+
fn fresh_bound_variable<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
452+
debruijn: ty::DebruijnIndex)
453+
-> &'tcx ty::Region {
449454
infcx.region_vars.new_bound(debruijn)
450455
}
451456
}
452457
}
453458

454459
fn var_ids<'a, 'gcx, 'tcx>(fields: &CombineFields<'a, 'gcx, 'tcx>,
455-
map: &FnvHashMap<ty::BoundRegion, ty::Region>)
460+
map: &FnvHashMap<ty::BoundRegion, &'tcx ty::Region>)
456461
-> Vec<ty::RegionVid> {
457462
map.iter()
458-
.map(|(_, r)| match *r {
463+
.map(|(_, &r)| match *r {
459464
ty::ReVar(r) => { r }
460-
r => {
465+
_ => {
461466
span_bug!(
462467
fields.trace.origin.span(),
463468
"found non-region-vid: {:?}",
@@ -467,8 +472,8 @@ fn var_ids<'a, 'gcx, 'tcx>(fields: &CombineFields<'a, 'gcx, 'tcx>,
467472
.collect()
468473
}
469474

470-
fn is_var_in_set(new_vars: &[ty::RegionVid], r: ty::Region) -> bool {
471-
match r {
475+
fn is_var_in_set(new_vars: &[ty::RegionVid], r: &ty::Region) -> bool {
476+
match *r {
472477
ty::ReVar(ref v) => new_vars.iter().any(|x| x == v),
473478
_ => false
474479
}
@@ -479,13 +484,13 @@ fn fold_regions_in<'a, 'gcx, 'tcx, T, F>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
479484
mut fldr: F)
480485
-> T
481486
where T: TypeFoldable<'tcx>,
482-
F: FnMut(ty::Region, ty::DebruijnIndex) -> ty::Region,
487+
F: FnMut(&'tcx ty::Region, ty::DebruijnIndex) -> &'tcx ty::Region,
483488
{
484489
tcx.fold_regions(unbound_value, &mut false, |region, current_depth| {
485490
// we should only be encountering "escaping" late-bound regions here,
486491
// because the ones at the current level should have been replaced
487492
// with fresh variables
488-
assert!(match region {
493+
assert!(match *region {
489494
ty::ReLateBound(..) => false,
490495
_ => true
491496
});
@@ -497,9 +502,9 @@ fn fold_regions_in<'a, 'gcx, 'tcx, T, F>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
497502
impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
498503
fn tainted_regions(&self,
499504
snapshot: &CombinedSnapshot,
500-
r: ty::Region,
505+
r: &'tcx ty::Region,
501506
directions: TaintDirections)
502-
-> FnvHashSet<ty::Region> {
507+
-> FnvHashSet<&'tcx ty::Region> {
503508
self.region_vars.tainted(&snapshot.region_vars_snapshot, r, directions)
504509
}
505510

@@ -596,7 +601,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
596601
pub fn skolemize_late_bound_regions<T>(&self,
597602
binder: &ty::Binder<T>,
598603
snapshot: &CombinedSnapshot)
599-
-> (T, SkolemizationMap)
604+
-> (T, SkolemizationMap<'tcx>)
600605
where T : TypeFoldable<'tcx>
601606
{
602607
let (result, map) = self.tcx.replace_late_bound_regions(binder, |br| {
@@ -619,7 +624,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
619624
pub fn leak_check(&self,
620625
overly_polymorphic: bool,
621626
span: Span,
622-
skol_map: &SkolemizationMap,
627+
skol_map: &SkolemizationMap<'tcx>,
623628
snapshot: &CombinedSnapshot)
624629
-> RelateResult<'tcx, ()>
625630
{
@@ -673,7 +678,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
673678
for &tainted_region in &incoming_taints {
674679
// Each skolemized should only be relatable to itself
675680
// or new variables:
676-
match tainted_region {
681+
match *tainted_region {
677682
ty::ReVar(vid) => {
678683
if new_vars.contains(&vid) {
679684
warnings.extend(
@@ -742,7 +747,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
742747
/// to the depth of the predicate, in this case 1, so that the final
743748
/// predicate is `for<'a> &'a int : Clone`.
744749
pub fn plug_leaks<T>(&self,
745-
skol_map: SkolemizationMap,
750+
skol_map: SkolemizationMap<'tcx>,
746751
snapshot: &CombinedSnapshot,
747752
value: &T) -> T
748753
where T : TypeFoldable<'tcx>
@@ -755,7 +760,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
755760
// region back to the `ty::BoundRegion` that it originally
756761
// represented. Because `leak_check` passed, we know that
757762
// these taint sets are mutually disjoint.
758-
let inv_skol_map: FnvHashMap<ty::Region, ty::BoundRegion> =
763+
let inv_skol_map: FnvHashMap<&'tcx ty::Region, ty::BoundRegion> =
759764
skol_map
760765
.iter()
761766
.flat_map(|(&skol_br, &skol)| {
@@ -794,15 +799,16 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
794799
// (which ought not to escape the snapshot, but we
795800
// don't check that) or itself
796801
assert!(
797-
match r {
802+
match *r {
798803
ty::ReVar(_) => true,
799804
ty::ReSkolemized(_, ref br1) => br == br1,
800805
_ => false,
801806
},
802807
"leak-check would have us replace {:?} with {:?}",
803808
r, br);
804809

805-
ty::ReLateBound(ty::DebruijnIndex::new(current_depth - 1), br.clone())
810+
self.tcx.mk_region(ty::ReLateBound(
811+
ty::DebruijnIndex::new(current_depth - 1), br.clone()))
806812
}
807813
}
808814
});
@@ -826,7 +832,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
826832
///
827833
/// Note: popping also occurs implicitly as part of `leak_check`.
828834
pub fn pop_skolemized(&self,
829-
skol_map: SkolemizationMap,
835+
skol_map: SkolemizationMap<'tcx>,
830836
snapshot: &CombinedSnapshot)
831837
{
832838
debug!("pop_skolemized({:?})", skol_map);

src/librustc/infer/lub.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
5757
lattice::super_lattice_tys(self, a, b)
5858
}
5959

60-
fn regions(&mut self, a: ty::Region, b: ty::Region) -> RelateResult<'tcx, ty::Region> {
60+
fn regions(&mut self, a: &'tcx ty::Region, b: &'tcx ty::Region)
61+
-> RelateResult<'tcx, &'tcx ty::Region> {
6162
debug!("{}.regions({:?}, {:?})",
6263
self.tag(),
6364
a,

0 commit comments

Comments
 (0)