Skip to content

Commit cec8e09

Browse files
committed
Run AbortUnwindingCalls after generator transform
1 parent cfbf1bf commit cec8e09

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

compiler/rustc_mir_transform/src/generator.rs

+16
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@
5050
//! For generators with state 1 (returned) and state 2 (poisoned) it does nothing.
5151
//! Otherwise it drops all the values in scope at the last suspension point.
5252
53+
use crate::abort_unwinding_calls;
5354
use crate::deref_separator::deref_finder;
5455
use crate::errors;
56+
use crate::pass_manager as pm;
5557
use crate::simplify;
5658
use crate::MirPass;
5759
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -64,6 +66,7 @@ use rustc_index::{Idx, IndexVec};
6466
use rustc_middle::mir::dump_mir;
6567
use rustc_middle::mir::visit::{MutVisitor, PlaceContext, Visitor};
6668
use rustc_middle::mir::*;
69+
use rustc_middle::ty::InstanceDef;
6770
use rustc_middle::ty::{self, AdtDef, Ty, TyCtxt};
6871
use rustc_middle::ty::{GeneratorArgs, GenericArgsRef};
6972
use rustc_mir_dataflow::impls::{
@@ -1147,6 +1150,17 @@ fn create_generator_drop_shim<'tcx>(
11471150
// unrelated code from the resume part of the function
11481151
simplify::remove_dead_blocks(tcx, &mut body);
11491152

1153+
// Update the body's def to become the drop glue.
1154+
let drop_in_place = tcx.require_lang_item(LangItem::DropInPlace, None);
1155+
body.source.instance = InstanceDef::DropGlue(drop_in_place, Some(gen_ty));
1156+
1157+
pm::run_passes_no_validate(
1158+
tcx,
1159+
&mut body,
1160+
&[&abort_unwinding_calls::AbortUnwindingCalls],
1161+
None,
1162+
);
1163+
11501164
dump_mir(tcx, false, "generator_drop", &0, &body, |_, _| Ok(()));
11511165

11521166
body
@@ -1317,6 +1331,8 @@ fn create_generator_resume_function<'tcx>(
13171331
// unrelated code from the drop part of the function
13181332
simplify::remove_dead_blocks(tcx, body);
13191333

1334+
pm::run_passes_no_validate(tcx, body, &[&abort_unwinding_calls::AbortUnwindingCalls], None);
1335+
13201336
dump_mir(tcx, false, "generator_resume", &0, body, |_, _| Ok(()));
13211337
}
13221338

tests/mir-opt/building/async_await.a-{closure#0}.generator_resume.0.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn a::{closure#0}(_1: Pin<&mut [async fn body@$DIR/async_await.rs:11:14: 11:16]>
3030
}
3131

3232
bb2: {
33-
assert(const false, "`async fn` resumed after completion") -> [success: bb2, unwind continue];
33+
assert(const false, "`async fn` resumed after completion") -> [success: bb2, unwind unreachable];
3434
}
3535

3636
bb3: {

tests/mir-opt/building/async_await.b-{closure#0}.generator_resume.0.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ fn b::{closure#0}(_1: Pin<&mut [async fn body@$DIR/async_await.rs:14:18: 17:2]>,
310310
}
311311

312312
bb28: {
313-
assert(const false, "`async fn` resumed after completion") -> [success: bb28, unwind continue];
313+
assert(const false, "`async fn` resumed after completion") -> [success: bb28, unwind unreachable];
314314
}
315315

316316
bb29: {

tests/run-make/panic-abort-eh_frame/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
include ../tools.mk
77

88
all:
9-
$(RUSTC) foo.rs --crate-type=lib --emit=obj=$(TMPDIR)/foo.o -Cpanic=abort
9+
$(RUSTC) foo.rs --crate-type=lib --emit=obj=$(TMPDIR)/foo.o -Cpanic=abort --edition 2021 -Z validate-mir
1010
objdump --dwarf=frames $(TMPDIR)/foo.o | $(CGREP) -v 'DW_CFA'

tests/run-make/panic-abort-eh_frame/foo.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
#![no_std]
22

3+
use core::future::Future;
4+
5+
pub struct NeedsDrop;
6+
7+
impl Drop for NeedsDrop {
8+
fn drop(&mut self) {}
9+
}
10+
311
#[panic_handler]
412
fn handler(_: &core::panic::PanicInfo<'_>) -> ! {
513
loop {}
@@ -8,3 +16,19 @@ fn handler(_: &core::panic::PanicInfo<'_>) -> ! {
816
pub unsafe fn oops(x: *const u32) -> u32 {
917
*x
1018
}
19+
20+
pub async fn foo(_: NeedsDrop) {
21+
async fn bar() {}
22+
bar().await;
23+
}
24+
25+
pub fn poll_foo(x: &mut core::task::Context<'_>) {
26+
let _g = NeedsDrop;
27+
let mut p = core::pin::pin!(foo(NeedsDrop));
28+
let _ = p.as_mut().poll(x);
29+
let _ = p.as_mut().poll(x);
30+
}
31+
32+
pub fn drop_foo() {
33+
drop(foo(NeedsDrop));
34+
}

0 commit comments

Comments
 (0)