Skip to content

Commit aae0f54

Browse files
No resume argument in the drop shim
1 parent 3bb8ecb commit aae0f54

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

src/librustc_mir/transform/generator.rs

+33-18
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ fn create_generator_drop_shim<'tcx>(
891891

892892
let source_info = source_info(&body);
893893

894-
let mut cases = create_cases(&mut body, transform, |point| point.drop);
894+
let mut cases = create_cases(&mut body, transform, Operation::Drop);
895895

896896
cases.insert(0, (UNRESUMED, drop_clean));
897897

@@ -1009,7 +1009,7 @@ fn create_generator_resume_function<'tcx>(
10091009
}
10101010
}
10111011

1012-
let mut cases = create_cases(body, &transform, |point| Some(point.resume));
1012+
let mut cases = create_cases(body, &transform, Operation::Resume);
10131013

10141014
use rustc::mir::interpret::PanicInfo::{ResumedAfterPanic, ResumedAfterReturn};
10151015

@@ -1059,22 +1059,35 @@ fn insert_clean_drop(body: &mut BodyAndCache<'_>) -> BasicBlock {
10591059
drop_clean
10601060
}
10611061

1062-
fn create_cases<'tcx, F>(
1062+
/// An operation that can be performed on a generator.
1063+
#[derive(PartialEq, Copy, Clone)]
1064+
enum Operation {
1065+
Resume,
1066+
Drop,
1067+
}
1068+
1069+
impl Operation {
1070+
fn target_block(self, point: &SuspensionPoint<'_>) -> Option<BasicBlock> {
1071+
match self {
1072+
Operation::Resume => Some(point.resume),
1073+
Operation::Drop => point.drop,
1074+
}
1075+
}
1076+
}
1077+
1078+
fn create_cases<'tcx>(
10631079
body: &mut BodyAndCache<'tcx>,
10641080
transform: &TransformVisitor<'tcx>,
1065-
target: F,
1066-
) -> Vec<(usize, BasicBlock)>
1067-
where
1068-
F: Fn(&SuspensionPoint<'tcx>) -> Option<BasicBlock>,
1069-
{
1081+
operation: Operation,
1082+
) -> Vec<(usize, BasicBlock)> {
10701083
let source_info = source_info(body);
10711084

10721085
transform
10731086
.suspension_points
10741087
.iter()
10751088
.filter_map(|point| {
10761089
// Find the target for this suspension point, if applicable
1077-
target(point).map(|target| {
1090+
operation.target_block(point).map(|target| {
10781091
let block = BasicBlock::new(body.basic_blocks().len());
10791092
let mut statements = Vec::new();
10801093

@@ -1087,15 +1100,17 @@ where
10871100
}
10881101
}
10891102

1090-
// Move the resume argument to the destination place of the `Yield` terminator
1091-
let resume_arg = Local::new(2); // 0 = return, 1 = self
1092-
statements.push(Statement {
1093-
source_info,
1094-
kind: StatementKind::Assign(box (
1095-
point.resume_arg,
1096-
Rvalue::Use(Operand::Move(resume_arg.into())),
1097-
)),
1098-
});
1103+
if operation == Operation::Resume {
1104+
// Move the resume argument to the destination place of the `Yield` terminator
1105+
let resume_arg = Local::new(2); // 0 = return, 1 = self
1106+
statements.push(Statement {
1107+
source_info,
1108+
kind: StatementKind::Assign(box (
1109+
point.resume_arg,
1110+
Rvalue::Use(Operand::Move(resume_arg.into())),
1111+
)),
1112+
});
1113+
}
10991114

11001115
// Then jump to the real target
11011116
body.basic_blocks_mut().push(BasicBlockData {

src/test/mir-opt/generator-drop-cleanup.rs

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ fn main() {
4040
// StorageLive(_2);
4141
// StorageLive(_3);
4242
// StorageLive(_4);
43-
// _3 = move _2;
4443
// goto -> bb1;
4544
// }
4645
// bb8: {

0 commit comments

Comments
 (0)