rustc_middle/mir/interpret/
value.rs1use std::fmt;
2
3use either::{Either, Left, Right};
4use rustc_abi::{HasDataLayout, Size};
5use rustc_apfloat::Float;
6use rustc_apfloat::ieee::{Double, Half, Quad, Single};
7use rustc_macros::{HashStable, TyDecodable, TyEncodable};
8
9use super::{
10 AllocId, CtfeProvenance, InterpResult, Pointer, PointerArithmetic, Provenance,
11 ScalarSizeMismatch, interp_ok,
12};
13use crate::ty::ScalarInt;
14
15#[derive(#[automatically_derived]
impl<Prov: ::core::clone::Clone> ::core::clone::Clone for Scalar<Prov> {
#[inline]
fn clone(&self) -> Scalar<Prov> {
match self {
Scalar::Int(__self_0) =>
Scalar::Int(::core::clone::Clone::clone(__self_0)),
Scalar::Ptr(__self_0, __self_1) =>
Scalar::Ptr(::core::clone::Clone::clone(__self_0),
::core::clone::Clone::clone(__self_1)),
}
}
}Clone, #[automatically_derived]
impl<Prov: ::core::marker::Copy> ::core::marker::Copy for Scalar<Prov> { }Copy, #[automatically_derived]
impl<Prov: ::core::cmp::Eq> ::core::cmp::Eq for Scalar<Prov> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) -> () {
let _: ::core::cmp::AssertParamIsEq<ScalarInt>;
let _: ::core::cmp::AssertParamIsEq<Pointer<Prov>>;
let _: ::core::cmp::AssertParamIsEq<u8>;
}
}Eq, #[automatically_derived]
impl<Prov: ::core::cmp::PartialEq> ::core::cmp::PartialEq for Scalar<Prov> {
#[inline]
fn eq(&self, other: &Scalar<Prov>) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(Scalar::Int(__self_0), Scalar::Int(__arg1_0)) =>
__self_0 == __arg1_0,
(Scalar::Ptr(__self_0, __self_1),
Scalar::Ptr(__arg1_0, __arg1_1)) =>
__self_1 == __arg1_1 && __self_0 == __arg1_0,
_ => unsafe { ::core::intrinsics::unreachable() }
}
}
}PartialEq, const _: () =
{
impl<'tcx, Prov, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for Scalar<Prov> where
Pointer<Prov>: ::rustc_serialize::Encodable<__E> {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
Scalar::Int(ref __binding_0) => { 0usize }
Scalar::Ptr(ref __binding_0, ref __binding_1) => { 1usize }
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
Scalar::Int(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
Scalar::Ptr(ref __binding_0, ref __binding_1) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
}
}
}
};TyEncodable, const _: () =
{
impl<'tcx, Prov, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
::rustc_serialize::Decodable<__D> for Scalar<Prov> where
Pointer<Prov>: ::rustc_serialize::Decodable<__D> {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => {
Scalar::Int(::rustc_serialize::Decodable::decode(__decoder))
}
1usize => {
Scalar::Ptr(::rustc_serialize::Decodable::decode(__decoder),
::rustc_serialize::Decodable::decode(__decoder))
}
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `Scalar`, expected 0..2, actual {0}",
n));
}
}
}
}
};TyDecodable, #[automatically_derived]
impl<Prov: ::core::hash::Hash> ::core::hash::Hash for Scalar<Prov> {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state);
match self {
Scalar::Int(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
Scalar::Ptr(__self_0, __self_1) => {
::core::hash::Hash::hash(__self_0, state);
::core::hash::Hash::hash(__self_1, state)
}
}
}
}Hash)]
23#[derive(const _: () =
{
impl<'__ctx, Prov>
::rustc_data_structures::stable_hasher::HashStable<::rustc_query_system::ich::StableHashingContext<'__ctx>>
for Scalar<Prov> where
Prov: ::rustc_data_structures::stable_hasher::HashStable<::rustc_query_system::ich::StableHashingContext<'__ctx>>
{
#[inline]
fn hash_stable(&self,
__hcx:
&mut ::rustc_query_system::ich::StableHashingContext<'__ctx>,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
match *self {
Scalar::Int(ref __binding_0) => {
{ __binding_0.hash_stable(__hcx, __hasher); }
}
Scalar::Ptr(ref __binding_0, ref __binding_1) => {
{ __binding_0.hash_stable(__hcx, __hasher); }
{ __binding_1.hash_stable(__hcx, __hasher); }
}
}
}
}
};HashStable)]
24pub enum Scalar<Prov = CtfeProvenance> {
25 Int(ScalarInt),
27
28 Ptr(Pointer<Prov>, u8),
34}
35
36#[cfg(target_pointer_width = "64")]
37const _: [(); 24] = [(); ::std::mem::size_of::<Scalar>()];rustc_data_structures::static_assert_size!(Scalar, 24);
38
39impl<Prov: Provenance> fmt::Debug for Scalar<Prov> {
42 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43 match self {
44 Scalar::Ptr(ptr, _size) => f.write_fmt(format_args!("{0:?}", ptr))write!(f, "{ptr:?}"),
45 Scalar::Int(int) => f.write_fmt(format_args!("{0:?}", int))write!(f, "{int:?}"),
46 }
47 }
48}
49
50impl<Prov: Provenance> fmt::Display for Scalar<Prov> {
51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52 match self {
53 Scalar::Ptr(ptr, _size) => f.write_fmt(format_args!("pointer to {0:?}", ptr))write!(f, "pointer to {ptr:?}"),
54 Scalar::Int(int) => f.write_fmt(format_args!("{0}", int))write!(f, "{int}"),
55 }
56 }
57}
58
59impl<Prov: Provenance> fmt::LowerHex for Scalar<Prov> {
60 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61 match self {
62 Scalar::Ptr(ptr, _size) => f.write_fmt(format_args!("pointer to {0:?}", ptr))write!(f, "pointer to {ptr:?}"),
63 Scalar::Int(int) => f.write_fmt(format_args!("{0:#x}", int))write!(f, "{int:#x}"),
64 }
65 }
66}
67
68impl<Prov> From<Half> for Scalar<Prov> {
69 #[inline(always)]
70 fn from(f: Half) -> Self {
71 Scalar::from_f16(f)
72 }
73}
74
75impl<Prov> From<Single> for Scalar<Prov> {
76 #[inline(always)]
77 fn from(f: Single) -> Self {
78 Scalar::from_f32(f)
79 }
80}
81
82impl<Prov> From<Double> for Scalar<Prov> {
83 #[inline(always)]
84 fn from(f: Double) -> Self {
85 Scalar::from_f64(f)
86 }
87}
88
89impl<Prov> From<Quad> for Scalar<Prov> {
90 #[inline(always)]
91 fn from(f: Quad) -> Self {
92 Scalar::from_f128(f)
93 }
94}
95
96impl<Prov> From<ScalarInt> for Scalar<Prov> {
97 #[inline(always)]
98 fn from(ptr: ScalarInt) -> Self {
99 Scalar::Int(ptr)
100 }
101}
102
103impl<Prov> Scalar<Prov> {
104 #[inline(always)]
105 pub fn from_pointer(ptr: Pointer<Prov>, cx: &impl HasDataLayout) -> Self {
106 Scalar::Ptr(ptr, u8::try_from(cx.pointer_size().bytes()).unwrap())
107 }
108
109 pub fn from_maybe_pointer(ptr: Pointer<Option<Prov>>, cx: &impl HasDataLayout) -> Self {
112 match ptr.into_raw_parts() {
113 (Some(prov), offset) => Scalar::from_pointer(Pointer::new(prov, offset), cx),
114 (None, offset) => {
115 Scalar::Int(ScalarInt::try_from_uint(offset.bytes(), cx.pointer_size()).unwrap())
116 }
117 }
118 }
119
120 #[inline]
121 pub fn null_ptr(cx: &impl HasDataLayout) -> Self {
122 Scalar::Int(ScalarInt::null(cx.pointer_size()))
123 }
124
125 #[inline]
126 pub fn from_bool(b: bool) -> Self {
127 Scalar::Int(b.into())
128 }
129
130 #[inline]
131 pub fn from_char(c: char) -> Self {
132 Scalar::Int(c.into())
133 }
134
135 #[inline]
136 pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
137 let i = i.into();
138 ScalarInt::try_from_uint(i, size)
139 .unwrap_or_else(|| crate::util::bug::bug_fmt(format_args!("Unsigned value {0:#x} does not fit in {1} bits",
i, size.bits()))bug!("Unsigned value {:#x} does not fit in {} bits", i, size.bits()))
140 .into()
141 }
142
143 #[inline]
144 pub fn from_u8(i: u8) -> Self {
145 Scalar::Int(i.into())
146 }
147
148 #[inline]
149 pub fn from_u16(i: u16) -> Self {
150 Scalar::Int(i.into())
151 }
152
153 #[inline]
154 pub fn from_u32(i: u32) -> Self {
155 Scalar::Int(i.into())
156 }
157
158 #[inline]
159 pub fn from_u64(i: u64) -> Self {
160 Scalar::Int(i.into())
161 }
162
163 #[inline]
164 pub fn from_u128(i: u128) -> Self {
165 Scalar::Int(i.into())
166 }
167
168 #[inline]
169 pub fn from_target_usize(i: u64, cx: &impl HasDataLayout) -> Self {
170 Self::from_uint(i, cx.data_layout().pointer_offset())
171 }
172
173 #[inline]
174 pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
175 let i = i.into();
176 ScalarInt::try_from_int(i, size)
177 .unwrap_or_else(|| crate::util::bug::bug_fmt(format_args!("Signed value {0:#x} does not fit in {1} bits",
i, size.bits()))bug!("Signed value {:#x} does not fit in {} bits", i, size.bits()))
178 .into()
179 }
180
181 #[inline]
182 pub fn from_i8(i: i8) -> Self {
183 Self::Int(i.into())
184 }
185
186 #[inline]
187 pub fn from_i16(i: i16) -> Self {
188 Self::Int(i.into())
189 }
190
191 #[inline]
192 pub fn from_i32(i: i32) -> Self {
193 Self::Int(i.into())
194 }
195
196 #[inline]
197 pub fn from_i64(i: i64) -> Self {
198 Self::Int(i.into())
199 }
200
201 #[inline]
202 pub fn from_i128(i: i128) -> Self {
203 Self::Int(i.into())
204 }
205
206 #[inline]
207 pub fn from_target_isize(i: i64, cx: &impl HasDataLayout) -> Self {
208 Self::from_int(i, cx.data_layout().pointer_offset())
209 }
210
211 #[inline]
212 pub fn from_f16(f: Half) -> Self {
213 Scalar::Int(f.into())
214 }
215
216 #[inline]
217 pub fn from_f32(f: Single) -> Self {
218 Scalar::Int(f.into())
219 }
220
221 #[inline]
222 pub fn from_f64(f: Double) -> Self {
223 Scalar::Int(f.into())
224 }
225
226 #[inline]
227 pub fn from_f128(f: Quad) -> Self {
228 Scalar::Int(f.into())
229 }
230
231 #[inline]
240 pub fn to_bits_or_ptr_internal(
241 self,
242 target_size: Size,
243 ) -> Result<Either<u128, Pointer<Prov>>, ScalarSizeMismatch> {
244 match (&(target_size.bytes()), &(0)) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::Some(format_args!("you should never look at the bits of a ZST")));
}
}
};assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
245 Ok(match self {
246 Scalar::Int(int) => Left(int.try_to_bits(target_size).map_err(|size| {
247 ScalarSizeMismatch { target_size: target_size.bytes(), data_size: size.bytes() }
248 })?),
249 Scalar::Ptr(ptr, sz) => {
250 if target_size.bytes() != u64::from(sz) {
251 return Err(ScalarSizeMismatch {
252 target_size: target_size.bytes(),
253 data_size: sz.into(),
254 });
255 }
256 Right(ptr)
257 }
258 })
259 }
260
261 #[inline]
262 pub fn size(self) -> Size {
263 match self {
264 Scalar::Int(int) => int.size(),
265 Scalar::Ptr(_ptr, sz) => Size::from_bytes(sz),
266 }
267 }
268}
269
270impl<'tcx, Prov: Provenance> Scalar<Prov> {
271 pub fn to_pointer(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, Pointer<Option<Prov>>> {
272 match self
273 .to_bits_or_ptr_internal(cx.pointer_size())
274 .map_err(|s| crate::mir::interpret::InterpErrorKind::UndefinedBehavior(crate::mir::interpret::UndefinedBehaviorInfo::ScalarSizeMismatch(s))err_ub!(ScalarSizeMismatch(s)))?
275 {
276 Right(ptr) => interp_ok(ptr.into()),
277 Left(bits) => {
278 let addr = u64::try_from(bits).unwrap();
279 interp_ok(Pointer::without_provenance(addr))
280 }
281 }
282 }
283
284 #[inline]
294 pub fn try_to_scalar_int(self) -> Result<ScalarInt, Scalar<AllocId>> {
295 match self {
296 Scalar::Int(int) => Ok(int),
297 Scalar::Ptr(ptr, sz) => {
298 if Prov::OFFSET_IS_ADDR {
299 Ok(ScalarInt::try_from_uint(ptr.offset.bytes(), Size::from_bytes(sz)).unwrap())
300 } else {
301 let (prov, offset) = ptr.into_raw_parts();
303 Err(Scalar::Ptr(Pointer::new(prov.get_alloc_id().unwrap(), offset), sz))
305 }
306 }
307 }
308 }
309
310 pub fn clear_provenance(&mut self) -> InterpResult<'tcx> {
311 if #[allow(non_exhaustive_omitted_patterns)] match self {
Scalar::Ptr(..) => true,
_ => false,
}matches!(self, Scalar::Ptr(..)) {
312 *self = self.to_scalar_int()?.into();
313 }
314 interp_ok(())
315 }
316
317 #[inline(always)]
318 pub fn to_scalar_int(self) -> InterpResult<'tcx, ScalarInt> {
319 self.try_to_scalar_int().map_err(|_| crate::mir::interpret::InterpErrorKind::Unsupported(crate::mir::interpret::UnsupportedOpInfo::ReadPointerAsInt(None))err_unsup!(ReadPointerAsInt(None))).into()
320 }
321
322 #[inline(always)]
323 #[cfg_attr(debug_assertions, track_caller)] pub fn assert_scalar_int(self) -> ScalarInt {
325 self.try_to_scalar_int().expect("got a pointer where a ScalarInt was expected")
326 }
327
328 #[inline]
331 pub fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
332 match (&(target_size.bytes()), &(0)) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::Some(format_args!("you should never look at the bits of a ZST")));
}
}
};assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
333 self.to_scalar_int()?
334 .try_to_bits(target_size)
335 .map_err(|size| {
336 crate::mir::interpret::InterpErrorKind::UndefinedBehavior(crate::mir::interpret::UndefinedBehaviorInfo::ScalarSizeMismatch(ScalarSizeMismatch {
target_size: target_size.bytes(),
data_size: size.bytes(),
}))err_ub!(ScalarSizeMismatch(ScalarSizeMismatch {
337 target_size: target_size.bytes(),
338 data_size: size.bytes(),
339 }))
340 })
341 .into()
342 }
343
344 pub fn to_bool(self) -> InterpResult<'tcx, bool> {
345 let val = self.to_u8()?;
346 match val {
347 0 => interp_ok(false),
348 1 => interp_ok(true),
349 _ => do yeet crate::mir::interpret::InterpErrorKind::UndefinedBehavior(crate::mir::interpret::UndefinedBehaviorInfo::InvalidBool(val))throw_ub!(InvalidBool(val)),
350 }
351 }
352
353 pub fn to_char(self) -> InterpResult<'tcx, char> {
354 let val = self.to_u32()?;
355 match std::char::from_u32(val) {
356 Some(c) => interp_ok(c),
357 None => do yeet crate::mir::interpret::InterpErrorKind::UndefinedBehavior(crate::mir::interpret::UndefinedBehaviorInfo::InvalidChar(val))throw_ub!(InvalidChar(val)),
358 }
359 }
360
361 #[inline]
364 pub fn to_uint(self, size: Size) -> InterpResult<'tcx, u128> {
365 self.to_bits(size)
366 }
367
368 pub fn to_u8(self) -> InterpResult<'tcx, u8> {
370 self.to_uint(Size::from_bits(8)).map(|v| u8::try_from(v).unwrap())
371 }
372
373 pub fn to_u16(self) -> InterpResult<'tcx, u16> {
375 self.to_uint(Size::from_bits(16)).map(|v| u16::try_from(v).unwrap())
376 }
377
378 pub fn to_u32(self) -> InterpResult<'tcx, u32> {
380 self.to_uint(Size::from_bits(32)).map(|v| u32::try_from(v).unwrap())
381 }
382
383 pub fn to_u64(self) -> InterpResult<'tcx, u64> {
385 self.to_uint(Size::from_bits(64)).map(|v| u64::try_from(v).unwrap())
386 }
387
388 pub fn to_u128(self) -> InterpResult<'tcx, u128> {
390 self.to_uint(Size::from_bits(128))
391 }
392
393 pub fn to_target_usize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
396 let b = self.to_uint(cx.data_layout().pointer_size())?;
397 interp_ok(u64::try_from(b).unwrap())
398 }
399
400 #[inline]
403 pub fn to_int(self, size: Size) -> InterpResult<'tcx, i128> {
404 let b = self.to_bits(size)?;
405 interp_ok(size.sign_extend(b))
406 }
407
408 pub fn to_i8(self) -> InterpResult<'tcx, i8> {
410 self.to_int(Size::from_bits(8)).map(|v| i8::try_from(v).unwrap())
411 }
412
413 pub fn to_i16(self) -> InterpResult<'tcx, i16> {
415 self.to_int(Size::from_bits(16)).map(|v| i16::try_from(v).unwrap())
416 }
417
418 pub fn to_i32(self) -> InterpResult<'tcx, i32> {
420 self.to_int(Size::from_bits(32)).map(|v| i32::try_from(v).unwrap())
421 }
422
423 pub fn to_i64(self) -> InterpResult<'tcx, i64> {
425 self.to_int(Size::from_bits(64)).map(|v| i64::try_from(v).unwrap())
426 }
427
428 pub fn to_i128(self) -> InterpResult<'tcx, i128> {
430 self.to_int(Size::from_bits(128))
431 }
432
433 pub fn to_target_isize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, i64> {
436 let b = self.to_int(cx.data_layout().pointer_size())?;
437 interp_ok(i64::try_from(b).unwrap())
438 }
439
440 #[inline]
441 pub fn to_float<F: Float>(self) -> InterpResult<'tcx, F> {
442 interp_ok(F::from_bits(self.to_bits(Size::from_bits(F::BITS))?))
444 }
445
446 #[inline]
447 pub fn to_f16(self) -> InterpResult<'tcx, Half> {
448 self.to_float()
449 }
450
451 #[inline]
452 pub fn to_f32(self) -> InterpResult<'tcx, Single> {
453 self.to_float()
454 }
455
456 #[inline]
457 pub fn to_f64(self) -> InterpResult<'tcx, Double> {
458 self.to_float()
459 }
460
461 #[inline]
462 pub fn to_f128(self) -> InterpResult<'tcx, Quad> {
463 self.to_float()
464 }
465}