22
33use std:: borrow:: Cow ;
44
5- use rustc_abi:: { ExternAbi , FieldIdx , VariantIdx } ;
5+ use rustc_abi:: { ExternAbi , FieldIdx } ;
66use rustc_ast:: Mutability ;
77use rustc_hir:: LangItem ;
88use rustc_middle:: span_bug;
@@ -12,27 +12,11 @@ use rustc_span::{Symbol, sym};
1212
1313use crate :: const_eval:: CompileTimeMachine ;
1414use crate :: interpret:: {
15- CtfeProvenance , Immediate , InterpCx , InterpResult , MPlaceTy , MemoryKind , Projectable , Scalar ,
16- Writeable , interp_ok,
15+ CtfeProvenance , Immediate , InterpCx , InterpResult , MPlaceTy , MemoryKind , Scalar , Writeable ,
16+ interp_ok,
1717} ;
1818
1919impl < ' tcx > InterpCx < ' tcx , CompileTimeMachine < ' tcx > > {
20- /// Equivalent to `project_downcast`, but identifies the variant by name instead of index.
21- fn downcast < ' a > (
22- & self ,
23- place : & ( impl Writeable < ' tcx , CtfeProvenance > + ' a ) ,
24- name : Symbol ,
25- ) -> InterpResult < ' tcx , ( VariantIdx , impl Writeable < ' tcx , CtfeProvenance > + ' a ) > {
26- let variants = place. layout ( ) . ty . ty_adt_def ( ) . unwrap ( ) . variants ( ) ;
27- let variant_idx = variants
28- . iter_enumerated ( )
29- . find ( |( _idx, var) | var. name == name)
30- . unwrap_or_else ( || panic ! ( "got {name} but expected one of {variants:#?}" ) )
31- . 0 ;
32-
33- interp_ok ( ( variant_idx, self . project_downcast ( place, variant_idx) ?) )
34- }
35-
3620 // A general method to write an array to a static slice place.
3721 fn allocate_fill_and_write_slice_ptr (
3822 & mut self ,
@@ -83,7 +67,7 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
8367 let variant_index = match ty. kind ( ) {
8468 ty:: Tuple ( fields) => {
8569 let ( variant, variant_place) =
86- self . downcast ( & field_dest, sym:: Tuple ) ?;
70+ self . project_downcast_sym ( & field_dest, sym:: Tuple ) ?;
8771 // project to the single tuple variant field of `type_info::Tuple` struct type
8872 let tuple_place = self . project_field ( & variant_place, FieldIdx :: ZERO ) ?;
8973 assert_eq ! (
@@ -102,7 +86,7 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
10286 }
10387 ty:: Array ( ty, len) => {
10488 let ( variant, variant_place) =
105- self . downcast ( & field_dest, sym:: Array ) ?;
89+ self . project_downcast_sym ( & field_dest, sym:: Array ) ?;
10690 let array_place = self . project_field ( & variant_place, FieldIdx :: ZERO ) ?;
10791
10892 self . write_array_type_info ( array_place, * ty, * len) ?;
@@ -111,7 +95,7 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
11195 }
11296 ty:: Slice ( ty) => {
11397 let ( variant, variant_place) =
114- self . downcast ( & field_dest, sym:: Slice ) ?;
98+ self . project_downcast_sym ( & field_dest, sym:: Slice ) ?;
11599 let slice_place = self . project_field ( & variant_place, FieldIdx :: ZERO ) ?;
116100
117101 self . write_slice_type_info ( slice_place, * ty) ?;
@@ -123,16 +107,17 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
123107 }
124108 ty:: Bool => {
125109 let ( variant, _variant_place) =
126- self . downcast ( & field_dest, sym:: Bool ) ?;
110+ self . project_downcast_sym ( & field_dest, sym:: Bool ) ?;
127111 variant
128112 }
129113 ty:: Char => {
130114 let ( variant, _variant_place) =
131- self . downcast ( & field_dest, sym:: Char ) ?;
115+ self . project_downcast_sym ( & field_dest, sym:: Char ) ?;
132116 variant
133117 }
134118 ty:: Int ( int_ty) => {
135- let ( variant, variant_place) = self . downcast ( & field_dest, sym:: Int ) ?;
119+ let ( variant, variant_place) =
120+ self . project_downcast_sym ( & field_dest, sym:: Int ) ?;
136121 let place = self . project_field ( & variant_place, FieldIdx :: ZERO ) ?;
137122 self . write_int_type_info (
138123 place,
@@ -142,7 +127,8 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
142127 variant
143128 }
144129 ty:: Uint ( uint_ty) => {
145- let ( variant, variant_place) = self . downcast ( & field_dest, sym:: Int ) ?;
130+ let ( variant, variant_place) =
131+ self . project_downcast_sym ( & field_dest, sym:: Int ) ?;
146132 let place = self . project_field ( & variant_place, FieldIdx :: ZERO ) ?;
147133 self . write_int_type_info (
148134 place,
@@ -153,18 +139,19 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
153139 }
154140 ty:: Float ( float_ty) => {
155141 let ( variant, variant_place) =
156- self . downcast ( & field_dest, sym:: Float ) ?;
142+ self . project_downcast_sym ( & field_dest, sym:: Float ) ?;
157143 let place = self . project_field ( & variant_place, FieldIdx :: ZERO ) ?;
158144 self . write_float_type_info ( place, float_ty. bit_width ( ) ) ?;
159145 variant
160146 }
161147 ty:: Str => {
162- let ( variant, _variant_place) = self . downcast ( & field_dest, sym:: Str ) ?;
148+ let ( variant, _variant_place) =
149+ self . project_downcast_sym ( & field_dest, sym:: Str ) ?;
163150 variant
164151 }
165152 ty:: Ref ( _, ty, mutability) => {
166153 let ( variant, variant_place) =
167- self . downcast ( & field_dest, sym:: Reference ) ?;
154+ self . project_downcast_sym ( & field_dest, sym:: Reference ) ?;
168155 let reference_place =
169156 self . project_field ( & variant_place, FieldIdx :: ZERO ) ?;
170157 self . write_reference_type_info ( reference_place, * ty, * mutability) ?;
@@ -173,7 +160,7 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
173160 }
174161 ty:: RawPtr ( ty, mutability) => {
175162 let ( variant, variant_place) =
176- self . downcast ( & field_dest, sym:: Pointer ) ?;
163+ self . project_downcast_sym ( & field_dest, sym:: Pointer ) ?;
177164 let pointer_place =
178165 self . project_field ( & variant_place, FieldIdx :: ZERO ) ?;
179166
@@ -183,14 +170,14 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
183170 }
184171 ty:: Dynamic ( predicates, region) => {
185172 let ( variant, variant_place) =
186- self . downcast ( & field_dest, sym:: DynTrait ) ?;
173+ self . project_downcast_sym ( & field_dest, sym:: DynTrait ) ?;
187174 let dyn_place = self . project_field ( & variant_place, FieldIdx :: ZERO ) ?;
188175 self . write_dyn_trait_type_info ( dyn_place, * predicates, * region) ?;
189176 variant
190177 }
191178 ty:: FnPtr ( sig, fn_header) => {
192179 let ( variant, variant_place) =
193- self . downcast ( & field_dest, sym:: FnPtr ) ?;
180+ self . project_downcast_sym ( & field_dest, sym:: FnPtr ) ?;
194181 let fn_ptr_place =
195182 self . project_field ( & variant_place, FieldIdx :: ZERO ) ?;
196183
@@ -214,27 +201,10 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
214201 | ty:: Bound ( ..)
215202 | ty:: Placeholder ( _)
216203 | ty:: Infer ( ..)
217- | ty:: Error ( _) => self . downcast ( & field_dest, sym:: Other ) ?. 0 ,
204+ | ty:: Error ( _) => self . project_downcast_sym ( & field_dest, sym:: Other ) ?. 0 ,
218205 } ;
219206 self . write_discriminant ( variant_index, & field_dest) ?
220207 }
221- sym:: size => {
222- let layout = self . layout_of ( ty) ?;
223- let variant_index = if layout. is_sized ( ) {
224- let ( variant, variant_place) = self . downcast ( & field_dest, sym:: Some ) ?;
225- let size_field_place =
226- self . project_field ( & variant_place, FieldIdx :: ZERO ) ?;
227- self . write_scalar (
228- ScalarInt :: try_from_target_usize ( layout. size . bytes ( ) , self . tcx . tcx )
229- . unwrap ( ) ,
230- & size_field_place,
231- ) ?;
232- variant
233- } else {
234- self . downcast ( & field_dest, sym:: None ) ?. 0
235- } ;
236- self . write_discriminant ( variant_index, & field_dest) ?;
237- }
238208 other => span_bug ! ( self . tcx. span, "unknown `Type` field {other}" ) ,
239209 }
240210 }
@@ -433,16 +403,17 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
433403 sym:: abi => match fn_sig_kind. abi ( ) {
434404 ExternAbi :: C { .. } => {
435405 let ( rust_variant, _rust_place) =
436- self . downcast ( & field_place, sym:: ExternC ) ?;
406+ self . project_downcast_sym ( & field_place, sym:: ExternC ) ?;
437407 self . write_discriminant ( rust_variant, & field_place) ?;
438408 }
439409 ExternAbi :: Rust => {
440410 let ( rust_variant, _rust_place) =
441- self . downcast ( & field_place, sym:: ExternRust ) ?;
411+ self . project_downcast_sym ( & field_place, sym:: ExternRust ) ?;
442412 self . write_discriminant ( rust_variant, & field_place) ?;
443413 }
444414 other_abi => {
445- let ( variant, variant_place) = self . downcast ( & field_place, sym:: Named ) ?;
415+ let ( variant, variant_place) =
416+ self . project_downcast_sym ( & field_place, sym:: Named ) ?;
446417 let str_place = self . allocate_str_dedup ( other_abi. as_str ( ) ) ?;
447418 let str_ref = self . mplace_to_imm_ptr ( & str_place, None ) ?;
448419 let payload = self . project_field ( & variant_place, FieldIdx :: ZERO ) ?;
0 commit comments