Skip to content

Commit d46a207

Browse files
committed
Deduplicate field and variant visitor methods
1 parent b174b0b commit d46a207

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

src/librustc_mir/interpret/validity.rs

+22-25
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,12 @@ struct ValidityVisitor<'rt, 'a: 'rt, 'mir: 'rt, 'tcx: 'a+'rt+'mir, M: Machine<'a
167167
}
168168

169169
impl<'rt, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> ValidityVisitor<'rt, 'a, 'mir, 'tcx, M> {
170-
fn push_aggregate_field_path_elem(
170+
fn aggregate_field_path_elem(
171171
&mut self,
172172
layout: TyLayout<'tcx>,
173173
field: usize,
174-
) {
175-
let elem = match layout.ty.sty {
174+
) -> PathElem {
175+
match layout.ty.sty {
176176
// generators and closures.
177177
ty::Closure(def_id, _) | ty::Generator(def_id, _, _) => {
178178
if let Some(upvar) = self.ecx.tcx.optimized_mir(def_id).upvar_decls.get(field) {
@@ -194,11 +194,7 @@ impl<'rt, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> ValidityVisitor<'rt, 'a, '
194194
layout::Variants::Single { index } =>
195195
// Inside a variant
196196
PathElem::Field(def.variants[index].fields[field].ident.name),
197-
_ => {
198-
// Enums have no fields other than their tag
199-
assert_eq!(field, 0);
200-
PathElem::Tag
201-
}
197+
_ => bug!(),
202198
}
203199
}
204200

@@ -213,8 +209,22 @@ impl<'rt, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> ValidityVisitor<'rt, 'a, '
213209

214210
// nothing else has an aggregate layout
215211
_ => bug!("aggregate_field_path_elem: got non-aggregate type {:?}", layout.ty),
216-
};
212+
}
213+
}
214+
215+
fn visit_elem(
216+
&mut self,
217+
new_op: OpTy<'tcx, M::PointerTag>,
218+
elem: PathElem,
219+
) -> EvalResult<'tcx> {
220+
// Remember the old state
221+
let path_len = self.path.len();
222+
// Perform operation
217223
self.path.push(elem);
224+
self.visit_value(new_op)?;
225+
// Undo changes
226+
self.path.truncate(path_len);
227+
Ok(())
218228
}
219229
}
220230

@@ -235,14 +245,8 @@ impl<'rt, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>>
235245
field: usize,
236246
new_op: OpTy<'tcx, M::PointerTag>
237247
) -> EvalResult<'tcx> {
238-
// Remember the old state
239-
let path_len = self.path.len();
240-
// Perform operation
241-
self.push_aggregate_field_path_elem(old_op.layout, field);
242-
self.visit_value(new_op)?;
243-
// Undo changes
244-
self.path.truncate(path_len);
245-
Ok(())
248+
let elem = self.aggregate_field_path_elem(old_op.layout, field);
249+
self.visit_elem(new_op, elem)
246250
}
247251

248252
#[inline]
@@ -252,15 +256,8 @@ impl<'rt, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>>
252256
variant_id: VariantIdx,
253257
new_op: OpTy<'tcx, M::PointerTag>
254258
) -> EvalResult<'tcx> {
255-
// Remember the old state
256-
let path_len = self.path.len();
257-
// Perform operation
258259
let name = old_op.layout.ty.ty_adt_def().unwrap().variants[variant_id].name;
259-
self.path.push(PathElem::Variant(name));
260-
self.visit_value(new_op)?;
261-
// Undo changes
262-
self.path.truncate(path_len);
263-
Ok(())
260+
self.visit_elem(new_op, PathElem::Variant(name))
264261
}
265262

266263
#[inline]

0 commit comments

Comments
 (0)