Skip to content

Commit 6563788

Browse files
committed
ub-ref-ptr: check some more of the detailed error messages
1 parent e2c01ca commit 6563788

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

tests/ui/consts/const-eval/ub-ref-ptr.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ const UNALIGNED_BOX: Box<u16> = unsafe { mem::transmute(&[0u8; 4]) };
2222

2323
const NULL: &u16 = unsafe { mem::transmute(0usize) };
2424
//~^ ERROR it is undefined behavior to use this value
25+
//~| null reference
2526

2627
const NULL_BOX: Box<u16> = unsafe { mem::transmute(0usize) };
2728
//~^ ERROR it is undefined behavior to use this value
29+
//~| null box
2830

2931

3032
// It is very important that we reject this: We do promote `&(4 * REF_AS_USIZE)`,
@@ -41,23 +43,28 @@ const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[usize],
4143

4244
const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) };
4345
//~^ ERROR it is undefined behavior to use this value
46+
//~| dangling reference
4447

4548
const USIZE_AS_BOX: Box<u8> = unsafe { mem::transmute(1337usize) };
4649
//~^ ERROR it is undefined behavior to use this value
50+
//~| dangling box
4751

4852
const UNINIT_PTR: *const i32 = unsafe { MaybeUninit { uninit: () }.init };
4953
//~^ ERROR evaluation of constant value failed
5054
//~| uninitialized
5155

5256
const NULL_FN_PTR: fn() = unsafe { mem::transmute(0usize) };
5357
//~^ ERROR it is undefined behavior to use this value
58+
//~| encountered null pointer, but expected a function pointer
5459
const UNINIT_FN_PTR: fn() = unsafe { MaybeUninit { uninit: () }.init };
5560
//~^ ERROR evaluation of constant value failed
5661
//~| uninitialized
5762
const DANGLING_FN_PTR: fn() = unsafe { mem::transmute(13usize) };
5863
//~^ ERROR it is undefined behavior to use this value
64+
//~| expected a function pointer
5965
const DATA_FN_PTR: fn() = unsafe { mem::transmute(&13) };
6066
//~^ ERROR it is undefined behavior to use this value
67+
//~| expected a function pointer
6168

6269

6370
const UNALIGNED_READ: () = unsafe {

tests/ui/consts/const-eval/ub-ref-ptr.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ LL | const NULL: &u16 = unsafe { mem::transmute(0usize) };
3232
}
3333

3434
error[E0080]: it is undefined behavior to use this value
35-
--> $DIR/ub-ref-ptr.rs:26:1
35+
--> $DIR/ub-ref-ptr.rs:27:1
3636
|
3737
LL | const NULL_BOX: Box<u16> = unsafe { mem::transmute(0usize) };
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a null box
@@ -43,7 +43,7 @@ LL | const NULL_BOX: Box<u16> = unsafe { mem::transmute(0usize) };
4343
}
4444

4545
error[E0080]: evaluation of constant value failed
46-
--> $DIR/ub-ref-ptr.rs:33:1
46+
--> $DIR/ub-ref-ptr.rs:35:1
4747
|
4848
LL | const REF_AS_USIZE: usize = unsafe { mem::transmute(&0) };
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into integer
@@ -52,7 +52,7 @@ LL | const REF_AS_USIZE: usize = unsafe { mem::transmute(&0) };
5252
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
5353

5454
error[E0080]: evaluation of constant value failed
55-
--> $DIR/ub-ref-ptr.rs:36:39
55+
--> $DIR/ub-ref-ptr.rs:38:39
5656
|
5757
LL | const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }];
5858
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into integer
@@ -61,13 +61,13 @@ LL | const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }];
6161
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
6262

6363
note: erroneous constant encountered
64-
--> $DIR/ub-ref-ptr.rs:36:38
64+
--> $DIR/ub-ref-ptr.rs:38:38
6565
|
6666
LL | const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }];
6767
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6868

6969
error[E0080]: evaluation of constant value failed
70-
--> $DIR/ub-ref-ptr.rs:39:86
70+
--> $DIR/ub-ref-ptr.rs:41:86
7171
|
7272
LL | const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[usize], _>(&[mem::transmute(&0)]) };
7373
| ^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into integer
@@ -76,13 +76,13 @@ LL | const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[us
7676
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
7777

7878
note: erroneous constant encountered
79-
--> $DIR/ub-ref-ptr.rs:39:85
79+
--> $DIR/ub-ref-ptr.rs:41:85
8080
|
8181
LL | const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[usize], _>(&[mem::transmute(&0)]) };
8282
| ^^^^^^^^^^^^^^^^^^^^^
8383

