Skip to content

Commit 94c9c22

Browse files
committed
explain why CTFE/Miri perform truncation on shift offset
1 parent 311fa1f commit 94c9c22

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

compiler/rustc_const_eval/src/interpret/operator.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
130130
let signed = left_layout.abi.is_signed();
131131
let size = u128::from(left_layout.size.bits());
132132
let overflow = r >= size;
133-
let r = r % size; // mask to type size
133+
// The shift offset is implicitly masked to the type size, to make sure this operation
134+
// is always defined. This is the one MIR operator that does *not* directly map to a
135+
// single LLVM operation. See
136+
// <https://github.com/rust-lang/rust/blob/a3b9405ae7bb6ab4e8103b414e75c44598a10fd2/compiler/rustc_codegen_ssa/src/common.rs#L131-L158>
137+
// for the corresponding truncation in our codegen backends.
138+
let r = r % size;
134139
let r = u32::try_from(r).unwrap(); // we masked so this will always fit
135140
let result = if signed {
136141
let l = self.sign_extend(l, left_layout) as i128;

0 commit comments

Comments
 (0)