Skip to content

Commit 3ec8974

Browse files
committed
test diagnostics for more ranges
1 parent b42b9b3 commit 3ec8974

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(const_transmute)]
11+
#![feature(rustc_attrs, const_transmute)]
1212
#![allow(const_err)] // make sure we cannot allow away the errors tested here
1313

1414
use std::mem;
@@ -23,4 +23,18 @@ const NULL_U8: NonZeroU8 = unsafe { mem::transmute(0u8) };
2323
const NULL_USIZE: NonZeroUsize = unsafe { mem::transmute(0usize) };
2424
//~^ ERROR it is undefined behavior to use this value
2525

26+
// Also test other uses of rustc_layout_scalar_valid_range_start
27+
28+
#[rustc_layout_scalar_valid_range_start(10)]
29+
#[rustc_layout_scalar_valid_range_end(30)]
30+
struct RestrictedRange1(u32);
31+
const BAD_RANGE1: RestrictedRange1 = unsafe { RestrictedRange1(42) };
32+
//~^ ERROR it is undefined behavior to use this value
33+
34+
#[rustc_layout_scalar_valid_range_start(30)]
35+
#[rustc_layout_scalar_valid_range_end(10)]
36+
struct RestrictedRange2(u32);
37+
const BAD_RANGE2: RestrictedRange2 = unsafe { RestrictedRange2(20) };
38+
//~^ ERROR it is undefined behavior to use this value
39+
2640
fn main() {}

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ LL | const NULL_USIZE: NonZeroUsize = unsafe { mem::transmute(0usize) };
2222
|
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

25-
error: aborting due to 3 previous errors
25+
error[E0080]: it is undefined behavior to use this value
26+
--> $DIR/ub-nonnull.rs:31:1
27+
|
28+
LL | const BAD_RANGE1: RestrictedRange1 = unsafe { RestrictedRange1(42) };
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 42, but expected something in the range 10..=30
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:37:1
35+
|
36+
LL | const BAD_RANGE2: RestrictedRange2 = unsafe { RestrictedRange2(20) };
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 20, but expected something less or equal to 10, or greater or equal to 30
38+
|
39+
= 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
40+
41+
error: aborting due to 5 previous errors
2642

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

0 commit comments

Comments
 (0)