Skip to content

Commit 8d3d3a8

Browse files
authored
Rollup merge of #120330 - compiler-errors:no-coroutine-info-in-coroutine-drop-body, r=nnethercote
Remove coroutine info when building coroutine drop body Coroutine drop shims are not themselves coroutines, so erase the "`coroutine`" field from the body so that helper fns like `yield_ty` and `coroutine_kind` properly return `None` for the drop shim.
2 parents e47ba63 + 07b7c77 commit 8d3d3a8

File tree

4 files changed

+21
-47
lines changed

4 files changed

+21
-47
lines changed

compiler/rustc_middle/src/mir/mod.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,23 @@ impl<'tcx> MirSource<'tcx> {
244244
}
245245
}
246246

247+
/// Additional information carried by a MIR body when it is lowered from a coroutine.
248+
/// This information is modified as it is lowered during the `StateTransform` MIR pass,
249+
/// so not all fields will be active at a given time. For example, the `yield_ty` is
250+
/// taken out of the field after yields are turned into returns, and the `coroutine_drop`
251+
/// body is only populated after the state transform pass.
247252
#[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable, TypeVisitable)]
248253
pub struct CoroutineInfo<'tcx> {
249-
/// The yield type of the function, if it is a coroutine.
254+
/// The yield type of the function. This field is removed after the state transform pass.
250255
pub yield_ty: Option<Ty<'tcx>>,
251256

252-
/// The resume type of the function, if it is a coroutine.
257+
/// The resume type of the function. This field is removed after the state transform pass.
253258
pub resume_ty: Option<Ty<'tcx>>,
254259

255-
/// Coroutine drop glue.
260+
/// Coroutine drop glue. This field is populated after the state transform pass.
256261
pub coroutine_drop: Option<Body<'tcx>>,
257262

258-
/// The layout of a coroutine. Produced by the state transformation.
263+
/// The layout of a coroutine. This field is populated after the state transform pass.
259264
pub coroutine_layout: Option<CoroutineLayout<'tcx>>,
260265

261266
/// If this is a coroutine then record the type of source expression that caused this coroutine
@@ -303,6 +308,12 @@ pub struct Body<'tcx> {
303308
/// and used for debuginfo. Indexed by a `SourceScope`.
304309
pub source_scopes: IndexVec<SourceScope, SourceScopeData<'tcx>>,
305310

311+
/// Additional information carried by a MIR body when it is lowered from a coroutine.
312+
///
313+
/// Note that the coroutine drop shim, any promoted consts, and other synthetic MIR
314+
/// bodies that come from processing a coroutine body are not typically coroutines
315+
/// themselves, and should probably set this to `None` to avoid carrying redundant
316+
/// information.
306317
pub coroutine: Option<Box<CoroutineInfo<'tcx>>>,
307318

308319
/// Declarations of locals.

compiler/rustc_mir_transform/src/coroutine.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,12 @@ fn create_coroutine_drop_shim<'tcx>(
12311231
drop_clean: BasicBlock,
12321232
) -> Body<'tcx> {
12331233
let mut body = body.clone();
1234-
body.arg_count = 1; // make sure the resume argument is not included here
1234+
// Take the coroutine info out of the body, since the drop shim is
1235+
// not a coroutine body itself; it just has its drop built out of it.
1236+
let _ = body.coroutine.take();
1237+
// Make sure the resume argument is not included here, since we're
1238+
// building a body for `drop_in_place`.
1239+
body.arg_count = 1;
12351240

12361241
let source_info = SourceInfo::outermost(body.span);
12371242

tests/mir-opt/coroutine_drop_cleanup.main-{closure#0}.coroutine_drop.0.panic-abort.mir

-21
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,4 @@
11
// MIR for `main::{closure#0}` 0 coroutine_drop
2-
/* coroutine_layout = CoroutineLayout {
3-
field_tys: {
4-
_0: CoroutineSavedTy {
5-
ty: std::string::String,
6-
source_info: SourceInfo {
7-
span: $DIR/coroutine_drop_cleanup.rs:12:13: 12:15 (#0),
8-
scope: scope[0],
9-
},
10-
ignore_for_traits: false,
11-
},
12-
},
13-
variant_fields: {
14-
Unresumed(0): [],
15-
Returned (1): [],
16-
Panicked (2): [],
17-
Suspend0 (3): [_0],
18-
},
19-
storage_conflicts: BitMatrix(1x1) {
20-
(_0, _0),
21-
},
22-
} */
232

243
fn main::{closure#0}(_1: *mut {coroutine@$DIR/coroutine_drop_cleanup.rs:11:15: 11:17}) -> () {
254
let mut _0: ();

tests/mir-opt/coroutine_drop_cleanup.main-{closure#0}.coroutine_drop.0.panic-unwind.mir

-21
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,4 @@
11
// MIR for `main::{closure#0}` 0 coroutine_drop
2-
/* coroutine_layout = CoroutineLayout {
3-
field_tys: {
4-
_0: CoroutineSavedTy {
5-
ty: std::string::String,
6-
source_info: SourceInfo {
7-
span: $DIR/coroutine_drop_cleanup.rs:12:13: 12:15 (#0),
8-
scope: scope[0],
9-
},
10-
ignore_for_traits: false,
11-
},
12-
},
13-
variant_fields: {
14-
Unresumed(0): [],
15-
Returned (1): [],
16-
Panicked (2): [],
17-
Suspend0 (3): [_0],
18-
},
19-
storage_conflicts: BitMatrix(1x1) {
20-
(_0, _0),
21-
},
22-
} */
232

243
fn main::{closure#0}(_1: *mut {coroutine@$DIR/coroutine_drop_cleanup.rs:11:15: 11:17}) -> () {
254
let mut _0: ();

0 commit comments

Comments
 (0)