@@ -382,28 +382,26 @@ pub trait LayoutCalculator {
382
382
let (start, end) = scalar_valid_range;
383
383
match st.abi {
384
384
Abi::Scalar(ref mut scalar) | Abi::ScalarPair(ref mut scalar, _) => {
385
- // the asserts ensure that we are not using the
386
- // `#[rustc_layout_scalar_valid_range(n)]`
387
- // attribute to widen the range of anything as that would probably
388
- // result in UB somewhere
389
- // FIXME(eddyb) the asserts are probably not needed,
390
- // as larger validity ranges would result in missed
385
+ // Enlarging validity ranges would result in missed
391
386
// optimizations, *not* wrongly assuming the inner
392
- // value is valid. e.g. unions enlarge validity ranges,
387
+ // value is valid. e.g. unions already enlarge validity ranges,
393
388
// because the values may be uninitialized.
389
+ //
390
+ // Because of that we only check that the start and end
391
+ // of the range is representable with this scalar type.
392
+
393
+ let max_value = scalar.size(dl).unsigned_int_max();
394
394
if let Bound::Included(start) = start {
395
395
// FIXME(eddyb) this might be incorrect - it doesn't
396
396
// account for wrap-around (end < start) ranges.
397
- let valid_range = scalar.valid_range_mut();
398
- assert!(valid_range.start <= start);
399
- valid_range.start = start;
397
+ assert!(start <= max_value, "{start} > {max_value}");
398
+ scalar.valid_range_mut().start = start;
400
399
}
401
400
if let Bound::Included(end) = end {
402
401
// FIXME(eddyb) this might be incorrect - it doesn't
403
402
// account for wrap-around (end < start) ranges.
404
- let valid_range = scalar.valid_range_mut();
405
- assert!(valid_range.end >= end);
406
- valid_range.end = end;
403
+ assert!(end <= max_value, "{end} > {max_value}");
404
+ scalar.valid_range_mut().end = end;
407
405
}
408
406
409
407
// Update `largest_niche` if we have introduced a larger niche.
0 commit comments