Skip to content

Commit c9a7f8a

Browse files
authored
Rollup merge of #152103 - eggyal:caught-divergence-not-unused, r=cjgillot
Consider captures to be used by closures that unwind Assignments to a captured variable within a diverging closure should not be considered unused if the divergence is caught. This patch considers such assignments/captures to be used by diverging closures irrespective of whether the divergence is caught, but better a false negative unused lint than a false positive one (the latter having caused a stable-to-stable regression). Fixes #152079 r? compiler
2 parents 494c6da + 58292e2 commit c9a7f8a

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

compiler/rustc_mir_transform/src/liveness.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,7 @@ impl<'tcx> Visitor<'tcx> for TransferFunction<'_, 'tcx> {
12911291
TerminatorKind::Return
12921292
| TerminatorKind::Yield { .. }
12931293
| TerminatorKind::Goto { target: START_BLOCK } // Inserted for the `FnMut` case.
1294+
| TerminatorKind::Call { target: None, .. } // unwinding could be caught
12941295
if self.capture_kind != CaptureKind::None =>
12951296
{
12961297
// All indirect captures have an effect on the environment, so we mark them as live.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//! Assignments to a captured variable within a diverging closure should not be considered unused if
2+
//! the divergence is caught.
3+
//!
4+
//! Regression test for https://github.com/rust-lang/rust/issues/152079
5+
//@ compile-flags: -Wunused
6+
//@ check-pass
7+
8+
fn main() {
9+
let mut x = 1;
10+
catch(|| {
11+
x = 2;
12+
panic!();
13+
});
14+
dbg!(x);
15+
}
16+
17+
fn catch<F: FnOnce()>(f: F) {
18+
if let Ok(true) = std::fs::exists("may_or_may_not_call_f") {
19+
_ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(f));
20+
}
21+
}

0 commit comments

Comments
 (0)