Skip to content

Rollup of 10 pull requests #94225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Feb 22, 2022
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5cc292e
rustc_const_eval: adopt let else in more places
est31 Feb 18, 2022
297364e
Some improvements to the async docs
Feb 19, 2022
f233323
Gracefully handle non-UTF-8 string slices when pretty printing
tmiasko Feb 19, 2022
72a7e73
Update pin_static_ref stabilization version.
ehuss Feb 20, 2022
c358ffe
Implement LowerHex on Scalar to clean up their display in rustdoc
GuillaumeGomez Feb 20, 2022
b45cb09
Use Metadata::modified instead of FileTime::from_last_modification_ti…
bjorn3 Feb 20, 2022
1e3609b
CTFE engine: Scalar: expose size-generic to_(u)int methods
RalfJung Feb 21, 2022
413f3f7
Fix typo
est31 Feb 21, 2022
76ea566
Better error if the user tries to do assignment ... else
est31 Feb 21, 2022
239f33e
add comment
lcnr Feb 21, 2022
15e95c0
rename function
lcnr Feb 21, 2022
ec0a0ca
don't check for the leak_check twice
lcnr Feb 21, 2022
6a1f5ea
obligation forest docs
lcnr Feb 21, 2022
910d46f
Correctly handle miniz_oxide extern crate declaration
GuillaumeGomez Feb 21, 2022
12705b4
Rollup merge of #91192 - r00ster91:futuredocs, r=GuillaumeGomez
matthiaskrgr Feb 21, 2022
ea7f7f7
Rollup merge of #94143 - est31:let_else_const_eval, r=lcnr
matthiaskrgr Feb 21, 2022
da25e1e
Rollup merge of #94156 - tmiasko:pp-str, r=petrochenkov
matthiaskrgr Feb 21, 2022
74cb6b7
Rollup merge of #94186 - ehuss:pin-stable-1.61, r=m-ou-se
matthiaskrgr Feb 21, 2022
f639ba6
Rollup merge of #94189 - GuillaumeGomez:scalar-lower-hex, r=RalfJung
matthiaskrgr Feb 21, 2022
3095743
Rollup merge of #94190 - bjorn3:less_filetime, r=Mark-Simulacrum
matthiaskrgr Feb 21, 2022
f3a1a8c
Rollup merge of #94203 - RalfJung:to_sized_int, r=oli-obk
matthiaskrgr Feb 21, 2022
d3649f8
Rollup merge of #94211 - est31:let_else_destructuring_error, r=matthe…
matthiaskrgr Feb 21, 2022
9157775
Rollup merge of #94215 - lcnr:leak-check, r=jackh726
matthiaskrgr Feb 21, 2022
ed35309
Rollup merge of #94220 - GuillaumeGomez:miniz-oxide-decl, r=Amanieu
matthiaskrgr Feb 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
CTFE engine: Scalar: expose size-generic to_(u)int methods
  • Loading branch information
RalfJung committed Feb 21, 2022
commit 1e3609b1ba6cc83a7a7897202ddfca42e5dbf0ad
46 changes: 25 additions & 21 deletions compiler/rustc_middle/src/mir/interpret/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,78 +370,82 @@ impl<'tcx, Tag: Provenance> Scalar<Tag> {
}
}

/// Converts the scalar to produce an unsigned integer of the given size.
/// Fails if the scalar is a pointer.
#[inline]
fn to_unsigned_with_bit_width(self, bits: u64) -> InterpResult<'static, u128> {
let sz = Size::from_bits(bits);
self.to_bits(sz)
pub fn to_uint(self, size: Size) -> InterpResult<'static, u128> {
self.to_bits(size)
}

/// Converts the scalar to produce a `u8`. Fails if the scalar is a pointer.
pub fn to_u8(self) -> InterpResult<'static, u8> {
self.to_unsigned_with_bit_width(8).map(|v| u8::try_from(v).unwrap())
self.to_uint(Size::from_bits(8)).map(|v| u8::try_from(v).unwrap())
}

