]> git.proxmox.com Git - rustc.git/blob - src/test/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.rs
New upstream version 1.55.0+dfsg1
[rustc.git] / src / test / ui / consts / const-mut-refs / mut_ref_in_final_dynamic_check.rs
1 #![feature(const_mut_refs)]
2 #![feature(raw_ref_op)]
3 #![feature(const_raw_ptr_deref)]
4
5 // This file checks that our dynamic checks catch things that the static checks miss.
6 // We do not have static checks for these, because we do not look into function bodies.
7 // We treat all functions as not returning a mutable reference, because there is no way to
8 // do that without causing the borrow checker to complain (see the B4/helper test in
9 // mut_ref_in_final.rs).
10
11 const fn helper() -> Option<&'static mut i32> { unsafe {
12 // Undefined behaviour (integer as pointer), who doesn't love tests like this.
13 // This code never gets executed, because the static checks fail before that.
14 Some(&mut *(42 as *mut i32)) //~ ERROR evaluation of constant value failed
15 //~| 0x2a is not a valid pointer
16 } }
17 // The error is an evaluation error and not a validation error, so the error is reported
18 // directly at the site where it occurs.
19 const A: Option<&mut i32> = helper();
20
21 const fn helper2() -> Option<&'static mut i32> { unsafe {
22 // Undefined behaviour (dangling pointer), who doesn't love tests like this.
23 // This code never gets executed, because the static checks fail before that.
24 Some(&mut *(&mut 42 as *mut i32))
25 } }
26 const B: Option<&mut i32> = helper2(); //~ ERROR encountered dangling pointer in final constant
27
28 fn main() {}