Skip to content

Commit abf5e81

Browse files
committed
Use Ranges for vars_since_snapshot
1 parent a5c653b commit abf5e81

File tree

4 files changed

+38
-39
lines changed

4 files changed

+38
-39
lines changed

src/librustc/infer/fudge.rs

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use crate::infer::type_variable::TypeVariableMap;
2-
use crate::ty::{self, Ty, TyCtxt};
1+
use crate::ty::{self, Ty, TyCtxt, TyVid, RegionVid};
32
use crate::ty::fold::{TypeFoldable, TypeFolder};
43

54
use super::InferCtxt;
65
use super::RegionVariableOrigin;
76

7+
use std::ops::Range;
8+
89
impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
910
/// This rather funky routine is used while processing expected
1011
/// types. What happens here is that we want to propagate a
@@ -42,12 +43,14 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
4243
/// regions in question are not particularly important. We will
4344
/// use the expected types to guide coercions, but we will still
4445
/// type-check the resulting types from those coercions against
45-
/// the actual types (`?T`, `Option<?T`) -- and remember that
46+
/// the actual types (`?T`, `Option<?T>`) -- and remember that
4647
/// after the snapshot is popped, the variable `?T` is no longer
4748
/// unified.
48-
pub fn fudge_regions_if_ok<T, E, F>(&self,
49-
origin: &RegionVariableOrigin,
50-
f: F) -> Result<T, E> where
49+
pub fn fudge_regions_if_ok<T, E, F>(
50+
&self,
51+
origin: &RegionVariableOrigin,
52+
f: F,
53+
) -> Result<T, E> where
5154
F: FnOnce() -> Result<T, E>,
5255
T: TypeFoldable<'tcx>,
5356
{
@@ -101,8 +104,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
101104

102105
pub struct RegionFudger<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
103106
infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
104-
type_variables: &'a TypeVariableMap,
105-
region_vars: &'a Vec<ty::RegionVid>,
107+
type_variables: &'a Range<TyVid>,
108+
region_vars: &'a Range<RegionVid>,
106109
origin: &'a RegionVariableOrigin,
107110
}
108111

@@ -114,25 +117,21 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for RegionFudger<'a, 'gcx, 'tcx> {
114117
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
115118
match ty.sty {
116119
ty::Infer(ty::InferTy::TyVar(vid)) => {
117-
match self.type_variables.get(&vid) {
118-
None => {
119-
// This variable was created before the
120-
// "fudging". Since we refresh all type
121-
// variables to their binding anyhow, we know
122-
// that it is unbound, so we can just return
123-
// it.
124-
debug_assert!(self.infcx.type_variables.borrow_mut()
125-
.probe(vid)
126-
.is_unknown());
127-
ty
128-
}
129-
130-
Some(&origin) => {
131-
// This variable was created during the
132-
// fudging. Recreate it with a fresh variable
133-
// here.
134-
self.infcx.next_ty_var(origin)
135-
}
120+
if self.type_variables.contains(&vid) {
121+
// This variable was created during the fudging.
122+
// Recreate it with a fresh variable here.
123+
let origin = self.infcx.type_variables.borrow().var_origin(vid).clone();
124+
self.infcx.next_ty_var(origin)
125+
} else {
126+
// This variable was created before the
127+
// "fudging". Since we refresh all type
128+
// variables to their binding anyhow, we know
129+
// that it is unbound, so we can just return
130+
// it.
131+
debug_assert!(self.infcx.type_variables.borrow_mut()
132+
.probe(vid)
133+
.is_unknown());
134+
ty
136135
}
137136
}
138137
_ => ty.super_fold_with(self),
@@ -141,12 +140,10 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for RegionFudger<'a, 'gcx, 'tcx> {
141140

142141
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
143142
match *r {
144-
ty::ReVar(v) if self.region_vars.contains(&v) => {
143+
ty::ReVar(vid) if self.region_vars.contains(&vid) => {
145144
self.infcx.next_region_var(self.origin.clone())
146145
}
147-
_ => {
148-
r
149-
}
146+
_ => r,
150147
}
151148
}
152149
}

src/librustc/infer/region_constraints/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::ty::{Region, RegionVid};
1616

1717
use std::collections::BTreeMap;
1818
use std::{cmp, fmt, mem, u32};
19+
use std::ops::Range;
1920

2021
mod leak_check;
2122

@@ -840,8 +841,8 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
840841
}
841842
}
842843

843-
pub fn vars_since_snapshot(&self, mark: &RegionSnapshot) -> Vec<RegionVid> {
844-
self.unification_table.vars_since_snapshot(&mark.region_snapshot).collect()
844+
pub fn vars_since_snapshot(&self, mark: &RegionSnapshot) -> Range<RegionVid> {
845+
self.unification_table.vars_since_snapshot(&mark.region_snapshot)
845846
}
846847

847848
/// See [`RegionInference::region_constraints_added_in_snapshot`].

src/librustc/infer/type_variable.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use syntax::symbol::InternedString;
22
use syntax_pos::Span;
3-
use crate::ty::{self, Ty};
3+
use crate::ty::{self, Ty, TyVid};
44

55
use std::cmp;
66
use std::marker::PhantomData;
7+
use std::ops::Range;
78
use std::u32;
89
use rustc_data_structures::fx::FxHashMap;
910
use rustc_data_structures::snapshot_vec as sv;
1011
use rustc_data_structures::unify as ut;
12+
use ut::UnifyKey;
1113

1214
pub struct TypeVariableTable<'tcx> {
1315
values: sv::SnapshotVec<Delegate>,
@@ -294,11 +296,9 @@ impl<'tcx> TypeVariableTable<'tcx> {
294296

295297
/// Returns a map from the type variables created during the
296298
/// snapshot to the origin of the type variable.
297-
pub fn vars_since_snapshot(&mut self, s: &Snapshot<'tcx>) -> TypeVariableMap {
298-
self.values.values_since_snapshot(&s.snapshot).map(|idx| {
299-
let origin = self.values.get(idx).origin.clone();
300-
(ty::TyVid { index: idx as u32 }, origin)
301-
}).collect()
299+
pub fn vars_since_snapshot(&mut self, s: &Snapshot<'tcx>) -> Range<TyVid> {
300+
let range = self.values.values_since_snapshot(&s.snapshot);
301+
TyVid::from_index(range.start as u32)..TyVid::from_index(range.end as u32)
302302
}
303303

304304
/// Finds the set of type variables that existed *before* `s`

src/librustc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#![feature(non_exhaustive)]
4545
#![feature(proc_macro_internals)]
4646
#![feature(optin_builtin_traits)]
47+
#![feature(range_is_empty)]
4748
#![feature(refcell_replace_swap)]
4849
#![feature(rustc_diagnostic_macros)]
4950
#![feature(rustc_attrs)]

0 commit comments

Comments
 (0)