8484
error[E0080]: it is undefined behavior to use this value
85-
--> $DIR/ub-ref-ptr.rs:42:1
85+
--> $DIR/ub-ref-ptr.rs:44:1
8686
|
8787
LL | const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) };
8888
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a dangling reference (0x539[noalloc] has no provenance)
@@ -93,7 +93,7 @@ LL | const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) };
9393
}
9494

9595
error[E0080]: it is undefined behavior to use this value
96-
--> $DIR/ub-ref-ptr.rs:45:1
96+
--> $DIR/ub-ref-ptr.rs:48:1
9797
|
9898
LL | const USIZE_AS_BOX: Box<u8> = unsafe { mem::transmute(1337usize) };
9999
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a dangling box (0x539[noalloc] has no provenance)
@@ -104,13 +104,13 @@ LL | const USIZE_AS_BOX: Box<u8> = unsafe { mem::transmute(1337usize) };
104104
}
105105

106106
error[E0080]: evaluation of constant value failed
107-
--> $DIR/ub-ref-ptr.rs:48:41
107+
--> $DIR/ub-ref-ptr.rs:52:41
108108
|
109109
LL | const UNINIT_PTR: *const i32 = unsafe { MaybeUninit { uninit: () }.init };
110110
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
111111

112112
error[E0080]: it is undefined behavior to use this value
113-
--> $DIR/ub-ref-ptr.rs:52:1
113+
--> $DIR/ub-ref-ptr.rs:56:1
114114
|
115115
LL | const NULL_FN_PTR: fn() = unsafe { mem::transmute(0usize) };
116116
| ^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered null pointer, but expected a function pointer
@@ -121,13 +121,13 @@ LL | const NULL_FN_PTR: fn() = unsafe { mem::transmute(0usize) };
121121
}
122122

123123
error[E0080]: evaluation of constant value failed
124-
--> $DIR/ub-ref-ptr.rs:54:38
124+
--> $DIR/ub-ref-ptr.rs:59:38
125125
|
126126
LL | const UNINIT_FN_PTR: fn() = unsafe { MaybeUninit { uninit: () }.init };
127127
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
128128

129129
error[E0080]: it is undefined behavior to use this value
130-
--> $DIR/ub-ref-ptr.rs:57:1
130+
--> $DIR/ub-ref-ptr.rs:62:1
131131
|
132132
LL | const DANGLING_FN_PTR: fn() = unsafe { mem::transmute(13usize) };
133133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0xd[noalloc], but expected a function pointer
@@ -138,7 +138,7 @@ LL | const DANGLING_FN_PTR: fn() = unsafe { mem::transmute(13usize) };
138138
}
139139

140140
error[E0080]: it is undefined behavior to use this value
141-
--> $DIR/ub-ref-ptr.rs:59:1
141+
--> $DIR/ub-ref-ptr.rs:65:1
142142
|
143143
LL | const DATA_FN_PTR: fn() = unsafe { mem::transmute(&13) };
144144
| ^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered ALLOC2<imm>, but expected a function pointer
@@ -158,13 +158,13 @@ note: inside `std::ptr::read::<u32>`
158158
note: inside `std::ptr::const_ptr::<impl *const u32>::read`
159159
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
160160
note: inside `UNALIGNED_READ`
161-
--> $DIR/ub-ref-ptr.rs:66:5
161+
--> $DIR/ub-ref-ptr.rs:73:5
162162
|
163163
LL | ptr.read();
164164
| ^^^^^^^^^^
165165

166166
error[E0080]: it is undefined behavior to use this value
167-
--> $DIR/ub-ref-ptr.rs:69:1
167+
--> $DIR/ub-ref-ptr.rs:76:1
168168
|
169169
LL | const POINTS_TO_FUNCTION: &() = unsafe {
170170
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a reference pointing to a function
@@ -175,7 +175,7 @@ LL | const POINTS_TO_FUNCTION: &() = unsafe {
175175
}
176176

177177
error[E0080]: it is undefined behavior to use this value
178-
--> $DIR/ub-ref-ptr.rs:74:1
178+
--> $DIR/ub-ref-ptr.rs:81:1
179179
|
180180
LL | const POINTS_TO_VTABLE: (&(), &()) = unsafe {
181181
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .1: encountered a reference pointing to a vtable

0 commit comments

Comments
 (0)