Skip to content

Commit 7354f6e

Browse files
authored
Rollup merge of rust-lang#135479 - lcnr:method-calls-on-opaques, r=compiler-errors
mir borrowck: cleanup late-bound region handling r? types
2 parents e0e2f6a + 99657aa commit 7354f6e

7 files changed

+34
-48
lines changed

Diff for: compiler/rustc_borrowck/src/renumber.rs

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ pub(crate) enum RegionCtxt {
3434
Location(Location),
3535
TyContext(TyContext),
3636
Free(Symbol),
37-
Bound(Symbol),
3837
LateBound(Symbol),
3938
Existential(Option<Symbol>),
4039
Placeholder(Symbol),

Diff for: compiler/rustc_borrowck/src/universal_regions.rs

+26-39
Original file line numberDiff line numberDiff line change
@@ -467,15 +467,13 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
467467
self.infcx.tcx.local_parent(self.mir_def),
468468
|r| {
469469
debug!(?r);
470-
if !indices.indices.contains_key(&r) {
471-
let region_vid = {
472-
let name = r.get_name_or_anon();
473-
self.infcx.next_nll_region_var(FR, || RegionCtxt::LateBound(name))
474-
};
475-
476-
debug!(?region_vid);
477-
indices.insert_late_bound_region(r, region_vid.as_var());
478-
}
470+
let region_vid = {
471+
let name = r.get_name_or_anon();
472+
self.infcx.next_nll_region_var(FR, || RegionCtxt::LateBound(name))
473+
};
474+
475+
debug!(?region_vid);
476+
indices.insert_late_bound_region(r, region_vid.as_var());
479477
},
480478
);
481479

@@ -484,21 +482,17 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
484482
self.infcx.num_region_vars()
485483
};
486484

487-
// "Liberate" the late-bound regions. These correspond to
488-
// "local" free regions.
485+
// Converse of above, if this is a function/closure then the late-bound regions declared
486+
// on its signature are local.
487+
//
488+
// We manually loop over `bound_inputs_and_output` instead of using
489+
// `for_each_late_bound_region_in_item` as we may need to add the otherwise
490+
// implicit `ClosureEnv` region.
489491
let bound_inputs_and_output = self.compute_inputs_and_output(&indices, defining_ty);
490-
491-
let inputs_and_output = self.infcx.replace_bound_regions_with_nll_infer_vars(
492-
FR,
493-
self.mir_def,
494-
bound_inputs_and_output,
495-
&mut indices,
496-
);
497-
// Converse of above, if this is a function/closure then the late-bound regions declared on its
498-
// signature are local.
499-
for_each_late_bound_region_in_item(self.infcx.tcx, self.mir_def, |r| {
500-
debug!(?r);
501-
if !indices.indices.contains_key(&r) {
492+
for (idx, bound_var) in bound_inputs_and_output.bound_vars().iter().enumerate() {
493+
if let ty::BoundVariableKind::Region(kind) = bound_var {
494+
let kind = ty::LateParamRegionKind::from_bound(ty::BoundVar::from_usize(idx), kind);
495+
let r = ty::Region::new_late_param(self.infcx.tcx, self.mir_def.to_def_id(), kind);
502496
let region_vid = {
503497
let name = r.get_name_or_anon();
504498
self.infcx.next_nll_region_var(FR, || RegionCtxt::LateBound(name))
@@ -507,7 +501,12 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
507501
debug!(?region_vid);
508502
indices.insert_late_bound_region(r, region_vid.as_var());
509503
}
510-
});
504+
}
505+
let inputs_and_output = self.infcx.replace_bound_regions_with_nll_infer_vars(
506+
self.mir_def,
507+
bound_inputs_and_output,
508+
&indices,
509+
);
511510

512511
let (unnormalized_output_ty, mut unnormalized_input_tys) =
513512
inputs_and_output.split_last().unwrap();
@@ -832,10 +831,9 @@ impl<'tcx> BorrowckInferCtxt<'tcx> {
832831
#[instrument(level = "debug", skip(self, indices))]
833832
fn replace_bound_regions_with_nll_infer_vars<T>(
834833
&self,
835-
origin: NllRegionVariableOrigin,
836834
all_outlive_scope: LocalDefId,
837835
value: ty::Binder<'tcx, T>,
838-
indices: &mut UniversalRegionIndices<'tcx>,
836+
indices: &UniversalRegionIndices<'tcx>,
839837
) -> T
840838
where
841839
T: TypeFoldable<TyCtxt<'tcx>>,
@@ -845,18 +843,7 @@ impl<'tcx> BorrowckInferCtxt<'tcx> {
845843
let kind = ty::LateParamRegionKind::from_bound(br.var, br.kind);
846844
let liberated_region =
847845
ty::Region::new_late_param(self.tcx, all_outlive_scope.to_def_id(), kind);
848-
let region_vid = {
849-
let name = match br.kind.get_name() {
850-
Some(name) => name,
851-
_ => sym::anon,
852-
};
853-
854-
self.next_nll_region_var(origin, || RegionCtxt::Bound(name))
855-
};
856-
857-
indices.insert_late_bound_region(liberated_region, region_vid.as_var());
858-
debug!(?liberated_region, ?region_vid);
859-
region_vid
846+
ty::Region::new_var(self.tcx, indices.to_region_vid(liberated_region))
860847
});
861848
value
862849
}
@@ -870,7 +857,7 @@ impl<'tcx> UniversalRegionIndices<'tcx> {
870857
/// well. These are used for error reporting.
871858
fn insert_late_bound_region(&mut self, r: ty::Region<'tcx>, vid: ty::RegionVid) {
872859
debug!("insert_late_bound_region({:?}, {:?})", r, vid);
873-
self.indices.insert(r, vid);
860+
assert_eq!(self.indices.insert(r, vid), None);
874861
}
875862

876863
/// Converts `r` into a local inference variable: `r` can either

Diff for: tests/ui/closures/binder/nested-closures-regions.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | for<'a> || -> () { for<'c> |_: &'a ()| -> () {}; };
99
extern "rust-call" fn((&(),)),
1010
(),
1111
]
12-
= note: late-bound region is '?4
12+
= note: late-bound region is '?3
1313
= note: late-bound region is '?2
1414
= note: number of external vids: 3
1515
= note: where '?1: '?2
@@ -26,7 +26,7 @@ LL | for<'a> || -> () { for<'c> |_: &'a ()| -> () {}; };
2626
extern "rust-call" fn(()),
2727
(),
2828
]
29-
= note: late-bound region is '?2
29+
= note: late-bound region is '?1
3030

