Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 74b93f3

Browse files
committed
Add a self_arg convenience
1 parent 8c97ffa commit 74b93f3

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/librustc_mir/transform/generator.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<'tcx> MutVisitor<'tcx> for DerefArgVisitor {
5252
lvalue: &mut Lvalue<'tcx>,
5353
context: LvalueContext<'tcx>,
5454
location: Location) {
55-
if *lvalue == Lvalue::Local(Local::new(1)) {
55+
if *lvalue == Lvalue::Local(self_arg()) {
5656
*lvalue = Lvalue::Projection(Box::new(Projection {
5757
base: lvalue.clone(),
5858
elem: ProjectionElem::Deref,
@@ -63,6 +63,10 @@ impl<'tcx> MutVisitor<'tcx> for DerefArgVisitor {
6363
}
6464
}
6565

66+
fn self_arg() -> Local {
67+
Local::new(1)
68+
}
69+
6670
struct TransformVisitor<'a, 'tcx: 'a> {
6771
tcx: TyCtxt<'a, 'tcx, 'tcx>,
6872
state_adt_ref: &'tcx AdtDef,
@@ -97,7 +101,7 @@ impl<'a, 'tcx> TransformVisitor<'a, 'tcx> {
97101

98102
// Create a Lvalue referencing a generator struct field
99103
fn make_field(&self, idx: usize, ty: Ty<'tcx>) -> Lvalue<'tcx> {
100-
let base = Lvalue::Local(Local::new(1));
104+
let base = Lvalue::Local(self_arg());
101105
let field = Projection {
102106
base: base,
103107
elem: ProjectionElem::Field(Field::new(idx), ty),
@@ -256,7 +260,7 @@ fn locals_live_across_suspend_points<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
256260
}
257261

258262
// The generator argument is ignored
259-
set.remove(&Local::new(1));
263+
set.remove(&self_arg());
260264

261265
set
262266
}
@@ -334,7 +338,7 @@ fn elaborate_generator_drops<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
334338
use shim::DropShimElaborator;
335339

336340
let param_env = tcx.param_env(def_id);
337-
let gen = Local::new(1);
341+
let gen = self_arg();
338342

339343
for block in mir.basic_blocks().indices() {
340344
let (target, unwind, source_info) = match mir.basic_blocks()[block].terminator() {
@@ -452,7 +456,7 @@ fn create_generator_drop_shim<'a, 'tcx>(
452456
make_generator_state_argument_indirect(tcx, def_id, &mut mir);
453457

454458
// Change the generator argument from &mut to *mut
455-
mir.local_decls[Local::new(1)] = LocalDecl {
459+
mir.local_decls[self_arg()] = LocalDecl {
456460
mutability: Mutability::Mut,
457461
ty: tcx.mk_ptr(ty::TypeAndMut {
458462
ty: gen_ty,
@@ -604,7 +608,7 @@ fn insert_clean_drop<'a, 'tcx>(mir: &mut Mir<'tcx>) -> BasicBlock {
604608
// Create a block to destroy an unresumed generators. This can only destroy upvars.
605609
let drop_clean = BasicBlock::new(mir.basic_blocks().len());
606610
let term = TerminatorKind::Drop {
607-
location: Lvalue::Local(Local::new(1)),
611+
location: Lvalue::Local(self_arg()),
608612
target: return_block,
609613
unwind: None,
610614
};

0 commit comments

Comments
 (0)