Skip to content

Commit fb4cc99

Browse files
Rollup merge of rust-lang#141582 - RalfJung:cleanup, r=bjorn3
intrinsics, ScalarInt: minor cleanup Taken out of rust-lang#141507 while we resolve technical disagreements in that PR. r? ``@bjorn3``
2 parents fbac805 + ff33414 commit fb4cc99

File tree

4 files changed

+60
-28
lines changed

4 files changed

+60
-28
lines changed

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ impl<'tcx, Prov: Provenance> ImmTy<'tcx, Prov> {
310310
let ty = tcx.ty_ordering_enum(None);
311311
let layout =
312312
tcx.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ty)).unwrap();
313-
Self::from_scalar(Scalar::from_i8(c as i8), layout)
313+
Self::from_scalar(Scalar::Int(c.into()), layout)
314314
}
315315

316316
pub fn from_pair(a: Self, b: Self, cx: &(impl HasTypingEnv<'tcx> + HasTyCtxt<'tcx>)) -> Self {

compiler/rustc_middle/src/mir/interpret/value.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,27 +180,27 @@ impl<Prov> Scalar<Prov> {
180180

181181
#[inline]
182182
pub fn from_i8(i: i8) -> Self {
183-
Self::from_int(i, Size::from_bits(8))
183+
Self::Int(i.into())
184184
}
185185

186186
#[inline]
187187
pub fn from_i16(i: i16) -> Self {
188-
Self::from_int(i, Size::from_bits(16))
188+
Self::Int(i.into())
189189
}
190190

191191
#[inline]
192192
pub fn from_i32(i: i32) -> Self {
193-
Self::from_int(i, Size::from_bits(32))
193+
Self::Int(i.into())
194194
}
195195

196196
#[inline]
197197
pub fn from_i64(i: i64) -> Self {
198-
Self::from_int(i, Size::from_bits(64))
198+
Self::Int(i.into())
199199
}
200200

201201
#[inline]
202202
pub fn from_i128(i: i128) -> Self {
203-
Self::from_int(i, Size::from_bits(128))
203+
Self::Int(i.into())
204204
}
205205

206206
#[inline]

compiler/rustc_middle/src/ty/consts/int.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,9 @@ macro_rules! from_scalar_int_for_x {
422422
impl From<ScalarInt> for $ty {
423423
#[inline]
424424
fn from(int: ScalarInt) -> Self {
425-
// The `unwrap` cannot fail because to_bits (if it succeeds)
425+
// The `unwrap` cannot fail because to_uint (if it succeeds)
426426
// is guaranteed to return a value that fits into the size.
427-
int.to_bits(Size::from_bytes(size_of::<$ty>()))
427+
int.to_uint(Size::from_bytes(size_of::<$ty>()))
428428
.try_into().unwrap()
429429
}
430430
}
@@ -450,6 +450,49 @@ impl From<char> for ScalarInt {
450450
}
451451
}
452452

453+
macro_rules! from_x_for_scalar_int_signed {
454+
($($ty:ty),*) => {
455+
$(
456+
impl From<$ty> for ScalarInt {
457+
#[inline]
458+
fn from(u: $ty) -> Self {
459+
Self {
460+
data: u128::from(u.cast_unsigned()), // go via the unsigned type of the same size
461+
size: NonZero::new(size_of::<$ty>() as u8).unwrap(),
462+
}
463+
}
464+
}
465+
)*
466+
}
467+
}
468+
469+
macro_rules! from_scalar_int_for_x_signed {
470+
($($ty:ty),*) => {
471+
$(
472+
impl From<ScalarInt> for $ty {
473+
#[inline]
474+
fn from(int: ScalarInt) -> Self {
475+
// The `unwrap` cannot fail because to_int (if it succeeds)
476+
// is guaranteed to return a value that fits into the size.
477+
int.to_int(Size::from_bytes(size_of::<$ty>()))
478+
.try_into().unwrap()
479+
}
480+
}
481+
)*
482+
}
483+
}
484+
485+
from_x_for_scalar_int_signed!(i8, i16, i32, i64, i128);
486+
from_scalar_int_for_x_signed!(i8, i16, i32, i64, i128);
487+
488+
impl From<std::cmp::Ordering> for ScalarInt {
489+
#[inline]
490+
fn from(c: std::cmp::Ordering) -> Self {
491+
// Here we rely on `Ordering` having the same values in host and target!
492+
ScalarInt::from(c as i8)
493+
}
494+
}
495+
453496
/// Error returned when a conversion from ScalarInt to char fails.
454497
#[derive(Debug)]
455498
pub struct CharTryFromScalarInt;

library/core/src/intrinsics/mod.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
//! Compiler intrinsics.
22
//!
3-
//! The corresponding definitions are in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_llvm/src/intrinsic.rs>.
4-
//! The corresponding const implementations are in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_const_eval/src/interpret/intrinsics.rs>.
3+
//! These are the imports making intrinsics available to Rust code. The actual implementations live in the compiler.
4+
//! Some of these intrinsics are lowered to MIR in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir_transform/src/lower_intrinsics.rs>.
5+
//! The remaining intrinsics are implemented for the LLVM backend in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_ssa/src/mir/intrinsic.rs>
6+
//! and <https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_llvm/src/intrinsic.rs>,
7+
//! and for const evaluation in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_const_eval/src/interpret/intrinsics.rs>.
58
//!
69
//! # Const intrinsics
710
//!
@@ -20,28 +23,14 @@
2023
//!
2124
//! The volatile intrinsics provide operations intended to act on I/O
2225
//! memory, which are guaranteed to not be reordered by the compiler
23-
//! across other volatile intrinsics. See the LLVM documentation on
24-
//! [[volatile]].
25-
//!
26-
//! [volatile]: https://llvm.org/docs/LangRef.html#volatile-memory-accesses
26+
//! across other volatile intrinsics. See [`read_volatile`][ptr::read_volatile]
27+
//! and [`write_volatile`][ptr::write_volatile].
2728
//!
2829
//! # Atomics
2930
//!
3031
//! The atomic intrinsics provide common atomic operations on machine
31-
//! words, with multiple possible memory orderings. They obey the same
32-
//! semantics as C++11. See the LLVM documentation on [[atomics]].
33-
//!
34-
//! [atomics]: https://llvm.org/docs/Atomics.html
35-
//!
36-
//! A quick refresher on memory ordering:
37-
//!
38-
//! * Acquire - a barrier for acquiring a lock. Subsequent reads and writes
39-
//! take place after the barrier.
40-
//! * Release - a barrier for releasing a lock. Preceding reads and writes
41-
//! take place before the barrier.
42-
//! * Sequentially consistent - sequentially consistent operations are
43-
//! guaranteed to happen in order. This is the standard mode for working
44-
//! with atomic types and is equivalent to Java's `volatile`.
32+
//! words, with multiple possible memory orderings. See the
33+
//! [atomic types][crate::sync::atomic] docs for details.
4534
//!
4635
//! # Unwinding
4736
//!

0 commit comments

Comments
 (0)