Skip to content

Commit 6ab6734

Browse files
committed
Move ADT related code to a sub module for type info
1 parent a575fe1 commit 6ab6734

File tree

2 files changed

+280
-267
lines changed

2 files changed

+280
-267
lines changed

compiler/rustc_const_eval/src/const_eval/type_info.rs

Lines changed: 4 additions & 267 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1+
mod adt;
2+
13
use std::borrow::Cow;
24

35
use rustc_abi::{FieldIdx, VariantIdx};
46
use rustc_ast::Mutability;
57
use rustc_hir::LangItem;
8+
use rustc_middle::span_bug;
69
use rustc_middle::ty::layout::TyAndLayout;
7-
use rustc_middle::ty::{
8-
self, AdtDef, AdtKind, Const, ConstKind, GenericArgKind, GenericArgs, Region, ScalarInt, Ty,
9-
VariantDef,
10-
};
11-
use rustc_middle::{bug, span_bug};
10+
use rustc_middle::ty::{self, Const, ScalarInt, Ty};
1211
use rustc_span::{Symbol, sym};
1312

1413
use crate::const_eval::CompileTimeMachine;
@@ -232,25 +231,6 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
232231
interp_ok(())
233232
}
234233

235-
// Write fields for struct, enum variants
236-
fn write_variant_fields(
237-
&mut self,
238-
place: impl Writeable<'tcx, CtfeProvenance>,
239-
variant_def: &'tcx VariantDef,
240-
variant_layout: TyAndLayout<'tcx>,
241-
generics: &'tcx GenericArgs<'tcx>,
242-
) -> InterpResult<'tcx> {
243-
self.allocate_fill_and_write_slice_ptr(
244-
place,
245-
variant_def.fields.len() as u64,
246-
|this, i, place| {
247-
let field_def = &variant_def.fields[FieldIdx::from_usize(i as usize)];
248-
let field_ty = field_def.ty(*this.tcx, generics);
249-
this.write_field(field_ty, place, variant_layout, Some(field_def.name), i)
250-
},
251-
)
252-
}
253-
254234
fn write_field(
255235
&mut self,
256236
field_ty: Ty<'tcx>,
@@ -355,249 +335,6 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
355335
interp_ok(())
356336
}
357337

