Skip to content

Commit 74ef9d2

Browse files
committed
Merge DerefArgVisitor and PinArgVisitor.
They are almost identical, differing only in the `ProjectionElem` they insert. This commit merges them into a new type `SelfArgVisitor`.
1 parent 510767c commit 74ef9d2

File tree

1 file changed

+7
-47
lines changed

1 file changed

+7
-47
lines changed

compiler/rustc_mir_transform/src/coroutine.rs

+7-47
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,12 @@ impl<'tcx> MutVisitor<'tcx> for RenameLocalVisitor<'tcx> {
112112
}
113113
}
114114

115-
struct DerefArgVisitor<'tcx> {
115+
struct SelfArgVisitor<'tcx> {
116+
elem: ProjectionElem<Local, Ty<'tcx>>,
116117
tcx: TyCtxt<'tcx>,
117118
}
118119

119-
impl<'tcx> MutVisitor<'tcx> for DerefArgVisitor<'tcx> {
120+
impl<'tcx> MutVisitor<'tcx> for SelfArgVisitor<'tcx> {
120121
fn tcx(&self) -> TyCtxt<'tcx> {
121122
self.tcx
122123
}
@@ -129,49 +130,7 @@ impl<'tcx> MutVisitor<'tcx> for DerefArgVisitor<'tcx> {
129130
if place.local == SELF_ARG {
130131
replace_base(
131132
place,
132-
Place {
133-
local: SELF_ARG,
134-
projection: self.tcx().mk_place_elems(&[ProjectionElem::Deref]),
135-
},
136-
self.tcx,
137-
);
138-
} else {
139-
self.visit_local(&mut place.local, context, location);
140-
141-
for elem in place.projection.iter() {
142-
if let PlaceElem::Index(local) = elem {
143-
assert_ne!(local, SELF_ARG);
144-
}
145-
}
146-
}
147-
}
148-
}
149-
150-
struct PinArgVisitor<'tcx> {
151-
ref_coroutine_ty: Ty<'tcx>,
152-
tcx: TyCtxt<'tcx>,
153-
}
154-
155-
impl<'tcx> MutVisitor<'tcx> for PinArgVisitor<'tcx> {
156-
fn tcx(&self) -> TyCtxt<'tcx> {
157-
self.tcx
158-
}
159-
160-
fn visit_local(&mut self, local: &mut Local, _: PlaceContext, _: Location) {
161-
assert_ne!(*local, SELF_ARG);
162-
}
163-
164-
fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
165-
if place.local == SELF_ARG {
166-
replace_base(
167-
place,
168-
Place {
169-
local: SELF_ARG,
170-
projection: self.tcx().mk_place_elems(&[ProjectionElem::Field(
171-
FieldIdx::ZERO,
172-
self.ref_coroutine_ty,
173-
)]),
174-
},
133+
Place { local: SELF_ARG, projection: self.tcx().mk_place_elems(&[self.elem]) },
175134
self.tcx,
176135
);
177136
} else {
@@ -568,7 +527,7 @@ fn make_coroutine_state_argument_indirect<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Bo
568527
body.local_decls.raw[1].ty = ref_coroutine_ty;
569528

570529
// Add a deref to accesses of the coroutine state
571-
DerefArgVisitor { tcx }.visit_body(body);
530+
SelfArgVisitor { tcx, elem: ProjectionElem::Deref }.visit_body(body);
572531
}
573532

574533
fn make_coroutine_state_argument_pinned<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
@@ -583,7 +542,8 @@ fn make_coroutine_state_argument_pinned<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body
583542
body.local_decls.raw[1].ty = pin_ref_coroutine_ty;
584543

585544
// Add the Pin field access to accesses of the coroutine state
586-
PinArgVisitor { ref_coroutine_ty, tcx }.visit_body(body);
545+
SelfArgVisitor { tcx, elem: ProjectionElem::Field(FieldIdx::ZERO, ref_coroutine_ty) }
546+
.visit_body(body);
587547
}
588548

589549
/// Allocates a new local and replaces all references of `local` with it. Returns the new local.

0 commit comments

Comments
 (0)