diff options
| -rw-r--r-- | compiler/rustc_middle/src/ty/consts/int.rs | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/compiler/rustc_middle/src/ty/consts/int.rs b/compiler/rustc_middle/src/ty/consts/int.rs index 6b8e9edc13c..298e3f5de6f 100644 --- a/compiler/rustc_middle/src/ty/consts/int.rs +++ b/compiler/rustc_middle/src/ty/consts/int.rs @@ -131,6 +131,10 @@ pub struct ScalarInt { // can't take references to fields of packed structs. impl<CTX> crate::ty::HashStable<CTX> for ScalarInt { fn hash_stable(&self, hcx: &mut CTX, hasher: &mut crate::ty::StableHasher) { + // Using a block `{self.data}` here to force a copy instead of using `self.data` + // directly, because `hash_stable` takes `&self` and would thus borrow `self.data`. + // Since `Self` is a packed struct, that would create a possibly unaligned reference, + // which is UB on a lot of platforms. { self.data }.hash_stable(hcx, hasher); self.size.hash_stable(hcx, hasher); } @@ -167,6 +171,11 @@ impl ScalarInt { /// construct `Scalar`s). #[inline(always)] fn check_data(self) { + // Using a block `{self.data}` here to force a copy instead of using `self.data` + // directly, because `assert_eq` takes references to its arguments and formatting + // arguments and would thus borrow `self.data`. Since `Self` + // is a packed struct, that would create a possibly unaligned reference, which + // is UB on a lot of platforms. debug_assert_eq!( truncate(self.data, self.size()), { self.data }, @@ -336,6 +345,11 @@ impl fmt::LowerHex for ScalarInt { self.check_data(); // Format as hex number wide enough to fit any value of the given `size`. // So data=20, size=1 will be "0x14", but with size=4 it'll be "0x00000014". + // Using a block `{self.data}` here to force a copy instead of using `self.data` + // directly, because `write!` takes references to its formatting arguments and + // would thus borrow `self.data`. Since `Self` + // is a packed struct, that would create a possibly unaligned reference, which + // is UB on a lot of platforms. write!(f, "{:01$x}", { self.data }, self.size as usize * 2) } } |
