Skip to content

Commit 388d99d

Browse files
committed
fix tests
1 parent e4c39e1 commit 388d99d

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ const NON_NULL_PTR: NonNull<u8> = unsafe { mem::transmute(&1) };
1111
const NULL_PTR: NonNull<u8> = unsafe { mem::transmute(0usize) };
1212
//~^ ERROR it is undefined behavior to use this value
1313

14+
#[deny(const_err)] // this triggers a `const_err` so validation does not even happen
1415
const OUT_OF_BOUNDS_PTR: NonNull<u8> = { unsafe {
15-
//~^ ERROR it is undefined behavior to use this value
16-
let ptr: &(u8, u8, u8) = mem::transmute(&0u8); // &0 gets promoted so it does not dangle
17-
let out_of_bounds_ptr = &ptr.2; // use address-of-field for pointer arithmetic
16+
let ptr: &[u8; 256] = mem::transmute(&0u8); // &0 gets promoted so it does not dangle
17+
// Use address-of-element for pointer arithmetic. This could wrap around to NULL!
18+
let out_of_bounds_ptr = &ptr[255]; //~ ERROR any use of this value will cause an error
1819
mem::transmute(out_of_bounds_ptr)
1920
} };
2021

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

+17-12
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,58 @@ LL | const NULL_PTR: NonNull<u8> = unsafe { mem::transmute(0usize) };
66
|
77
= 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
88

9-
error[E0080]: it is undefined behavior to use this value
10-
--> $DIR/ub-nonnull.rs:14:1
9+
error: any use of this value will cause an error
10+
--> $DIR/ub-nonnull.rs:18:29
1111
|
1212
LL | / const OUT_OF_BOUNDS_PTR: NonNull<u8> = { unsafe {
13-
LL | |
14-
LL | | let ptr: &(u8, u8, u8) = mem::transmute(&0u8); // &0 gets promoted so it does not dangle
15-
LL | | let out_of_bounds_ptr = &ptr.2; // use address-of-field for pointer arithmetic
13+
LL | | let ptr: &[u8; 256] = mem::transmute(&0u8); // &0 gets promoted so it does not dangle
14+
LL | | // Use address-of-element for pointer arithmetic. This could wrap around to NULL!
15+
LL | | let out_of_bounds_ptr = &ptr[255];
16+
| | ^^^^^^^^^ Memory access failed: pointer must be in-bounds at offset 256, but is outside bounds of allocation 6 which has size 1
1617
LL | | mem::transmute(out_of_bounds_ptr)
1718
LL | | } };
18-
| |____^ type validation failed: encountered a potentially NULL pointer, but expected something that cannot possibly fail to be greater or equal to 1
19+
| |____-
1920
|
20-
= 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
21+
note: lint level defined here
22+
--> $DIR/ub-nonnull.rs:14:8
23+
|
24+
LL | #[deny(const_err)] // this triggers a `const_err` so validation does not even happen
25+
| ^^^^^^^^^
2126

2227
error[E0080]: it is undefined behavior to use this value
23-
--> $DIR/ub-nonnull.rs:21:1
28+
--> $DIR/ub-nonnull.rs:22:1
2429
|
2530
LL | const NULL_U8: NonZeroU8 = unsafe { mem::transmute(0u8) };
2631
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected something greater or equal to 1
2732
|
2833
= 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
2934

3035
error[E0080]: it is undefined behavior to use this value
31-
--> $DIR/ub-nonnull.rs:23:1
36+
--> $DIR/ub-nonnull.rs:24:1
3237
|
3338
LL | const NULL_USIZE: NonZeroUsize = unsafe { mem::transmute(0usize) };
3439
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected something greater or equal to 1
3540
|
3641
= 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
3742

3843
error[E0080]: it is undefined behavior to use this value
39-
--> $DIR/ub-nonnull.rs:30:1
44+
--> $DIR/ub-nonnull.rs:31:1
4045
|
4146
LL | const UNINIT: NonZeroU8 = unsafe { Transmute { uninit: () }.out };
4247
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected something greater or equal to 1
4348
|
4449
= 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
4550

4651
error[E0080]: it is undefined behavior to use this value
47-
--> $DIR/ub-nonnull.rs:38:1
52+
--> $DIR/ub-nonnull.rs:39:1
4853
|
4954
LL | const BAD_RANGE1: RestrictedRange1 = unsafe { RestrictedRange1(42) };
5055
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 42, but expected something in the range 10..=30
5156
|
5257
= 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
5358

5459
error[E0080]: it is undefined behavior to use this value
55-
--> $DIR/ub-nonnull.rs:44:1
60+
--> $DIR/ub-nonnull.rs:45:1
5661
|
5762
LL | const BAD_RANGE2: RestrictedRange2 = unsafe { RestrictedRange2(20) };
5863
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 20, but expected something less or equal to 10, or greater or equal to 30

0 commit comments

Comments
 (0)