3131
note: no external requirements
3232
--> $DIR/nested-closures-regions.rs:7:1

Diff for: tests/ui/nll/closure-requirements/escape-argument-callee.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ LL | let mut closure = expect_sig(|p, y| *p = y);
1717
| - - ^^^^^^ assignment requires that `'1` must outlive `'2`
1818
| | |
1919
| | has type `&'1 i32`
20-
| has type `&'?2 mut &'2 i32`
20+
| has type `&'?1 mut &'2 i32`
2121

2222
note: no external requirements
2323
--> $DIR/escape-argument-callee.rs:20:1

Diff for: tests/ui/nll/closure-requirements/propagate-approximated-shorter-to-static-comparing-against-free.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ LL | foo(cell, |cell_a, cell_x| {
2020
LL | cell_a.set(cell_x.get()); // forces 'x: 'a, error in closure
2121
| ^^^^^^^^^^^^^^^^^^^^^^^^ `cell_x` escapes the closure body here
2222
|
23-
= note: requirement occurs because of the type `Cell<&'?8 u32>`, which makes the generic argument `&'?8 u32` invariant
23+
= note: requirement occurs because of the type `Cell<&'?9 u32>`, which makes the generic argument `&'?9 u32` invariant
2424
= note: the struct `Cell<T>` is invariant over the parameter `T`
2525
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
2626

Diff for: tests/ui/nll/closure-requirements/propagate-fail-to-approximate-longer-no-bounds.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ error: lifetime may not live long enough
1616
--> $DIR/propagate-fail-to-approximate-longer-no-bounds.rs:37:9
1717
|
1818
LL | establish_relationships(&cell_a, &cell_b, |_outlives, x, y| {
19-
| --------- - has type `&'?7 Cell<&'1 u32>`
19+
| --------- - has type `&'?6 Cell<&'1 u32>`
2020
| |
21-
| has type `&'?5 Cell<&'2 &'?1 u32>`
21+
| has type `&'?4 Cell<&'2 &'?1 u32>`
2222
LL | // Only works if 'x: 'y:
2323
LL | demand_y(x, y, x.get())
2424
| ^^^^^^^^^^^^^^^^^^^^^^^ argument requires that `'1` must outlive `'2`

Diff for: tests/ui/nll/closure-requirements/propagate-fail-to-approximate-longer-wrong-bounds.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ error: lifetime may not live long enough
1616
--> $DIR/propagate-fail-to-approximate-longer-wrong-bounds.rs:41:9
1717
|
1818
LL | establish_relationships(&cell_a, &cell_b, |_outlives1, _outlives2, x, y| {
19-
| ---------- ---------- has type `&'?8 Cell<&'2 &'?2 u32>`
19+
| ---------- ---------- has type `&'?7 Cell<&'2 &'?2 u32>`
2020
| |
21-
| has type `&'?6 Cell<&'1 &'?1 u32>`
21+
| has type `&'?5 Cell<&'1 &'?1 u32>`
2222
LL | // Only works if 'x: 'y:
2323
LL | demand_y(x, y, x.get())
2424
| ^^^^^^^^^^^^^^^^^^^^^^^ argument requires that `'1` must outlive `'2`

0 commit comments

Comments
 (0)