Skip to content

Commit 2f41c0d

Browse files
committedOct 20, 2018
Rename InferTy::CanonicalTy to BoundTy and add DebruijnIndex to variant type
1 parent 2581bdc commit 2f41c0d

File tree

12 files changed

+43
-27
lines changed

12 files changed

+43
-27
lines changed
 

‎src/librustc/ich/impls_ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ impl_stable_hash_for!(enum ty::InferTy {
915915
FreshTy(a),
916916
FreshIntTy(a),
917917
FreshFloatTy(a),
918-
CanonicalTy(a),
918+
BoundTy(a),
919919
});
920920

921921
impl<'a, 'gcx> HashStable<StableHashingContext<'a>>

‎src/librustc/infer/canonical/canonicalizer.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use infer::InferCtxt;
2323
use std::sync::atomic::Ordering;
2424
use ty::fold::{TypeFoldable, TypeFolder};
2525
use ty::subst::Kind;
26-
use ty::{self, BoundTyIndex, Lift, List, Ty, TyCtxt, TypeFlags};
26+
use ty::{self, BoundTy, BoundTyIndex, Lift, List, Ty, TyCtxt, TypeFlags};
2727

2828
use rustc_data_structures::fx::FxHashMap;
2929
use rustc_data_structures::indexed_vec::Idx;
@@ -283,7 +283,7 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for Canonicalizer<'cx, 'gcx, 'tcx>
283283
bug!("encountered a fresh type during canonicalization")
284284
}
285285

