Skip to content

Commit 400e28d

Browse files
committed
fix validation range printing when encountering undef
1 parent 33b0b71 commit 400e28d

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

src/librustc_mir/interpret/validity.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,13 @@ impl<'rt, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>>
449449
}
450450
// At least one value is excluded. Get the bits.
451451
let value = try_validation!(value.not_undef(),
452-
value, self.path,
453-
format!("something in the range {:?}", layout.valid_range));
452+
value,
453+
self.path,
454+
format!(
455+
"something {}",
456+
wrapping_range_format(&layout.valid_range, max_hi),
457+
)
458+
);
454459
let bits = match value {
455460
Scalar::Ptr(ptr) => {
456461
if lo == 1 && hi == max_hi {

src/test/ui/consts/const-eval/ub-nonnull.rs

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ const NULL_U8: NonZeroU8 = unsafe { mem::transmute(0u8) };
1313
const NULL_USIZE: NonZeroUsize = unsafe { mem::transmute(0usize) };
1414
//~^ ERROR it is undefined behavior to use this value
1515

16+
union Transmute {
17+
uninit: (),
18+
out: NonZeroU8,
19+
}
20+
const UNINIT: NonZeroU8 = unsafe { Transmute { uninit: () }.out };
21+
//~^ ERROR it is undefined behavior to use this value
22+
1623
// Also test other uses of rustc_layout_scalar_valid_range_start
1724

1825
#[rustc_layout_scalar_valid_range_start(10)]

src/test/ui/consts/const-eval/ub-nonnull.stderr

+11-3
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,29 @@ LL | const NULL_USIZE: NonZeroUsize = unsafe { mem::transmute(0usize) };
2323
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
2424

2525
error[E0080]: it is undefined behavior to use this value
26-
--> $DIR/ub-nonnull.rs:21:1
26+
--> $DIR/ub-nonnull.rs:20:1
27+
|
28+
LL | const UNINIT: NonZeroU8 = unsafe { Transmute { uninit: () }.out };
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected something greater or equal to 1
30+
|
31+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
32+
33+
error[E0080]: it is undefined behavior to use this value
34+
--> $DIR/ub-nonnull.rs:28:1
2735
|
2836
LL | const BAD_RANGE1: RestrictedRange1 = unsafe { RestrictedRange1(42) };
2937
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 42, but expected something in the range 10..=30
3038
|
3139
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
3240

3341
error[E0080]: it is undefined behavior to use this value
34-
--> $DIR/ub-nonnull.rs:27:1
42+
--> $DIR/ub-nonnull.rs:34:1
3543
|
3644
LL | const BAD_RANGE2: RestrictedRange2 = unsafe { RestrictedRange2(20) };
3745
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 20, but expected something less or equal to 10, or greater or equal to 30
3846
|
3947
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
4048

41-
error: aborting due to 5 previous errors
49+
error: aborting due to 6 previous errors
4250

4351
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)