Skip to content

Commit ffabe32

Browse files
committed
Simplify the writing of tuple type info
1 parent 9c69bfd commit ffabe32

1 file changed

Lines changed: 15 additions & 37 deletions

File tree

compiler/rustc_const_eval/src/const_eval/type_info.rs

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
9797
.fields
9898
.len()
9999
);
100-
self.write_tuple_fields(tuple_place, fields, ty)?;
100+
self.write_tuple_type_info(tuple_place, fields, ty)?;
101101
variant
102102
}
103103
ty::Array(ty, len) => {
@@ -231,42 +231,6 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
231231
interp_ok(())
232232
}
233233

234-
// TODO(type_info): Remove this method, use `project_write_array` as it's more general.
235-
pub(crate) fn write_tuple_fields(
236-
&mut self,
237-
tuple_place: impl Writeable<'tcx, CtfeProvenance>,
238-
fields: &[Ty<'tcx>],
239-
tuple_ty: Ty<'tcx>,
240-
) -> InterpResult<'tcx> {
241-
// project into the `type_info::Tuple::fields` field
242-
let fields_slice_place = self.project_field(&tuple_place, FieldIdx::ZERO)?;
243-
// get the `type_info::Field` type from `fields: &[Field]`
244-
let field_type = fields_slice_place
245-
.layout()
246-
.ty
247-
.builtin_deref(false)
248-
.unwrap()
249-
.sequence_element_type(self.tcx.tcx);
250-
// Create an array with as many elements as the number of fields in the inspected tuple
251-
let fields_layout =
252-
self.layout_of(Ty::new_array(self.tcx.tcx, field_type, fields.len() as u64))?;
253-
let fields_place = self.allocate(fields_layout, MemoryKind::Stack)?;
254-
let mut fields_places = self.project_array_fields(&fields_place)?;
255-
256-
let tuple_layout = self.layout_of(tuple_ty)?;
257-
258-
while let Some((i, place)) = fields_places.next(self)? {
259-
let field_ty = fields[i as usize];
260-
self.write_field(field_ty, place, tuple_layout, None, i)?;
261-
}
262-
263-
let fields_place = fields_place.map_provenance(CtfeProvenance::as_immutable);
264-
265-
let ptr = Immediate::new_slice(fields_place.ptr(), fields.len() as u64, self);
266-
267-
self.write_immediate(ptr, &fields_slice_place)
268-
}
269-
270234
// Write fields for struct, enum variants
271235
fn write_variant_fields(
272236
&mut self,
@@ -320,6 +284,20 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
320284
interp_ok(())
321285
}
322286

287+
pub(crate) fn write_tuple_type_info(
288+
&mut self,
289+
tuple_place: impl Writeable<'tcx, CtfeProvenance>,
290+
fields: &[Ty<'tcx>],
291+
tuple_ty: Ty<'tcx>,
292+
) -> InterpResult<'tcx> {
293+
let tuple_layout = self.layout_of(tuple_ty)?;
294+
let fields_slice_place = self.project_field(&tuple_place, FieldIdx::ZERO)?;
295+
self.project_write_array(fields_slice_place, fields.len() as u64, |this, i, place| {
296+
let field_ty = fields[i as usize];
297+
this.write_field(field_ty, place, tuple_layout, None, i)
298+
})
299+
}
300+
323301
pub(crate) fn write_array_type_info(
324302
&mut self,
325303
place: impl Writeable<'tcx, CtfeProvenance>,

0 commit comments

Comments
 (0)