286-
ty::Infer(ty::CanonicalTy(_)) => {
286+
ty::Infer(ty::BoundTy(_)) => {
287287
bug!("encountered a canonical type during canonicalization")
288288
}
289289

@@ -393,7 +393,7 @@ impl<'cx, 'gcx, 'tcx> Canonicalizer<'cx, 'gcx, 'tcx> {
393393
/// or returns an existing variable if `kind` has already been
394394
/// seen. `kind` is expected to be an unbound variable (or
395395
/// potentially a free region).
396-
fn canonical_var(&mut self, info: CanonicalVarInfo, kind: Kind<'tcx>) -> BoundTyIndex {
396+
fn canonical_var(&mut self, info: CanonicalVarInfo, kind: Kind<'tcx>) -> BoundTy {
397397
let Canonicalizer {
398398
variables,
399399
query_state,
@@ -408,7 +408,7 @@ impl<'cx, 'gcx, 'tcx> Canonicalizer<'cx, 'gcx, 'tcx> {
408408
// avoid allocations in those cases. We also don't use `indices` to
409409
// determine if a kind has been seen before until the limit of 8 has
410410
// been exceeded, to also avoid allocations for `indices`.
411-
if !var_values.spilled() {
411+
let var = if !var_values.spilled() {
412412
// `var_values` is stack-allocated. `indices` isn't used yet. Do a
413413
// direct linear search of `var_values`.
414414
if let Some(idx) = var_values.iter().position(|&k| k == kind) {
@@ -442,15 +442,21 @@ impl<'cx, 'gcx, 'tcx> Canonicalizer<'cx, 'gcx, 'tcx> {
442442
assert_eq!(variables.len(), var_values.len());
443443
BoundTyIndex::new(variables.len() - 1)
444444
})
445+
};
446+
447+
BoundTy {
448+
level: ty::INNERMOST,
449+
var,
445450
}
446451
}
447452

448453
fn canonical_var_for_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
449454
let info = CanonicalVarInfo {
450455
kind: CanonicalVarKind::Region,
451456
};
452-
let cvar = self.canonical_var(info, r.into());
453-
self.tcx().mk_region(ty::ReCanonical(cvar))
457+
let b = self.canonical_var(info, r.into());
458+
debug_assert_eq!(ty::INNERMOST, b.level);
459+
self.tcx().mk_region(ty::ReCanonical(b.var))
454460
}
455461

456462
/// Given a type variable `ty_var` of the given kind, first check
@@ -466,8 +472,9 @@ impl<'cx, 'gcx, 'tcx> Canonicalizer<'cx, 'gcx, 'tcx> {
466472
let info = CanonicalVarInfo {
467473
kind: CanonicalVarKind::Ty(ty_kind),
468474
};
469-
let cvar = self.canonical_var(info, ty_var.into());
470-
self.tcx().mk_infer(ty::InferTy::CanonicalTy(cvar))
475+
let b = self.canonical_var(info, ty_var.into());
476+
debug_assert_eq!(ty::INNERMOST, b.level);
477+
self.tcx().mk_infer(ty::InferTy::BoundTy(b))
471478
}
472479
}
473480
}

‎src/librustc/infer/canonical/query_response.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,9 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
417417
match result_value.unpack() {
418418
UnpackedKind::Type(result_value) => {
419419
// e.g., here `result_value` might be `?0` in the example above...
420-
if let ty::Infer(ty::InferTy::CanonicalTy(index)) = result_value.sty {
420+
if let ty::Infer(ty::InferTy::BoundTy(b)) = result_value.sty {
421421
// in which case we would set `canonical_vars[0]` to `Some(?U)`.
422-
opt_values[index] = Some(*original_value);
422+
opt_values[b.var] = Some(*original_value);
423423
}
424424
}
425425
UnpackedKind::Lifetime(result_value) => {

‎src/librustc/infer/canonical/substitute.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for CanonicalVarValuesSubst<'cx, 'g
8585

8686
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
8787
match t.sty {
88-
ty::Infer(ty::InferTy::CanonicalTy(c)) => {
89-
match self.var_values.var_values[c].unpack() {
88+
ty::Infer(ty::InferTy::BoundTy(b)) => {
89+
debug_assert_eq!(ty::INNERMOST, b.level);
90+
match self.var_values.var_values[b.var].unpack() {
9091
UnpackedKind::Type(ty) => ty,
91-
r => bug!("{:?} is a type but value is {:?}", c, r),
92+
r => bug!("{:?} is a type but value is {:?}", b, r),
9293
}
9394
}
9495
_ => {

‎src/librustc/infer/freshen.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
171171
t
172172
}
173173

174-
ty::Infer(ty::CanonicalTy(..)) =>
174+
ty::Infer(ty::BoundTy(..)) =>
175175
bug!("encountered canonical ty during freshening"),
176176

177177
ty::Generator(..) |

‎src/librustc/traits/select.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2421,7 +2421,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
24212421
ty::Infer(ty::TyVar(_)) => Ambiguous,
24222422

24232423
ty::UnnormalizedProjection(..)
2424-
| ty::Infer(ty::CanonicalTy(_))
2424+
| ty::Infer(ty::BoundTy(_))
24252425
| ty::Infer(ty::FreshTy(_))
24262426
| ty::Infer(ty::FreshIntTy(_))
24272427
| ty::Infer(ty::FreshFloatTy(_)) => {
@@ -2506,7 +2506,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
25062506
}
25072507

25082508
ty::UnnormalizedProjection(..)
2509-
| ty::Infer(ty::CanonicalTy(_))
2509+
| ty::Infer(ty::BoundTy(_))
25102510
| ty::Infer(ty::FreshTy(_))
25112511
| ty::Infer(ty::FreshIntTy(_))
25122512
| ty::Infer(ty::FreshFloatTy(_)) => {
@@ -2549,7 +2549,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
25492549
| ty::Param(..)
25502550
| ty::Foreign(..)
25512551
| ty::Projection(..)
2552-
| ty::Infer(ty::CanonicalTy(_))
2552+
| ty::Infer(ty::BoundTy(_))
25532553
| ty::Infer(ty::TyVar(_))
25542554
| ty::Infer(ty::FreshTy(_))
25552555
| ty::Infer(ty::FreshIntTy(_))

‎src/librustc/ty/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {
217217
ty::Infer(ty::TyVar(_)) => "inferred type".into(),
218218
ty::Infer(ty::IntVar(_)) => "integral variable".into(),
219219
ty::Infer(ty::FloatVar(_)) => "floating-point variable".into(),
220-
ty::Infer(ty::CanonicalTy(_)) |
220+
ty::Infer(ty::BoundTy(_)) |
221221
ty::Infer(ty::FreshTy(_)) => "fresh type".into(),
222222
ty::Infer(ty::FreshIntTy(_)) => "fresh integral type".into(),
223223
ty::Infer(ty::FreshFloatTy(_)) => "fresh floating-point type".into(),

‎src/librustc/ty/flags.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl FlagComputation {
122122
ty::FreshTy(_) |
123123
ty::FreshIntTy(_) |
124124
ty::FreshFloatTy(_) |
125-
ty::CanonicalTy(_) => {
125+
ty::BoundTy(_) => {
126126
self.add_flags(TypeFlags::HAS_CANONICAL_VARS);
127127
}
128128

‎src/librustc/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult,
6363

6464
use hir;
6565

66-
pub use self::sty::{Binder, BoundTyIndex, DebruijnIndex, INNERMOST};
66+
pub use self::sty::{Binder, BoundTy, BoundTyIndex, DebruijnIndex, INNERMOST};
6767
pub use self::sty::{FnSig, GenSig, PolyFnSig, PolyGenSig};
6868
pub use self::sty::{InferTy, ParamTy, ProjectionTy, ExistentialPredicate};
6969
pub use self::sty::{ClosureSubsts, GeneratorSubsts, UpvarSubsts, TypeAndMut};

‎src/librustc/ty/sty.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1217,14 +1217,22 @@ pub enum InferTy {
12171217
FreshIntTy(u32),
12181218
FreshFloatTy(u32),
12191219

1220-
/// Canonicalized type variable, used only when preparing a trait query.
1221-
CanonicalTy(BoundTyIndex),
1220+
/// Bound type variable, used only when preparing a trait query.
1221+
BoundTy(BoundTy),
12221222
}
12231223

12241224
newtype_index! {
12251225
pub struct BoundTyIndex { .. }
12261226
}
12271227

1228+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, RustcEncodable, RustcDecodable)]
1229+
pub struct BoundTy {
1230+
pub level: DebruijnIndex,
1231+
pub var: BoundTyIndex,
1232+
}
1233+
1234+
impl_stable_hash_for!(struct BoundTy { level, var });
1235+
12281236
/// A `ProjectionPredicate` for an `ExistentialTraitRef`.
12291237
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, RustcEncodable, RustcDecodable)]
12301238
pub struct ExistentialProjection<'tcx> {
@@ -1919,7 +1927,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
19191927

19201928
ty::Infer(ty::TyVar(_)) => false,
19211929

1922-
ty::Infer(ty::CanonicalTy(_)) |
1930+
ty::Infer(ty::BoundTy(_)) |
19231931
ty::Infer(ty::FreshTy(_)) |
19241932
ty::Infer(ty::FreshIntTy(_)) |
19251933
ty::Infer(ty::FreshFloatTy(_)) =>

‎src/librustc/ty/subst.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ impl CanonicalUserSubsts<'tcx> {
556556
self.value.substs.iter().zip(BoundTyIndex::new(0)..).all(|(kind, cvar)| {
557557
match kind.unpack() {
558558
UnpackedKind::Type(ty) => match ty.sty {
559-
ty::Infer(ty::CanonicalTy(cvar1)) => cvar == cvar1,
559+
ty::Infer(ty::BoundTy(ref b)) => cvar == b.var,
560560
_ => false,
561561
},
562562

‎src/librustc/util/ppaux.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ define_print! {
984984
ty::TyVar(_) => write!(f, "_"),
985985
ty::IntVar(_) => write!(f, "{}", "{integer}"),
986986
ty::FloatVar(_) => write!(f, "{}", "{float}"),
987-
ty::CanonicalTy(_) => write!(f, "_"),
987+
ty::BoundTy(_) => write!(f, "_"),
988988
ty::FreshTy(v) => write!(f, "FreshTy({})", v),
989989
ty::FreshIntTy(v) => write!(f, "FreshIntTy({})", v),
990990
ty::FreshFloatTy(v) => write!(f, "FreshFloatTy({})", v)
@@ -996,7 +996,7 @@ define_print! {
996996
ty::TyVar(ref v) => write!(f, "{:?}", v),
997997
ty::IntVar(ref v) => write!(f, "{:?}", v),
998998
ty::FloatVar(ref v) => write!(f, "{:?}", v),
999-
ty::CanonicalTy(v) => write!(f, "?{:?}", v.index()),
999+
ty::BoundTy(v) => write!(f, "?{:?}", v.var.index()),
10001000
ty::FreshTy(v) => write!(f, "FreshTy({:?})", v),
10011001
ty::FreshIntTy(v) => write!(f, "FreshIntTy({:?})", v),
10021002
ty::FreshFloatTy(v) => write!(f, "FreshFloatTy({:?})", v)

0 commit comments

Comments
 (0)