/// Converts the scalar to produce a `u16`. Fails if the scalar is a pointer.
pub fn to_u16(self) -> InterpResult<'static, u16> {
self.to_unsigned_with_bit_width(16).map(|v| u16::try_from(v).unwrap())
self.to_uint(Size::from_bits(16)).map(|v| u16::try_from(v).unwrap())
}

/// Converts the scalar to produce a `u32`. Fails if the scalar is a pointer.
pub fn to_u32(self) -> InterpResult<'static, u32> {
self.to_unsigned_with_bit_width(32).map(|v| u32::try_from(v).unwrap())
self.to_uint(Size::from_bits(32)).map(|v| u32::try_from(v).unwrap())
}

/// Converts the scalar to produce a `u64`. Fails if the scalar is a pointer.
pub fn to_u64(self) -> InterpResult<'static, u64> {
self.to_unsigned_with_bit_width(64).map(|v| u64::try_from(v).unwrap())
self.to_uint(Size::from_bits(64)).map(|v| u64::try_from(v).unwrap())
}

/// Converts the scalar to produce a `u128`. Fails if the scalar is a pointer.
pub fn to_u128(self) -> InterpResult<'static, u128> {
self.to_unsigned_with_bit_width(128)
self.to_uint(Size::from_bits(128))
}

/// Converts the scalar to produce a machine-pointer-sized unsigned integer.
/// Fails if the scalar is a pointer.
pub fn to_machine_usize(self, cx: &impl HasDataLayout) -> InterpResult<'static, u64> {
let b = self.to_bits(cx.data_layout().pointer_size)?;
let b = self.to_uint(cx.data_layout().pointer_size)?;
Ok(u64::try_from(b).unwrap())
}

/// Converts the scalar to produce a signed integer of the given size.
/// Fails if the scalar is a pointer.
#[inline]
fn to_signed_with_bit_width(self, bits: u64) -> InterpResult<'static, i128> {
let sz = Size::from_bits(bits);
let b = self.to_bits(sz)?;
Ok(sz.sign_extend(b) as i128)
pub fn to_int(self, size: Size) -> InterpResult<'static, i128> {
let b = self.to_bits(size)?;
Ok(size.sign_extend(b) as i128)
}

/// Converts the scalar to produce an `i8`. Fails if the scalar is a pointer.
pub fn to_i8(self) -> InterpResult<'static, i8> {
self.to_signed_with_bit_width(8).map(|v| i8::try_from(v).unwrap())
self.to_int(Size::from_bits(8)).map(|v| i8::try_from(v).unwrap())
}

/// Converts the scalar to produce an `i16`. Fails if the scalar is a pointer.
pub fn to_i16(self) -> InterpResult<'static, i16> {
self.to_signed_with_bit_width(16).map(|v| i16::try_from(v).unwrap())
self.to_int(Size::from_bits(16)).map(|v| i16::try_from(v).unwrap())
}

/// Converts the scalar to produce an `i32`. Fails if the scalar is a pointer.
pub fn to_i32(self) -> InterpResult<'static, i32> {
self.to_signed_with_bit_width(32).map(|v| i32::try_from(v).unwrap())
self.to_int(Size::from_bits(32)).map(|v| i32::try_from(v).unwrap())
}

/// Converts the scalar to produce an `i64`. Fails if the scalar is a pointer.
pub fn to_i64(self) -> InterpResult<'static, i64> {
self.to_signed_with_bit_width(64).map(|v| i64::try_from(v).unwrap())
self.to_int(Size::from_bits(64)).map(|v| i64::try_from(v).unwrap())
}

/// Converts the scalar to produce an `i128`. Fails if the scalar is a pointer.
pub fn to_i128(self) -> InterpResult<'static, i128> {
self.to_signed_with_bit_width(128)
self.to_int(Size::from_bits(128))
}

/// Converts the scalar to produce a machine-pointer-sized signed integer.
/// Fails if the scalar is a pointer.
pub fn to_machine_isize(self, cx: &impl HasDataLayout) -> InterpResult<'static, i64> {
let sz = cx.data_layout().pointer_size;
let b = self.to_bits(sz)?;
let b = sz.sign_extend(b) as i128;
let b = self.to_int(cx.data_layout().pointer_size)?;
Ok(i64::try_from(b).unwrap())
}

Expand Down