-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0ee7cb5
commit 5d9b908
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
tests/ui/consts/const-eval/ptr-to-int-transmute-in-consts-issue-87525.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const fn foo(ptr: *const u8) -> usize { | ||
unsafe { | ||
std::mem::transmute(ptr) | ||
//~^ ERROR pointers cannot be transmuted to integers | ||
} | ||
} | ||
|
||
trait Human { | ||
const ID: u64 = { | ||
let value = 10; | ||
let ptr: *const i32 = &value; | ||
unsafe { | ||
std::mem::transmute(ptr) | ||
//~^ ERROR pointers cannot be transmuted to integers | ||
} | ||
}; | ||
|
||
fn id_plus_one() -> u64 { | ||
Self::ID + 1 | ||
} | ||
} | ||
|
||
struct Type<T>(T); | ||
|
||
impl<T> Type<T> { | ||
const ID: u64 = { | ||
let value = 10; | ||
let ptr: *const i32 = &value; | ||
unsafe { | ||
std::mem::transmute(ptr) | ||
//~^ ERROR pointers cannot be transmuted to integers | ||
} | ||
}; | ||
|
||
fn id_plus_one() -> u64 { | ||
Self::ID + 1 | ||
} | ||
} | ||
|
||
fn control(ptr: *const u8) -> usize { | ||
unsafe { | ||
std::mem::transmute(ptr) | ||
} | ||
} | ||
|
||
struct ControlStruct; | ||
|
||
impl ControlStruct { | ||
fn new() -> usize { | ||
let value = 10; | ||
let ptr: *const i32 = &value; | ||
unsafe { | ||
std::mem::transmute(ptr) | ||
} | ||
} | ||
} | ||
|
||
|
||
const fn zoom(ptr: *const u8) -> usize { | ||
unsafe { | ||
std::mem::transmute(ptr) | ||
//~^ ERROR pointers cannot be transmuted to integers | ||
} | ||
} | ||
|
||
fn main() { | ||
const a: u8 = 10; | ||
const value: usize = zoom(&a); | ||
//~^ ERROR evaluation of constant value failed | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/ui/consts/const-eval/ptr-to-int-transmute-in-consts-issue-87525.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/ptr-to-int-transmute-in-consts-issue-87525.rs:68:26 | ||
| | ||
LL | const value: usize = zoom(&a); | ||
| ^^^^^^^^ unable to turn pointer into integer | ||
| | ||
= help: this code performed an operation that depends on the underlying bytes representing a pointer | ||
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0080`. |