Skip to content

Commit d7f5d78

Browse files
committed
Simplify and document range layout computation
1 parent fcb1f18 commit d7f5d78

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

compiler/rustc_abi/src/layout.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -382,28 +382,26 @@ pub trait LayoutCalculator {
382382
let (start, end) = scalar_valid_range;
383383
match st.abi {
384384
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
391386
// 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,
393388
// 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();
394394
if let Bound::Included(start) = start {
395395
// FIXME(eddyb) this might be incorrect - it doesn't
396396
// 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;
400399
}
401400
if let Bound::Included(end) = end {
402401
// FIXME(eddyb) this might be incorrect - it doesn't
403402
// 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;
407405
}
408406

409407
// Update `largest_niche` if we have introduced a larger niche.

0 commit comments

Comments
 (0)