Skip to content

Commit df20355

Browse files
committed
Auto merge of rust-lang#95456 - RalfJung:size, r=oli-obk
allow large Size again This basically reverts most of rust-lang#80042, and instead does the panic in `bits()` with a `#[cold]` function to make sure it does not get inlined. rust-lang#80042 added a comment about an invariant ("The top 3 bits are ALWAYS zero") that is not actually enforced, and if it were enforced that would be a problem for rust-lang#95388. So I think we should not have that invariant, and I adjusted the code accordingly. r? `@oli-obk` Cc `@sivadeilra`
2 parents e730969 + 2799885 commit df20355

File tree

1 file changed

+7
-16
lines changed
  • compiler/rustc_target/src/abi

1 file changed

+7
-16
lines changed

compiler/rustc_target/src/abi/mod.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -279,30 +279,16 @@ impl ToJson for Endian {
279279
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Encodable, Decodable)]
280280
#[derive(HashStable_Generic)]
281281
pub struct Size {
282-
// The top 3 bits are ALWAYS zero.
283282
raw: u64,
284283
}
285284

286285
impl Size {
287286
pub const ZERO: Size = Size { raw: 0 };
288287

289288
/// Rounds `bits` up to the next-higher byte boundary, if `bits` is
290-
/// is not aligned.
289+
/// not a multiple of 8.
291290
pub fn from_bits(bits: impl TryInto<u64>) -> Size {
292291
let bits = bits.try_into().ok().unwrap();
293-
294-
#[cold]
295-
fn overflow(bits: u64) -> ! {
296-
panic!("Size::from_bits({}) has overflowed", bits);
297-
}
298-
299-
// This is the largest value of `bits` that does not cause overflow
300-
// during rounding, and guarantees that the resulting number of bytes
301-
// cannot cause overflow when multiplied by 8.
302-
if bits > 0xffff_ffff_ffff_fff8 {
303-
overflow(bits);
304-
}
305-
306292
// Avoid potential overflow from `bits + 7`.
307293
Size { raw: bits / 8 + ((bits % 8) + 7) / 8 }
308294
}
@@ -325,7 +311,12 @@ impl Size {
325311

326312
#[inline]
327313
pub fn bits(self) -> u64 {
328-
self.raw << 3
314+
#[cold]
315+
fn overflow(bytes: u64) -> ! {
316+
panic!("Size::bits: {} bytes in bits doesn't fit in u64", bytes)
317+
}
318+
319+
self.bytes().checked_mul(8).unwrap_or_else(|| overflow(self.bytes()))
329320
}
330321

331322
#[inline]

0 commit comments

Comments
 (0)