Skip to content

Commit fc19590

Browse files
committed
Auto merge of #51248 - fabric-and-ink:newtype_index_debrujin, r=nikomatsakis
Declare DebruijnIndex via newtype_index macro Part of #49887 Declare `DebruijnIndex` via the `newtype_index` macro.
2 parents 5230979 + 862d25c commit fc19590

27 files changed

+50
-56
lines changed

src/librustc/infer/error_reporting/nice_region_error/find_anon_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
7777
tcx: self.tcx,
7878
bound_region: *br,
7979
found_type: None,
80-
current_index: ty::DebruijnIndex::INNERMOST,
80+
current_index: ty::INNERMOST,
8181
};
8282
nested_visitor.visit_ty(arg);
8383
nested_visitor.found_type

src/librustc/infer/higher_ranked/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
417417
{
418418
for (a_br, a_r) in a_map {
419419
if *a_r == r {
420-
return infcx.tcx.mk_region(ty::ReLateBound(ty::DebruijnIndex::INNERMOST,
421-
*a_br));
420+
return infcx.tcx.mk_region(ty::ReLateBound(ty::INNERMOST, *a_br));
422421
}
423422
}
424423
span_bug!(
@@ -735,7 +734,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
735734
// trait checking, and all of the skolemized regions
736735
// appear inside predicates, which always have
737736
// binders, so this assert is satisfied.
738-
assert!(current_depth > ty::DebruijnIndex::INNERMOST);
737+
assert!(current_depth > ty::INNERMOST);
739738

740739
// since leak-check passed, this skolemized region
741740
// should only have incoming edges from variables

src/librustc/middle/resolve_lifetime.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl Region {
9898
}
9999

100100
fn late(hir_map: &Map, def: &hir::LifetimeDef) -> (hir::LifetimeName, Region) {
101-
let depth = ty::DebruijnIndex::INNERMOST;
101+
let depth = ty::INNERMOST;
102102
let def_id = hir_map.local_def_id(def.lifetime.id);
103103
let origin = LifetimeDefOrigin::from_is_in_band(def.in_band);
104104
debug!(
@@ -114,7 +114,7 @@ impl Region {
114114
fn late_anon(index: &Cell<u32>) -> Region {
115115
let i = index.get();
116116
index.set(i + 1);
117-
let depth = ty::DebruijnIndex::INNERMOST;
117+
let depth = ty::INNERMOST;
118118
Region::LateBoundAnon(depth, i)
119119
}
120120

@@ -1870,7 +1870,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
18701870
.map(|(i, input)| {
18711871
let mut gather = GatherLifetimes {
18721872
map: self.map,
1873-
outer_index: ty::DebruijnIndex::INNERMOST,
1873+
outer_index: ty::INNERMOST,
18741874
have_bound_regions: false,
18751875
lifetimes: FxHashSet(),
18761876
};

src/librustc/ty/flags.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl FlagComputation {
2424
fn new() -> FlagComputation {
2525
FlagComputation {
2626
flags: TypeFlags::empty(),
27-
outer_exclusive_binder: ty::DebruijnIndex::INNERMOST,
27+
outer_exclusive_binder: ty::INNERMOST,
2828
}
2929
}
3030

@@ -60,7 +60,7 @@ impl FlagComputation {
6060
// a region binder, so subtract one from the region depth
6161
// within when adding the depth to `self`.
6262
let outer_exclusive_binder = computation.outer_exclusive_binder;
63-
if outer_exclusive_binder > ty::DebruijnIndex::INNERMOST {
63+
if outer_exclusive_binder > ty::INNERMOST {
6464
self.add_exclusive_binder(outer_exclusive_binder.shifted_out(1));
6565
} else {
6666
// otherwise, this binder captures nothing

src/librustc/ty/fold.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
6565

6666
/// True if `self` has any late-bound regions that are either
6767
/// bound by `binder` or bound by some binder outside of `binder`.
68-
/// If `binder` is `ty::DebruijnIndex::INNERMOST`, this indicates whether
68+
/// If `binder` is `ty::INNERMOST`, this indicates whether
6969
/// there are any late-bound regions that appear free.
7070
fn has_regions_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
7171
self.visit_with(&mut HasEscapingRegionsVisitor { outer_index: binder })
@@ -78,7 +78,7 @@ pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
7878
}
7979

8080
fn has_escaping_regions(&self) -> bool {
81-
self.has_regions_bound_at_or_above(ty::DebruijnIndex::INNERMOST)
81+
self.has_regions_bound_at_or_above(ty::INNERMOST)
8282
}
8383

8484
fn has_type_flags(&self, flags: TypeFlags) -> bool {
@@ -246,7 +246,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
246246
T: TypeFoldable<'tcx>,
247247
{
248248
value.visit_with(&mut RegionVisitor {
249-
outer_index: ty::DebruijnIndex::INNERMOST,
249+
outer_index: ty::INNERMOST,
250250
callback
251251
});
252252

@@ -260,7 +260,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
260260
/// ^ ^ ^ ^
261261
/// | | | | here, would be shifted in 1
262262
/// | | | here, would be shifted in 2
263-
/// | | here, would be INNTERMOST shifted in by 1
263+
/// | | here, would be INNERMOST shifted in by 1
264264
/// | here, initially, binder would be INNERMOST
265265
/// ```
266266
///
@@ -333,7 +333,7 @@ impl<'a, 'gcx, 'tcx> RegionFolder<'a, 'gcx, 'tcx> {
333333
RegionFolder {
334334
tcx,
335335
skipped_regions,
336-
current_index: ty::DebruijnIndex::INNERMOST,
336+
current_index: ty::INNERMOST,
337337
fold_region_fn,
338338
}
339339
}
@@ -495,7 +495,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
495495
let mut counter = 0;
496496
Binder::bind(self.replace_late_bound_regions(sig, |_| {
497497
counter += 1;
498-
self.mk_region(ty::ReLateBound(ty::DebruijnIndex::INNERMOST, ty::BrAnon(counter)))
498+
self.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(counter)))
499499
}).0)
500500
}
501501
}
@@ -507,7 +507,7 @@ impl<'a, 'gcx, 'tcx> RegionReplacer<'a, 'gcx, 'tcx> {
507507
{
508508
RegionReplacer {
509509
tcx,
510-
current_index: ty::DebruijnIndex::INNERMOST,
510+
current_index: ty::INNERMOST,
511511
fld_r,
512512
map: BTreeMap::default()
513513
}
@@ -542,7 +542,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for RegionReplacer<'a, 'gcx, 'tcx> {
542542
// that region should always use the INNERMOST
543543
// debruijn index. Then we adjust it to the
544544
// correct depth.
545-
assert_eq!(debruijn1, ty::DebruijnIndex::INNERMOST);
545+
assert_eq!(debruijn1, ty::INNERMOST);
546546
self.tcx.mk_region(ty::ReLateBound(debruijn, br))
547547
} else {
548548
region
@@ -701,7 +701,7 @@ struct LateBoundRegionsCollector {
701701
impl LateBoundRegionsCollector {
702702
fn new(just_constrained: bool) -> Self {
703703
LateBoundRegionsCollector {
704-
current_index: ty::DebruijnIndex::INNERMOST,
704+
current_index: ty::INNERMOST,
705705
regions: FxHashSet(),
706706
just_constrained,
707707
}

src/librustc/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult,
6060

6161
use hir;
6262

63-
pub use self::sty::{Binder, CanonicalVar, DebruijnIndex};
63+
pub use self::sty::{Binder, CanonicalVar, DebruijnIndex, INNERMOST};
6464
pub use self::sty::{FnSig, GenSig, PolyFnSig, PolyGenSig};
6565
pub use self::sty::{InferTy, ParamTy, ProjectionTy, ExistentialPredicate};
6666
pub use self::sty::{ClosureSubsts, GeneratorSubsts, UpvarSubsts, TypeAndMut};

src/librustc/ty/sty.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,12 +1023,11 @@ impl<'a, 'gcx, 'tcx> ParamTy {
10231023
/// is the outer fn.
10241024
///
10251025
/// [dbi]: http://en.wikipedia.org/wiki/De_Bruijn_index
1026-
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug, Copy, PartialOrd, Ord)]
1027-
pub struct DebruijnIndex {
1028-
/// We maintain the invariant that this is never 0. So 1 indicates
1029-
/// the innermost binder.
1030-
index: u32,
1031-
}
1026+
newtype_index!(DebruijnIndex
1027+
{
1028+
DEBUG_FORMAT = "DebruijnIndex({})",
1029+
const INNERMOST = 0,
1030+
});
10321031

10331032
pub type Region<'tcx> = &'tcx RegionKind;
10341033

@@ -1261,8 +1260,6 @@ impl<'a, 'tcx, 'gcx> PolyExistentialProjection<'tcx> {
12611260
}
12621261

12631262
impl DebruijnIndex {
1264-
pub const INNERMOST: DebruijnIndex = DebruijnIndex { index: 0 };
1265-
12661263
/// Returns the resulting index when this value is moved into
12671264
/// `amount` number of new binders. So e.g. if you had
12681265
///
@@ -1275,7 +1272,7 @@ impl DebruijnIndex {
12751272
/// you would need to shift the index for `'a` into 1 new binder.
12761273
#[must_use]
12771274
pub const fn shifted_in(self, amount: u32) -> DebruijnIndex {
1278-
DebruijnIndex { index: self.index + amount }
1275+
DebruijnIndex(self.0 + amount)
12791276
}
12801277

12811278
/// Update this index in place by shifting it "in" through
@@ -1288,7 +1285,7 @@ impl DebruijnIndex {
12881285
/// `amount` number of new binders.
12891286
#[must_use]
12901287
pub const fn shifted_out(self, amount: u32) -> DebruijnIndex {
1291-
DebruijnIndex { index: self.index - amount }
1288+
DebruijnIndex(self.0 - amount)
12921289
}
12931290

12941291
/// Update in place by shifting out from `amount` binders.
@@ -1317,13 +1314,11 @@ impl DebruijnIndex {
13171314
/// bound by one of the binders we are shifting out of, that is an
13181315
/// error (and should fail an assertion failure).
13191316
pub fn shifted_out_to_binder(self, to_binder: DebruijnIndex) -> Self {
1320-
self.shifted_out(to_binder.index - Self::INNERMOST.index)
1317+
self.shifted_out((to_binder.0 - INNERMOST.0) as u32)
13211318
}
13221319
}
13231320

1324-
impl_stable_hash_for!(struct DebruijnIndex {
1325-
index
1326-
});
1321+
impl_stable_hash_for!(tuple_struct DebruijnIndex { index });
13271322

13281323
/// Region utilities
13291324
impl RegionKind {

src/librustc/ty/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
555555
-> Option<ty::Binder<Ty<'tcx>>>
556556
{
557557
let closure_ty = self.mk_closure(closure_def_id, closure_substs);
558-
let env_region = ty::ReLateBound(ty::DebruijnIndex::INNERMOST, ty::BrEnv);
558+
let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
559559
let closure_kind_ty = closure_substs.closure_kind_ty(closure_def_id, self);
560560
let closure_kind = closure_kind_ty.to_opt_closure_kind()?;
561561
let env_ty = match closure_kind {

src/librustc/util/ppaux.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ impl PrintContext {
527527
ty::BrNamed(tcx.hir.local_def_id(CRATE_NODE_ID), name)
528528
}
529529
};
530-
tcx.mk_region(ty::ReLateBound(ty::DebruijnIndex::INNERMOST, br))
530+
tcx.mk_region(ty::ReLateBound(ty::INNERMOST, br))
531531
}).0;
532532
start_or_continue(f, "", "> ")?;
533533

src/librustc_codegen_llvm/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ pub fn ty_fn_sig<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
425425
let tcx = cx.tcx;
426426
let sig = substs.poly_sig(def_id, cx.tcx);
427427

428-
let env_region = ty::ReLateBound(ty::DebruijnIndex::INNERMOST, ty::BrEnv);
428+
let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
429429
let env_ty = tcx.mk_mut_ref(tcx.mk_region(env_region), ty);
430430

431431
sig.map_bound(|sig| {

0 commit comments

Comments
 (0)