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