Skip to content

Commit 0a56eb1

Browse files
committed
Auto merge of #88698 - Noble-Mushtak:master, r=nikomatsakis,oli-obk
Add check that live_region is live in sanitize_promoted This pull request fixes #88434 by adding a check in `sanitize_promoted` to ensure that only regions which are actually live are added to the `liveness_constraints` of the `BorrowCheckContext`. To implement this change, I needed to add a method to `LivenessValues` which gets the elements contained by a region: /// Returns an iterator of all the elements contained by the region `r` crate fn get_elements(&self, row: N) -> impl Iterator<Item = Location> + '_ Then, inside `sanitize_promoted`, we check whether the iterator returned by this method is non-empty to ensure that the region is actually live at at least one location before adding that region to the `liveness_constraints` of the `BorrowCheckContext`. This is my first pull request to the Rust repo, so any feedback on how I can improve this pull request or if there is a better way to fix this issue would be very appreciated.
2 parents c34ac87 + 8fc329f commit 0a56eb1

File tree

7 files changed

+83
-16
lines changed

7 files changed

+83
-16
lines changed

compiler/rustc_borrowck/src/region_infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2154,7 +2154,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
21542154
// appears to be the most interesting point to report to the
21552155
// user via an even more ad-hoc guess.
21562156
categorized_path.sort_by(|p0, p1| p0.category.cmp(&p1.category));
2157-
debug!("`: sorted_path={:#?}", categorized_path);
2157+
debug!("best_blame_constraint: sorted_path={:#?}", categorized_path);
21582158

21592159
categorized_path.remove(0)
21602160
}

compiler/rustc_borrowck/src/region_infer/values.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,19 @@ impl<N: Idx> LivenessValues<N> {
174174
self.points.contains(row, index)
175175
}
176176

177+
/// Returns an iterator of all the elements contained by the region `r`
178+
crate fn get_elements(&self, row: N) -> impl Iterator<Item = Location> + '_ {
179+
self.points
180+
.row(row)
181+
.into_iter()
182+
.flat_map(|set| set.iter())
183+
.take_while(move |&p| self.elements.point_in_range(p))
184+
.map(move |p| self.elements.to_location(p))
185+
}
186+
177187
/// Returns a "pretty" string value of the region. Meant for debugging.
178188
crate fn region_value_str(&self, r: N) -> String {
179-
region_value_str(
180-
self.points
181-
.row(r)
182-
.into_iter()
183-
.flat_map(|set| set.iter())
184-
.take_while(|&p| self.elements.point_in_range(p))
185-
.map(|p| self.elements.to_location(p))
186-
.map(RegionElement::Location),
187-
)
189+
region_value_str(self.get_elements(r).map(RegionElement::Location))
188190
}
189191
}
190192

compiler/rustc_borrowck/src/type_check/mod.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -663,12 +663,17 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
663663
}
664664
self.cx.borrowck_context.constraints.outlives_constraints.push(constraint)
665665
}
666-
for live_region in liveness_constraints.rows() {
667-
self.cx
668-
.borrowck_context
669-
.constraints
670-
.liveness_constraints
671-
.add_element(live_region, location);
666+
for region in liveness_constraints.rows() {
667+
// If the region is live at at least one location in the promoted MIR,
668+
// then add a liveness constraint to the main MIR for this region
669+
// at the location provided as an argument to this method
670+
if let Some(_) = liveness_constraints.get_elements(region).next() {
671+
self.cx
672+
.borrowck_context
673+
.constraints
674+
.liveness_constraints
675+
.add_element(region, location);
676+
}
672677
}
673678

674679
if !closure_bounds.is_empty() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(const_fn_trait_bound)]
2+
// Regression test related to issue 88434
3+
4+
const _CONST: &() = &f(&|_| {});
5+
6+
const fn f<F>(_: &F)
7+
where
8+
F: FnMut(&u8),
9+
{
10+
panic!() //~ ERROR evaluation of constant value failed
11+
}
12+
13+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0080]: evaluation of constant value failed
2+
--> $DIR/issue-88434-minimal-example.rs:10:5
3+
|
4+
LL | const _CONST: &() = &f(&|_| {});
5+
| ---------- inside `_CONST` at $DIR/issue-88434-minimal-example.rs:4:22
6+
...
7+
LL | panic!()
8+
| ^^^^^^^^
9+
| |
10+
| the evaluated program panicked at 'explicit panic', $DIR/issue-88434-minimal-example.rs:10:5
11+
| inside `f::<[closure@$DIR/issue-88434-minimal-example.rs:4:25: 4:31]>` at $SRC_DIR/std/src/panic.rs:LL:COL
12+
|
13+
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(const_fn_trait_bound)]
2+
// Regression test for issue 88434
3+
4+
const _CONST: &[u8] = &f(&[], |_| {});
5+
6+
const fn f<F>(_: &[u8], _: F) -> &[u8]
7+
where
8+
F: FnMut(&u8),
9+
{
10+
panic!() //~ ERROR evaluation of constant value failed
11+
}
12+
13+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0080]: evaluation of constant value failed
2+
--> $DIR/issue-88434-removal-index-should-be-less.rs:10:5
3+
|
4+
LL | const _CONST: &[u8] = &f(&[], |_| {});
5+
| -------------- inside `_CONST` at $DIR/issue-88434-removal-index-should-be-less.rs:4:24
6+
...
7+
LL | panic!()
8+
| ^^^^^^^^
9+
| |
10+
| the evaluated program panicked at 'explicit panic', $DIR/issue-88434-removal-index-should-be-less.rs:10:5
11+
| inside `f::<[closure@$DIR/issue-88434-removal-index-should-be-less.rs:4:31: 4:37]>` at $SRC_DIR/std/src/panic.rs:LL:COL
12+
|
13+
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)