]> git.lizzy.rs Git - rust.git/blob - src/test/mir-opt/retag.rs
Rollup merge of #99084 - RalfJung:write_bytes, r=thomcc
[rust.git] / src / test / mir-opt / retag.rs
1 // ignore-wasm32-bare compiled with panic=abort by default
2 // ignore-tidy-linelength
3 // compile-flags: -Z mir-emit-retag -Z mir-opt-level=0 -Z span_free_formats
4
5 #![allow(unused)]
6
7 struct Test(i32);
8
9 // EMIT_MIR retag.{impl#0}-foo.SimplifyCfg-elaborate-drops.after.mir
10 // EMIT_MIR retag.{impl#0}-foo_shr.SimplifyCfg-elaborate-drops.after.mir
11 impl Test {
12     // Make sure we run the pass on a method, not just on bare functions.
13     fn foo<'x>(&self, x: &'x mut i32) -> &'x mut i32 {
14         x
15     }
16     fn foo_shr<'x>(&self, x: &'x i32) -> &'x i32 {
17         x
18     }
19 }
20
21 // EMIT_MIR core.ptr-drop_in_place.Test.SimplifyCfg-make_shim.after.mir
22
23 impl Drop for Test {
24     fn drop(&mut self) {}
25 }
26
27 // EMIT_MIR retag.main.SimplifyCfg-elaborate-drops.after.mir
28 // EMIT_MIR retag.main-{closure#0}.SimplifyCfg-elaborate-drops.after.mir
29 fn main() {
30     let mut x = 0;
31     {
32         let v = Test(0).foo(&mut x); // just making sure we do not panic when there is a tuple struct ctor
33         let w = { v }; // assignment
34         let w = w; // reborrow
35         // escape-to-raw (mut)
36         let _w = w as *mut _;
37     }
38
39     // Also test closures
40     let c: fn(&i32) -> &i32 = |x: &i32| -> &i32 {
41         let _y = x;
42         x
43     };
44     let _w = c(&x);
45
46     // need to call `foo_shr` or it doesn't even get generated
47     Test(0).foo_shr(&0);
48
49     // escape-to-raw (shr)
50     let _w = _w as *const _;
51
52     array_casts();
53 }
54
55 /// Casting directly to an array should also go through `&raw` and thus add appropriate retags.
56 // EMIT_MIR retag.array_casts.SimplifyCfg-elaborate-drops.after.mir
57 fn array_casts() {
58     let mut x: [usize; 2] = [0, 0];
59     let p = &mut x as *mut usize;
60     unsafe { *p.add(1) = 1; }
61
62     let x: [usize; 2] = [0, 1];
63     let p = &x as *const usize;
64     assert_eq!(unsafe { *p.add(1) }, 1);
65 }