Skip to content

Commit 0b9913c

Browse files
committed
Add regression test
1 parent 06cafcb commit 0b9913c

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn foo() {}
2+
3+
const _: &usize = unsafe { &std::mem::transmute(foo as fn()) };
4+
//~^ ERROR: constructing invalid value at .<deref>: encountered a pointer, but expected an integer
5+
6+
const _: usize = unsafe { std::mem::transmute(foo as fn()) };
7+
//~^ ERROR: unable to turn pointer into integer
8+
9+
fn main() {}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error[E0080]: constructing invalid value at .<deref>: encountered a pointer, but expected an integer
2+
--> $DIR/promotion-in-const-validation.rs:3:1
3+
|
4+
LL | const _: &usize = unsafe { &std::mem::transmute(foo as fn()) };
5+
| ^^^^^^^^^^^^^^^ it is undefined behavior to use this value
6+
|
7+
= help: this code performed an operation that depends on the underlying bytes representing a pointer
8+
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
9+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
10+
= note: the raw bytes of the constant (size: 8, align: 8) {
11+
╾ALLOC0<imm>╼ │ ╾──────╼
12+
}
13+
14+
note: erroneous constant encountered
15+
--> $DIR/promotion-in-const-validation.rs:3:28
16+
|
17+
LL | const _: &usize = unsafe { &std::mem::transmute(foo as fn()) };
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19+
20+
error[E0080]: unable to turn pointer into integer
21+
--> $DIR/promotion-in-const-validation.rs:6:1
22+
|
23+
LL | const _: usize = unsafe { std::mem::transmute(foo as fn()) };
24+
| ^^^^^^^^^^^^^^ evaluation of `_` failed here
25+
|
26+
= help: this code performed an operation that depends on the underlying bytes representing a pointer
27+
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
28+
29+
error: aborting due to 2 previous errors
30+
31+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)