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