]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_const_eval/src/interpret/cast.rs
b2c847d3fd8dd6fca2403ecd5d9216ae4c5e63f4
[rust.git] / compiler / rustc_const_eval / src / interpret / cast.rs
1 use std::assert_matches::assert_matches;
2
3 use rustc_apfloat::ieee::{Double, Single};
4 use rustc_apfloat::{Float, FloatConvert};
5 use rustc_middle::mir::interpret::{InterpResult, PointerArithmetic, Scalar};
6 use rustc_middle::mir::CastKind;
7 use rustc_middle::ty::adjustment::PointerCast;
8 use rustc_middle::ty::layout::{IntegerExt, LayoutOf, TyAndLayout};
9 use rustc_middle::ty::{self, FloatTy, Ty, TypeAndMut};
10 use rustc_target::abi::Integer;
11 use rustc_type_ir::sty::TyKind::*;
12
13 use super::{
14     util::ensure_monomorphic_enough, FnVal, ImmTy, Immediate, InterpCx, Machine, OpTy, PlaceTy,
15 };
16
17 impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
18     pub fn cast(
19         &mut self,
20         src: &OpTy<'tcx, M::Provenance>,
21         cast_kind: CastKind,
22         cast_ty: Ty<'tcx>,
23         dest: &PlaceTy<'tcx, M::Provenance>,
24     ) -> InterpResult<'tcx> {
25         use rustc_middle::mir::CastKind::*;
26         // FIXME: In which cases should we trigger UB when the source is uninit?
27         match cast_kind {
28             Pointer(PointerCast::Unsize) => {
29                 let cast_ty = self.layout_of(cast_ty)?;
30                 self.unsize_into(src, cast_ty, dest)?;
31             }
32
33             PointerExposeAddress => {
34                 let src = self.read_immediate(src)?;
35                 let res = self.pointer_expose_address_cast(&src, cast_ty)?;
36                 self.write_immediate(res, dest)?;
37             }
38
39             PointerFromExposedAddress => {
40                 let src = self.read_immediate(src)?;
41                 let res = self.pointer_from_exposed_address_cast(&src, cast_ty)?;
42                 self.write_immediate(res, dest)?;
43             }
44
45             IntToInt | IntToFloat => {
46                 let src = self.read_immediate(src)?;
47                 let res = self.int_to_int_or_float(&src, cast_ty)?;
48                 self.write_immediate(res, dest)?;
49             }
50
51             FloatToFloat | FloatToInt => {
52                 let src = self.read_immediate(src)?;
53                 let res = self.float_to_float_or_int(&src, cast_ty)?;
54                 self.write_immediate(res, dest)?;
55             }
56
57             FnPtrToPtr | PtrToPtr => {
58                 let src = self.read_immediate(&src)?;
59                 let res = self.ptr_to_ptr(&src, cast_ty)?;
60                 self.write_immediate(res, dest)?;
61             }
62
63             Pointer(PointerCast::MutToConstPointer | PointerCast::ArrayToPointer) => {
64                 // These are NOPs, but can be wide pointers.
65                 let v = self.read_immediate(src)?;
66                 self.write_immediate(*v, dest)?;
67             }
68
69             Pointer(PointerCast::ReifyFnPointer) => {
70                 // The src operand does not matter, just its type
71                 match *src.layout.ty.kind() {
72                     ty::FnDef(def_id, substs) => {
73                         // All reifications must be monomorphic, bail out otherwise.
74                         ensure_monomorphic_enough(*self.tcx, src.layout.ty)?;
75
76                         let instance = ty::Instance::resolve_for_fn_ptr(
77                             *self.tcx,
78                             self.param_env,
79                             def_id,
80                             substs,
81                         )
82                         .ok_or_else(|| err_inval!(TooGeneric))?;
83
84                         let fn_ptr = self.create_fn_alloc_ptr(FnVal::Instance(instance));
85                         self.write_pointer(fn_ptr, dest)?;
86                     }
87                     _ => span_bug!(self.cur_span(), "reify fn pointer on {:?}", src.layout.ty),
88                 }
89             }
90
91             Pointer(PointerCast::UnsafeFnPointer) => {
92                 let src = self.read_immediate(src)?;
93                 match cast_ty.kind() {
94                     ty::FnPtr(_) => {
95                         // No change to value
96                         self.write_immediate(*src, dest)?;
97                     }
98                     _ => span_bug!(self.cur_span(), "fn to unsafe fn cast on {:?}", cast_ty),
99                 }
100             }
101
102             Pointer(PointerCast::ClosureFnPointer(_)) => {
103                 // The src operand does not matter, just its type
104                 match *src.layout.ty.kind() {
105                     ty::Closure(def_id, substs) => {
106                         // All reifications must be monomorphic, bail out otherwise.
107                         ensure_monomorphic_enough(*self.tcx, src.layout.ty)?;
108
109                         let instance = ty::Instance::resolve_closure(
110                             *self.tcx,
111                             def_id,
112                             substs,
113                             ty::ClosureKind::FnOnce,
114                         )
115                         .ok_or_else(|| err_inval!(TooGeneric))?;
116                         let fn_ptr = self.create_fn_alloc_ptr(FnVal::Instance(instance));
117                         self.write_pointer(fn_ptr, dest)?;
118                     }
119                     _ => span_bug!(self.cur_span(), "closure fn pointer on {:?}", src.layout.ty),
120                 }
121             }
122
123             DynStar => {
124                 if let ty::Dynamic(data, _, ty::DynStar) = cast_ty.kind() {
125                     // Initial cast from sized to dyn trait
126                     let vtable = self.get_vtable_ptr(src.layout.ty, data.principal())?;
127                     let vtable = Scalar::from_maybe_pointer(vtable, self);
128                     let data = self.read_immediate(src)?.to_scalar();
129                     let _assert_pointer_sized = data.to_pointer(self)?;
130                     let val = Immediate::ScalarPair(data, vtable);
131                     self.write_immediate(val, dest)?;
132                 } else {
133                     bug!()
134                 }
135             }
136         }
137         Ok(())
138     }
139
140     /// Handles 'IntToInt' and 'IntToFloat' casts.
141     pub fn int_to_int_or_float(
142         &self,
143         src: &ImmTy<'tcx, M::Provenance>,
144         cast_ty: Ty<'tcx>,
145     ) -> InterpResult<'tcx, Immediate<M::Provenance>> {
146         assert!(src.layout.ty.is_integral() || src.layout.ty.is_char() || src.layout.ty.is_bool());
147         assert!(cast_ty.is_floating_point() || cast_ty.is_integral() || cast_ty.is_char());
148
149         Ok(self.cast_from_int_like(src.to_scalar(), src.layout, cast_ty)?.into())
150     }
151
152     /// Handles 'FloatToFloat' and 'FloatToInt' casts.
153     pub fn float_to_float_or_int(
154         &self,
155         src: &ImmTy<'tcx, M::Provenance>,
156         cast_ty: Ty<'tcx>,
157     ) -> InterpResult<'tcx, Immediate<M::Provenance>> {
158         use rustc_type_ir::sty::TyKind::*;
159
160         match src.layout.ty.kind() {
161             // Floating point
162             Float(FloatTy::F32) => {
163                 return Ok(self.cast_from_float(src.to_scalar().to_f32()?, cast_ty).into());
164             }
165             Float(FloatTy::F64) => {
166                 return Ok(self.cast_from_float(src.to_scalar().to_f64()?, cast_ty).into());
167             }
168             _ => {
169                 bug!("Can't cast 'Float' type into {:?}", cast_ty);
170             }
171         }
172     }
173
174     /// Handles 'FnPtrToPtr' and 'PtrToPtr' casts.
175     pub fn ptr_to_ptr(
176         &self,
177         src: &ImmTy<'tcx, M::Provenance>,
178         cast_ty: Ty<'tcx>,
179     ) -> InterpResult<'tcx, Immediate<M::Provenance>> {
180         assert!(src.layout.ty.is_any_ptr());
181         assert!(cast_ty.is_unsafe_ptr());
182         // Handle casting any ptr to raw ptr (might be a fat ptr).
183         let dest_layout = self.layout_of(cast_ty)?;
184         if dest_layout.size == src.layout.size {
185             // Thin or fat pointer that just hast the ptr kind of target type changed.
186             return Ok(**src);
187         } else {
188             // Casting the metadata away from a fat ptr.
189             assert_eq!(src.layout.size, 2 * self.pointer_size());
190             assert_eq!(dest_layout.size, self.pointer_size());
191             assert!(src.layout.ty.is_unsafe_ptr());
192             return match **src {
193                 Immediate::ScalarPair(data, _) => Ok(data.into()),
194                 Immediate::Scalar(..) => span_bug!(
195                     self.cur_span(),
196                     "{:?} input to a fat-to-thin cast ({:?} -> {:?})",
197                     *src,
198                     src.layout.ty,
199                     cast_ty
200                 ),
201                 Immediate::Uninit => throw_ub!(InvalidUninitBytes(None)),
202             };
203         }
204     }
205
206     pub fn pointer_expose_address_cast(
207         &mut self,
208         src: &ImmTy<'tcx, M::Provenance>,
209         cast_ty: Ty<'tcx>,
210     ) -> InterpResult<'tcx, Immediate<M::Provenance>> {
211         assert_matches!(src.layout.ty.kind(), ty::RawPtr(_) | ty::FnPtr(_));
212         assert!(cast_ty.is_integral());
213
214         let scalar = src.to_scalar();
215         let ptr = scalar.to_pointer(self)?;
216         match ptr.into_pointer_or_addr() {
217             Ok(ptr) => M::expose_ptr(self, ptr)?,
218             Err(_) => {} // Do nothing, exposing an invalid pointer (`None` provenance) is a NOP.
219         };
220         Ok(self.cast_from_int_like(scalar, src.layout, cast_ty)?.into())
221     }
222
223     pub fn pointer_from_exposed_address_cast(
224         &self,
225         src: &ImmTy<'tcx, M::Provenance>,
226         cast_ty: Ty<'tcx>,
227     ) -> InterpResult<'tcx, Immediate<M::Provenance>> {
228         assert!(src.layout.ty.is_integral());
229         assert_matches!(cast_ty.kind(), ty::RawPtr(_));
230
231         // First cast to usize.
232         let scalar = src.to_scalar();
233         let addr = self.cast_from_int_like(scalar, src.layout, self.tcx.types.usize)?;
234         let addr = addr.to_machine_usize(self)?;
235
236         // Then turn address into pointer.
237         let ptr = M::ptr_from_addr_cast(&self, addr)?;
238         Ok(Scalar::from_maybe_pointer(ptr, self).into())
239     }
240
241     /// Low-level cast helper function. This works directly on scalars and can take 'int-like' input
242     /// type (basically everything with a scalar layout) to int/float/char types.
243     pub fn cast_from_int_like(
244         &self,
245         scalar: Scalar<M::Provenance>, // input value (there is no ScalarTy so we separate data+layout)
246         src_layout: TyAndLayout<'tcx>,
247         cast_ty: Ty<'tcx>,
248     ) -> InterpResult<'tcx, Scalar<M::Provenance>> {
249         // Let's make sure v is sign-extended *if* it has a signed type.
250         let signed = src_layout.abi.is_signed(); // Also asserts that abi is `Scalar`.
251
252         let v = scalar.to_bits(src_layout.size)?;
253         let v = if signed { self.sign_extend(v, src_layout) } else { v };
254         trace!("cast_from_scalar: {}, {} -> {}", v, src_layout.ty, cast_ty);
255
256         Ok(match *cast_ty.kind() {
257             Int(_) | Uint(_) => {
258                 let size = match *cast_ty.kind() {
259                     Int(t) => Integer::from_int_ty(self, t).size(),
260                     Uint(t) => Integer::from_uint_ty(self, t).size(),
261                     _ => bug!(),
262                 };
263                 let v = size.truncate(v);
264                 Scalar::from_uint(v, size)
265             }
266
267             Float(FloatTy::F32) if signed => Scalar::from_f32(Single::from_i128(v as i128).value),
268             Float(FloatTy::F64) if signed => Scalar::from_f64(Double::from_i128(v as i128).value),
269             Float(FloatTy::F32) => Scalar::from_f32(Single::from_u128(v).value),
270             Float(FloatTy::F64) => Scalar::from_f64(Double::from_u128(v).value),
271
272             Char => {
273                 // `u8` to `char` cast
274                 Scalar::from_u32(u8::try_from(v).unwrap().into())
275             }
276
277             // Casts to bool are not permitted by rustc, no need to handle them here.
278             _ => span_bug!(self.cur_span(), "invalid int to {:?} cast", cast_ty),
279         })
280     }
281
282     /// Low-level cast helper function. Converts an apfloat `f` into int or float types.
283     fn cast_from_float<F>(&self, f: F, dest_ty: Ty<'tcx>) -> Scalar<M::Provenance>
284     where
285         F: Float + Into<Scalar<M::Provenance>> + FloatConvert<Single> + FloatConvert<Double>,
286     {
287         use rustc_type_ir::sty::TyKind::*;
288         match *dest_ty.kind() {
289             // float -> uint
290             Uint(t) => {
291                 let size = Integer::from_uint_ty(self, t).size();
292                 // `to_u128` is a saturating cast, which is what we need
293                 // (https://doc.rust-lang.org/nightly/nightly-rustc/rustc_apfloat/trait.Float.html#method.to_i128_r).
294                 let v = f.to_u128(size.bits_usize()).value;
295                 // This should already fit the bit width
296                 Scalar::from_uint(v, size)
297             }
298             // float -> int
299             Int(t) => {
300                 let size = Integer::from_int_ty(self, t).size();
301                 // `to_i128` is a saturating cast, which is what we need
302                 // (https://doc.rust-lang.org/nightly/nightly-rustc/rustc_apfloat/trait.Float.html#method.to_i128_r).
303                 let v = f.to_i128(size.bits_usize()).value;
304                 Scalar::from_int(v, size)
305             }
306             // float -> f32
307             Float(FloatTy::F32) => Scalar::from_f32(f.convert(&mut false).value),
308             // float -> f64
309             Float(FloatTy::F64) => Scalar::from_f64(f.convert(&mut false).value),
310             // That's it.
311             _ => span_bug!(self.cur_span(), "invalid float to {:?} cast", dest_ty),
312         }
313     }
314
315     fn unsize_into_ptr(
316         &mut self,
317         src: &OpTy<'tcx, M::Provenance>,
318         dest: &PlaceTy<'tcx, M::Provenance>,
319         // The pointee types
320         source_ty: Ty<'tcx>,
321         cast_ty: Ty<'tcx>,
322     ) -> InterpResult<'tcx> {
323         // A<Struct> -> A<Trait> conversion
324         let (src_pointee_ty, dest_pointee_ty) =
325             self.tcx.struct_lockstep_tails_erasing_lifetimes(source_ty, cast_ty, self.param_env);
326
327         match (&src_pointee_ty.kind(), &dest_pointee_ty.kind()) {
328             (&ty::Array(_, length), &ty::Slice(_)) => {
329                 let ptr = self.read_scalar(src)?;
330                 // u64 cast is from usize to u64, which is always good
331                 let val =
332                     Immediate::new_slice(ptr, length.eval_usize(*self.tcx, self.param_env), self);
333                 self.write_immediate(val, dest)
334             }
335             (ty::Dynamic(data_a, ..), ty::Dynamic(data_b, ..)) => {
336                 let val = self.read_immediate(src)?;
337                 if data_a.principal() == data_b.principal() {
338                     // A NOP cast that doesn't actually change anything, should be allowed even with mismatching vtables.
339                     return self.write_immediate(*val, dest);
340                 }
341                 let (old_data, old_vptr) = val.to_scalar_pair();
342                 let old_vptr = old_vptr.to_pointer(self)?;
343                 let (ty, old_trait) = self.get_ptr_vtable(old_vptr)?;
344                 if old_trait != data_a.principal() {
345                     throw_ub_format!("upcast on a pointer whose vtable does not match its type");
346                 }
347                 let new_vptr = self.get_vtable_ptr(ty, data_b.principal())?;
348                 self.write_immediate(Immediate::new_dyn_trait(old_data, new_vptr, self), dest)
349             }
350             (_, &ty::Dynamic(data, _, ty::Dyn)) => {
351                 // Initial cast from sized to dyn trait
352                 let vtable = self.get_vtable_ptr(src_pointee_ty, data.principal())?;
353                 let ptr = self.read_scalar(src)?;
354                 let val = Immediate::new_dyn_trait(ptr, vtable, &*self.tcx);
355                 self.write_immediate(val, dest)
356             }
357
358             _ => {
359                 span_bug!(self.cur_span(), "invalid unsizing {:?} -> {:?}", src.layout.ty, cast_ty)
360             }
361         }
362     }
363
364     fn unsize_into(
365         &mut self,
366         src: &OpTy<'tcx, M::Provenance>,
367         cast_ty: TyAndLayout<'tcx>,
368         dest: &PlaceTy<'tcx, M::Provenance>,
369     ) -> InterpResult<'tcx> {
370         trace!("Unsizing {:?} of type {} into {:?}", *src, src.layout.ty, cast_ty.ty);
371         match (&src.layout.ty.kind(), &cast_ty.ty.kind()) {
372             (&ty::Ref(_, s, _), &ty::Ref(_, c, _) | &ty::RawPtr(TypeAndMut { ty: c, .. }))
373             | (&ty::RawPtr(TypeAndMut { ty: s, .. }), &ty::RawPtr(TypeAndMut { ty: c, .. })) => {
374                 self.unsize_into_ptr(src, dest, *s, *c)
375             }
376             (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
377                 assert_eq!(def_a, def_b);
378
379                 // unsizing of generic struct with pointer fields
380                 // Example: `Arc<T>` -> `Arc<Trait>`
381                 // here we need to increase the size of every &T thin ptr field to a fat ptr
382                 for i in 0..src.layout.fields.count() {
383                     let cast_ty_field = cast_ty.field(self, i);
384                     if cast_ty_field.is_zst() {
385                         continue;
386                     }
387                     let src_field = self.operand_field(src, i)?;
388                     let dst_field = self.place_field(dest, i)?;
389                     if src_field.layout.ty == cast_ty_field.ty {
390                         self.copy_op(&src_field, &dst_field, /*allow_transmute*/ false)?;
391                     } else {
392                         self.unsize_into(&src_field, cast_ty_field, &dst_field)?;
393                     }
394                 }
395                 Ok(())
396             }
397             _ => span_bug!(
398                 self.cur_span(),
399                 "unsize_into: invalid conversion: {:?} -> {:?}",
400                 src.layout,
401                 dest.layout
402             ),
403         }
404     }
405 }