rustc_const_eval/const_eval/type_info/
adt.rs1use rustc_abi::{FieldIdx, VariantIdx};
2use rustc_middle::ty::{
3 AdtDef, AdtKind, Const, ConstKind, GenericArgKind, GenericArgs, Region, Ty,
4};
5use rustc_span::{bug, span_bug, sym};
6
7use crate::const_eval::CompileTimeMachine;
8use crate::interpret::{
9 CtfeProvenance, InterpCx, InterpResult, MPlaceTy, Projectable, Writeable, interp_ok,
10};
11
12impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
13 pub(crate) fn write_adt_type_info(
15 &mut self,
16 place: &impl Writeable<'tcx, CtfeProvenance>,
17 adt_def: AdtDef<'tcx>,
18 ) -> InterpResult<'tcx, VariantIdx> {
19 let variant_idx = match adt_def.adt_kind() {
20 AdtKind::Struct => {
21 let (variant, _) = self.project_downcast_named(place, sym::Struct)?;
22 variant
23 }
24 AdtKind::Union => {
25 let (variant, _) = self.project_downcast_named(place, sym::Union)?;
26 variant
27 }
28 AdtKind::Enum => {
29 let (variant, _) = self.project_downcast_named(place, sym::Enum)?;
30 variant
31 }
32 };
33 interp_ok(variant_idx)
34 }
35
36 pub(super) fn write_generics(
37 &mut self,
38 place: &impl Writeable<'tcx, CtfeProvenance>,
39 generics: GenericArgs<'tcx>,
40 ) -> InterpResult<'tcx> {
41 self.allocate_fill_and_write_slice_ptr(place, generics.len() as u64, |this, i, place| {
42 match generics[i as usize].kind() {
43 GenericArgKind::Lifetime(region) => this.write_generic_lifetime(region, place),
44 GenericArgKind::Type(ty) => this.write_generic_type(ty, place),
45 GenericArgKind::Const(c) => this.write_generic_const(c, place),
46 }
47 })
48 }
49
50 fn write_generic_lifetime(
51 &mut self,
52 _region: Region<'tcx>,
53 place: MPlaceTy<'tcx>,
54 ) -> InterpResult<'tcx> {
55 let (variant_idx, _) = self.project_downcast_named(&place, sym::Lifetime)?;
56 self.write_discriminant(variant_idx, &place)?;
57 interp_ok(())
58 }
59
60 fn write_generic_type(&mut self, ty: Ty<'tcx>, place: MPlaceTy<'tcx>) -> InterpResult<'tcx> {
61 let (variant_idx, variant_place) = self.project_downcast_named(&place, sym::Type)?;
62 let generic_type_place = self.project_field(&variant_place, FieldIdx::ZERO)?;
63
64 for (field_idx, field_def) in generic_type_place
65 .layout()
66 .ty
67 .ty_adt_def()
68 .unwrap()
69 .non_enum_variant()
70 .fields
71 .iter_enumerated()
72 {
73 let field_place = self.project_field(&generic_type_place, field_idx)?;
74 match field_def.name {
75 sym::ty => self.write_type_id(ty, &field_place)?,
76 other => ::rustc_span::macros::bug_impl(Some(self.tcx.def_span(field_def.did)),
format_args!("unimplemented field {0}", other), Location::caller())span_bug!(self.tcx.def_span(field_def.did), "unimplemented field {other}"),
77 }
78 }
79
80 self.write_discriminant(variant_idx, &place)?;
81 interp_ok(())
82 }
83
84 fn write_generic_const(&mut self, c: Const<'tcx>, place: MPlaceTy<'tcx>) -> InterpResult<'tcx> {
85 let ConstKind::Value(c) = c.kind() else { ::rustc_span::macros::bug_impl(None,
format_args!("expected a computed const, got {0:?}", c),
Location::caller())bug!("expected a computed const, got {c:?}") };
86
87 let (variant_idx, variant_place) = self.project_downcast_named(&place, sym::Const)?;
88 let const_place = self.project_field(&variant_place, FieldIdx::ZERO)?;
89
90 for (field_idx, field_def) in const_place
91 .layout()
92 .ty
93 .ty_adt_def()
94 .unwrap()
95 .non_enum_variant()
96 .fields
97 .iter_enumerated()
98 {
99 let field_place = self.project_field(&const_place, field_idx)?;
100 match field_def.name {
101 sym::ty => self.write_type_id(c.ty, &field_place)?,
102 other => ::rustc_span::macros::bug_impl(Some(self.tcx.def_span(field_def.did)),
format_args!("unimplemented field {0}", other), Location::caller())span_bug!(self.tcx.def_span(field_def.did), "unimplemented field {other}"),
103 }
104 }
105
106 self.write_discriminant(variant_idx, &place)?;
107 interp_ok(())
108 }
109}