From 41ce3979902a1b1f02679b224b968c6c4cf12554 Mon Sep 17 00:00:00 2001 From: Jack Huey Date: Thu, 29 Oct 2020 18:42:31 -0400 Subject: [PATCH] Make anonymous binders start at 0 --- compiler/rustc_middle/src/ty/fold.rs | 5 +++-- compiler/rustc_symbol_mangling/src/v0.rs | 16 +++------------- .../rustc_typeck/src/check/generator_interior.rs | 3 ++- .../expect-fn-supply-fn.stderr | 4 ++-- .../expect-region-supply-region-2.stderr | 4 ++-- src/test/ui/issues/issue-10291.stderr | 2 +- src/test/ui/issues/issue-52533-1.stderr | 4 ++-- src/test/ui/issues/issue-52533.stderr | 4 ++-- src/test/ui/regions/regions-nested-fns.stderr | 6 +++--- .../ui/regions/regions-ret-borrowed-1.stderr | 2 +- src/test/ui/regions/regions-ret-borrowed.stderr | 2 +- ...fer-argument-types-two-region-pointers.stderr | 4 ++-- 12 files changed, 24 insertions(+), 32 deletions(-) diff --git a/compiler/rustc_middle/src/ty/fold.rs b/compiler/rustc_middle/src/ty/fold.rs index 0e5e22dcaae..13bf24bf8cf 100644 --- a/compiler/rustc_middle/src/ty/fold.rs +++ b/compiler/rustc_middle/src/ty/fold.rs @@ -684,7 +684,7 @@ impl<'tcx> TyCtxt<'tcx> { } /// Rewrite any late-bound regions so that they are anonymous. Region numbers are - /// assigned starting at 1 and increasing monotonically in the order traversed + /// assigned starting at 0 and increasing monotonically in the order traversed /// by the fold operation. /// /// The chief purpose of this function is to canonicalize regions so that two @@ -698,8 +698,9 @@ impl<'tcx> TyCtxt<'tcx> { let mut counter = 0; Binder::bind( self.replace_late_bound_regions(sig, |_| { + let r = self.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(counter))); counter += 1; - self.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(counter))) + r }) .0, ) diff --git a/compiler/rustc_symbol_mangling/src/v0.rs b/compiler/rustc_symbol_mangling/src/v0.rs index 7833385cbc9..1ff043ae91f 100644 --- a/compiler/rustc_symbol_mangling/src/v0.rs +++ b/compiler/rustc_symbol_mangling/src/v0.rs @@ -200,15 +200,9 @@ impl SymbolMangler<'tcx> { let lifetimes = regions .into_iter() - .map(|br| { - match br { - ty::BrAnon(i) => { - // FIXME(eddyb) for some reason, `anonymize_late_bound_regions` starts at `1`. - assert_ne!(i, 0); - i - 1 - } - _ => bug!("symbol_names: non-anonymized region `{:?}` in `{:?}`", br, value), - } + .map(|br| match br { + ty::BrAnon(i) => i, + _ => bug!("symbol_names: non-anonymized region `{:?}` in `{:?}`", br, value), }) .max() .map_or(0, |max| max + 1); @@ -327,10 +321,6 @@ impl Printer<'tcx> for SymbolMangler<'tcx> { // Late-bound lifetimes use indices starting at 1, // see `BinderLevel` for more details. ty::ReLateBound(debruijn, ty::BrAnon(i)) => { - // FIXME(eddyb) for some reason, `anonymize_late_bound_regions` starts at `1`. - assert_ne!(i, 0); - let i = i - 1; - let binder = &self.binders[self.binders.len() - 1 - debruijn.index()]; let depth = binder.lifetime_depths.start + i; diff --git a/compiler/rustc_typeck/src/check/generator_interior.rs b/compiler/rustc_typeck/src/check/generator_interior.rs index 4473aa2081f..5bd8ff6cde7 100644 --- a/compiler/rustc_typeck/src/check/generator_interior.rs +++ b/compiler/rustc_typeck/src/check/generator_interior.rs @@ -186,8 +186,9 @@ pub fn resolve_interior<'a, 'tcx>( // which means that none of the regions inside relate to any other, even if // typeck had previously found constraints that would cause them to be related. let folded = fcx.tcx.fold_regions(&erased, &mut false, |_, current_depth| { + let r = fcx.tcx.mk_region(ty::ReLateBound(current_depth, ty::BrAnon(counter))); counter += 1; - fcx.tcx.mk_region(ty::ReLateBound(current_depth, ty::BrAnon(counter))) + r }); cause.ty = folded; diff --git a/src/test/ui/closure-expected-type/expect-fn-supply-fn.stderr b/src/test/ui/closure-expected-type/expect-fn-supply-fn.stderr index 0de15dfa735..7a4ff779410 100644 --- a/src/test/ui/closure-expected-type/expect-fn-supply-fn.stderr +++ b/src/test/ui/closure-expected-type/expect-fn-supply-fn.stderr @@ -6,7 +6,7 @@ LL | with_closure_expecting_fn_with_free_region(|x: fn(&'x u32), y| {}); | = note: expected fn pointer `fn(&u32)` found fn pointer `fn(&'x u32)` -note: the anonymous lifetime #2 defined on the body at 16:48... +note: the anonymous lifetime #1 defined on the body at 16:48... --> $DIR/expect-fn-supply-fn.rs:16:48 | LL | with_closure_expecting_fn_with_free_region(|x: fn(&'x u32), y| {}); @@ -30,7 +30,7 @@ note: the lifetime `'x` as defined on the function body at 13:36... | LL | fn expect_free_supply_free_from_fn<'x>(x: &'x u32) { | ^^ -note: ...does not necessarily outlive the anonymous lifetime #2 defined on the body at 16:48 +note: ...does not necessarily outlive the anonymous lifetime #1 defined on the body at 16:48 --> $DIR/expect-fn-supply-fn.rs:16:48 | LL | with_closure_expecting_fn_with_free_region(|x: fn(&'x u32), y| {}); diff --git a/src/test/ui/closures/closure-expected-type/expect-region-supply-region-2.stderr b/src/test/ui/closures/closure-expected-type/expect-region-supply-region-2.stderr index 7f527904a69..07a67a61834 100644 --- a/src/test/ui/closures/closure-expected-type/expect-region-supply-region-2.stderr +++ b/src/test/ui/closures/closure-expected-type/expect-region-supply-region-2.stderr @@ -6,7 +6,7 @@ LL | closure_expecting_bound(|x: &'x u32| { | = note: expected reference `&u32` found reference `&'x u32` -note: the anonymous lifetime #2 defined on the body at 14:29... +note: the anonymous lifetime #1 defined on the body at 14:29... --> $DIR/expect-region-supply-region-2.rs:14:29 | LL | closure_expecting_bound(|x: &'x u32| { @@ -37,7 +37,7 @@ note: the lifetime `'x` as defined on the function body at 9:30... | LL | fn expect_bound_supply_named<'x>() { | ^^ -note: ...does not necessarily outlive the anonymous lifetime #2 defined on the body at 14:29 +note: ...does not necessarily outlive the anonymous lifetime #1 defined on the body at 14:29 --> $DIR/expect-region-supply-region-2.rs:14:29 | LL | closure_expecting_bound(|x: &'x u32| { diff --git a/src/test/ui/issues/issue-10291.stderr b/src/test/ui/issues/issue-10291.stderr index 4fff4ee866c..ff51aa3acf4 100644 --- a/src/test/ui/issues/issue-10291.stderr +++ b/src/test/ui/issues/issue-10291.stderr @@ -4,7 +4,7 @@ error[E0312]: lifetime of reference outlives lifetime of borrowed content... LL | x | ^ | -note: ...the reference is valid for the anonymous lifetime #2 defined on the body at 2:69... +note: ...the reference is valid for the anonymous lifetime #1 defined on the body at 2:69... --> $DIR/issue-10291.rs:2:69 | LL | drop:: FnMut(&'z isize) -> &'z isize>>(Box::new(|z| { diff --git a/src/test/ui/issues/issue-52533-1.stderr b/src/test/ui/issues/issue-52533-1.stderr index dd37d3e5593..4247d551565 100644 --- a/src/test/ui/issues/issue-52533-1.stderr +++ b/src/test/ui/issues/issue-52533-1.stderr @@ -6,12 +6,12 @@ LL | gimme(|x, y| y) | = note: expected reference `&Foo<'_, '_, u32>` found reference `&Foo<'_, '_, u32>` -note: the anonymous lifetime #4 defined on the body at 9:11... +note: the anonymous lifetime #3 defined on the body at 9:11... --> $DIR/issue-52533-1.rs:9:11 | LL | gimme(|x, y| y) | ^^^^^^^^ -note: ...does not necessarily outlive the anonymous lifetime #3 defined on the body at 9:11 +note: ...does not necessarily outlive the anonymous lifetime #2 defined on the body at 9:11 --> $DIR/issue-52533-1.rs:9:11 | LL | gimme(|x, y| y) diff --git a/src/test/ui/issues/issue-52533.stderr b/src/test/ui/issues/issue-52533.stderr index 58654800207..4e41620eecf 100644 --- a/src/test/ui/issues/issue-52533.stderr +++ b/src/test/ui/issues/issue-52533.stderr @@ -4,12 +4,12 @@ error[E0312]: lifetime of reference outlives lifetime of borrowed content... LL | foo(|a, b| b) | ^ | -note: ...the reference is valid for the anonymous lifetime #2 defined on the body at 5:9... +note: ...the reference is valid for the anonymous lifetime #1 defined on the body at 5:9... --> $DIR/issue-52533.rs:5:9 | LL | foo(|a, b| b) | ^^^^^^^^ -note: ...but the borrowed content is only valid for the anonymous lifetime #3 defined on the body at 5:9 +note: ...but the borrowed content is only valid for the anonymous lifetime #2 defined on the body at 5:9 --> $DIR/issue-52533.rs:5:9 | LL | foo(|a, b| b) diff --git a/src/test/ui/regions/regions-nested-fns.stderr b/src/test/ui/regions/regions-nested-fns.stderr index 9e405d83140..eeec0cc7862 100644 --- a/src/test/ui/regions/regions-nested-fns.stderr +++ b/src/test/ui/regions/regions-nested-fns.stderr @@ -4,7 +4,7 @@ error[E0495]: cannot infer an appropriate lifetime due to conflicting requiremen LL | let mut ay = &y; | ^^ | -note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the body at 7:58... +note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 7:58... --> $DIR/regions-nested-fns.rs:7:58 | LL | ignore:: FnMut(&'z isize)>>(Box::new(|z| { @@ -19,7 +19,7 @@ note: ...so that reference does not outlive borrowed content | LL | ay = z; | ^ -note: but, the lifetime must be valid for the anonymous lifetime #2 defined on the body at 13:72... +note: but, the lifetime must be valid for the anonymous lifetime #1 defined on the body at 13:72... --> $DIR/regions-nested-fns.rs:13:72 | LL | ignore::< Box FnMut(&'z isize) -> &'z isize>>(Box::new(|z| { @@ -48,7 +48,7 @@ error[E0312]: lifetime of reference outlives lifetime of borrowed content... LL | if false { return x; } | ^ | -note: ...the reference is valid for the anonymous lifetime #2 defined on the body at 13:72... +note: ...the reference is valid for the anonymous lifetime #1 defined on the body at 13:72... --> $DIR/regions-nested-fns.rs:13:72 | LL | ignore::< Box FnMut(&'z isize) -> &'z isize>>(Box::new(|z| { diff --git a/src/test/ui/regions/regions-ret-borrowed-1.stderr b/src/test/ui/regions/regions-ret-borrowed-1.stderr index 2c4769d8e37..bba968cfde4 100644 --- a/src/test/ui/regions/regions-ret-borrowed-1.stderr +++ b/src/test/ui/regions/regions-ret-borrowed-1.stderr @@ -4,7 +4,7 @@ error[E0495]: cannot infer an appropriate lifetime due to conflicting requiremen LL | with(|o| o) | ^ | -note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the body at 10:10... +note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 10:10... --> $DIR/regions-ret-borrowed-1.rs:10:10 | LL | with(|o| o) diff --git a/src/test/ui/regions/regions-ret-borrowed.stderr b/src/test/ui/regions/regions-ret-borrowed.stderr index da560107cea..4b93ca0ae67 100644 --- a/src/test/ui/regions/regions-ret-borrowed.stderr +++ b/src/test/ui/regions/regions-ret-borrowed.stderr @@ -4,7 +4,7 @@ error[E0495]: cannot infer an appropriate lifetime due to conflicting requiremen LL | with(|o| o) | ^ | -note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the body at 13:10... +note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 13:10... --> $DIR/regions-ret-borrowed.rs:13:10 | LL | with(|o| o) diff --git a/src/test/ui/unboxed-closures/unboxed-closures-infer-argument-types-two-region-pointers.stderr b/src/test/ui/unboxed-closures/unboxed-closures-infer-argument-types-two-region-pointers.stderr index 526055ba04b..bd20fd26180 100644 --- a/src/test/ui/unboxed-closures/unboxed-closures-infer-argument-types-two-region-pointers.stderr +++ b/src/test/ui/unboxed-closures/unboxed-closures-infer-argument-types-two-region-pointers.stderr @@ -4,7 +4,7 @@ error[E0312]: lifetime of reference outlives lifetime of borrowed content... LL | x.set(y); | ^ | -note: ...the reference is valid for the anonymous lifetime #3 defined on the body at 16:14... +note: ...the reference is valid for the anonymous lifetime #2 defined on the body at 16:14... --> $DIR/unboxed-closures-infer-argument-types-two-region-pointers.rs:16:14 | LL | doit(0, &|x, y| { @@ -12,7 +12,7 @@ LL | doit(0, &|x, y| { LL | | x.set(y); LL | | }); | |_____^ -note: ...but the borrowed content is only valid for the anonymous lifetime #4 defined on the body at 16:14 +note: ...but the borrowed content is only valid for the anonymous lifetime #3 defined on the body at 16:14 --> $DIR/unboxed-closures-infer-argument-types-two-region-pointers.rs:16:14 | LL | doit(0, &|x, y| {