358-
// FIXME(type_info): No semver considerations for now
359-
pub(crate) fn write_adt_type_info(
360-
&mut self,
361-
place: &impl Writeable<'tcx, CtfeProvenance>,
362-
adt: (Ty<'tcx>, AdtDef<'tcx>),
363-
generics: &'tcx GenericArgs<'tcx>,
364-
) -> InterpResult<'tcx, VariantIdx> {
365-
let (adt_ty, adt_def) = adt;
366-
let variant_idx = match adt_def.adt_kind() {
367-
AdtKind::Struct => {
368-
let (variant, variant_place) = self.downcast(place, sym::Struct)?;
369-
let place = self.project_field(&variant_place, FieldIdx::ZERO)?;
370-
self.write_struct_type_info(
371-
place,
372-
(adt_ty, adt_def.variant(VariantIdx::ZERO)),
373-
generics,
374-
)?;
375-
variant
376-
}
377-
AdtKind::Union => {
378-
let (variant, variant_place) = self.downcast(place, sym::Union)?;
379-
let place = self.project_field(&variant_place, FieldIdx::ZERO)?;
380-
self.write_union_type_info(
381-
place,
382-
(adt_ty, adt_def.variant(VariantIdx::ZERO)),
383-
generics,
384-
)?;
385-
variant
386-
}
387-
AdtKind::Enum => {
388-
let (variant, variant_place) = self.downcast(place, sym::Enum)?;
389-
let place = self.project_field(&variant_place, FieldIdx::ZERO)?;
390-
self.write_enum_type_info(place, adt, generics)?;
391-
variant
392-
}
393-
};
394-
interp_ok(variant_idx)
395-
}
396-
397-
pub(crate) fn write_struct_type_info(
398-
&mut self,
399-
place: impl Writeable<'tcx, CtfeProvenance>,
400-
struct_: (Ty<'tcx>, &'tcx VariantDef),
401-
generics: &'tcx GenericArgs<'tcx>,
402-
) -> InterpResult<'tcx> {
403-
let (struct_ty, struct_def) = struct_;
404-
let struct_layout = self.layout_of(struct_ty)?;
405-
406-
for (field_idx, field) in
407-
place.layout().ty.ty_adt_def().unwrap().non_enum_variant().fields.iter_enumerated()
408-
{
409-
let field_place = self.project_field(&place, field_idx)?;
410-
411-
match field.name {
412-
sym::generics => self.write_generics(field_place, generics)?,
413-
sym::fields => {
414-
self.write_variant_fields(field_place, struct_def, struct_layout, generics)?
415-
}
416-
sym::non_exhaustive => {
417-
let is_non_exhaustive = struct_def.is_field_list_non_exhaustive();
418-
self.write_scalar(Scalar::from_bool(is_non_exhaustive), &field_place)?
419-
}
420-
other => span_bug!(self.tcx.def_span(field.did), "unimplemented field {other}"),
421-
}
422-
}
423-
424-
interp_ok(())
425-
}
426-
427-
pub(crate) fn write_union_type_info(
428-
&mut self,
429-
place: impl Writeable<'tcx, CtfeProvenance>,
430-
union_: (Ty<'tcx>, &'tcx VariantDef),
431-
generics: &'tcx GenericArgs<'tcx>,
432-
) -> InterpResult<'tcx> {
433-
let (union_ty, union_def) = union_;
434-
let union_layout = self.layout_of(union_ty)?;
435-
436-
for (field_idx, field) in
437-
place.layout().ty.ty_adt_def().unwrap().non_enum_variant().fields.iter_enumerated()
438-
{
439-
let field_place = self.project_field(&place, field_idx)?;
440-
441-
match field.name {
442-
sym::generics => self.write_generics(field_place, generics)?,
443-
sym::fields => {
444-
self.write_variant_fields(field_place, union_def, union_layout, generics)?
445-
}
446-
sym::non_exhaustive => {
447-
let is_non_exhaustive = union_def.is_field_list_non_exhaustive();
448-
self.write_scalar(Scalar::from_bool(is_non_exhaustive), &field_place)?
449-
}
450-
other => span_bug!(self.tcx.def_span(field.did), "unimplemented field {other}"),
451-
}
452-
}
453-
454-
interp_ok(())
455-
}
456-
457-
pub(crate) fn write_enum_type_info(
458-
&mut self,
459-
place: impl Writeable<'tcx, CtfeProvenance>,
460-
enum_: (Ty<'tcx>, AdtDef<'tcx>),
461-
generics: &'tcx GenericArgs<'tcx>,
462-
) -> InterpResult<'tcx> {
463-
let (enum_ty, enum_def) = enum_;
464-
let enum_layout = self.layout_of(enum_ty)?;
465-
466-
for (field_idx, field) in
467-
place.layout().ty.ty_adt_def().unwrap().non_enum_variant().fields.iter_enumerated()
468-
{
469-
let field_place = self.project_field(&place, field_idx)?;
470-
471-
match field.name {
472-
sym::generics => self.write_generics(field_place, generics)?,
473-
sym::variants => {
474-
self.allocate_fill_and_write_slice_ptr(
475-
field_place,
476-
enum_def.variants().len() as u64,
477-
|this, i, place| {
478-
let variant_idx = VariantIdx::from_usize(i as usize);
479-
let variant_def = &enum_def.variants()[variant_idx];
480-
let variant_layout = enum_layout.for_variant(this, variant_idx);
481-
this.write_enum_variant(place, (variant_layout, &variant_def), generics)
482-
},
483-
)?;
484-
}
485-
sym::non_exhaustive => {
486-
let is_non_exhaustive = enum_def.is_variant_list_non_exhaustive();
487-
self.write_scalar(Scalar::from_bool(is_non_exhaustive), &field_place)?
488-
}
489-
other => span_bug!(self.tcx.def_span(field.did), "unimplemented field {other}"),
490-
}
491-
}
492-
493-
interp_ok(())
494-
}
495-
496-
fn write_enum_variant(
497-
&mut self,
498-
place: impl Writeable<'tcx, CtfeProvenance>,
499-
variant: (TyAndLayout<'tcx>, &'tcx VariantDef),
500-
generics: &'tcx GenericArgs<'tcx>,
501-
) -> InterpResult<'tcx> {
502-
let (variant_layout, variant_def) = variant;
503-
504-
for (field_idx, field_def) in
505-
place.layout().ty.ty_adt_def().unwrap().non_enum_variant().fields.iter_enumerated()
506-
{
507-
let field_place = self.project_field(&place, field_idx)?;
508-
match field_def.name {
509-
sym::name => {
510-
let name_place = self.allocate_str_dedup(variant_def.name.as_str())?;
511-
let ptr = self.mplace_to_ref(&name_place)?;
512-
self.write_immediate(*ptr, &field_place)?
513-
}
514-
sym::fields => {
515-
self.write_variant_fields(field_place, &variant_def, variant_layout, generics)?
516-
}
517-
sym::non_exhaustive => {
518-
let is_non_exhaustive = variant_def.is_field_list_non_exhaustive();
519-
self.write_scalar(Scalar::from_bool(is_non_exhaustive), &field_place)?
520-
}
521-
other => span_bug!(self.tcx.def_span(field_def.did), "unimplemented field {other}"),
522-
}
523-
}
524-
interp_ok(())
525-
}
526-
527-
fn write_generics(
528-
&mut self,
529-
place: impl Writeable<'tcx, CtfeProvenance>,
530-
generics: &'tcx GenericArgs<'tcx>,
531-
) -> InterpResult<'tcx> {
532-
self.allocate_fill_and_write_slice_ptr(place, generics.len() as u64, |this, i, place| {
533-
match generics[i as usize].kind() {
534-
GenericArgKind::Lifetime(region) => this.write_generic_lifetime(region, place),
535-
GenericArgKind::Type(ty) => this.write_generic_type(ty, place),
536-
GenericArgKind::Const(c) => this.write_generic_const(c, place),
537-
}
538-
})
539-
}
540-
541-
fn write_generic_lifetime(
542-
&mut self,
543-
_region: Region<'tcx>,
544-
place: MPlaceTy<'tcx>,
545-
) -> InterpResult<'tcx> {
546-
let (variant_idx, _) = self.downcast(&place, sym::Lifetime)?;
547-
self.write_discriminant(variant_idx, &place)?;
548-
interp_ok(())
549-
}
550-
551-
fn write_generic_type(&mut self, ty: Ty<'tcx>, place: MPlaceTy<'tcx>) -> InterpResult<'tcx> {
552-
let (variant_idx, variant_place) = self.downcast(&place, sym::Type)?;
553-
let generic_type_place = self.project_field(&variant_place, FieldIdx::ZERO)?;
554-
555-
for (field_idx, field_def) in generic_type_place
556-
.layout()
557-
.ty
558-
.ty_adt_def()
559-
.unwrap()
560-
.non_enum_variant()
561-
.fields
562-
.iter_enumerated()
563-
{
564-
let field_place = self.project_field(&generic_type_place, field_idx)?;
565-
match field_def.name {
566-
sym::ty => self.write_type_id(ty, &field_place)?,
567-
other => span_bug!(self.tcx.def_span(field_def.did), "unimplemented field {other}"),
568-
}
569-
}
570-
571-
self.write_discriminant(variant_idx, &place)?;
572-
interp_ok(())
573-
}
574-
575-
fn write_generic_const(&mut self, c: Const<'tcx>, place: MPlaceTy<'tcx>) -> InterpResult<'tcx> {
576-
let ConstKind::Value(c) = c.kind() else { bug!("expected a computed const, got {c:?}") };
577-
578-
let (variant_idx, variant_place) = self.downcast(&place, sym::Const)?;
579-
let const_place = self.project_field(&variant_place, FieldIdx::ZERO)?;
580-
581-
for (field_idx, field_def) in const_place
582-
.layout()
583-
.ty
584-
.ty_adt_def()
585-
.unwrap()
586-
.non_enum_variant()
587-
.fields
588-
.iter_enumerated()
589-
{
590-
let field_place = self.project_field(&const_place, field_idx)?;
591-
match field_def.name {
592-
sym::ty => self.write_type_id(c.ty, &field_place)?,
593-
other => span_bug!(self.tcx.def_span(field_def.did), "unimplemented field {other}"),
594-
}
595-
}
596-
597-
self.write_discriminant(variant_idx, &place)?;
598-
interp_ok(())
599-
}
600-
601338
fn write_int_type_info(
602339
&mut self,
603340
place: impl Writeable<'tcx, CtfeProvenance>,

0 commit comments

Comments
 (0)