2023-10-16 12:36:39 -05:00
|
|
|
// skip-filecheck
|
2024-04-20 06:19:34 -05:00
|
|
|
//@ test-mir-pass: AddRetag
|
2023-06-08 02:18:34 -05:00
|
|
|
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
|
2018-10-24 06:38:09 -05:00
|
|
|
// ignore-tidy-linelength
|
|
|
|
//@ compile-flags: -Z mir-emit-retag -Z mir-opt-level=0 -Z span_free_formats
|
|
|
|
|
|
|
|
#![allow(unused)]
|
|
|
|
|
|
|
|
struct Test(i32);
|
|
|
|
|
2024-03-17 13:36:55 -05:00
|
|
|
// EMIT_MIR retag.{impl#0}-foo.SimplifyCfg-pre-optimizations.after.mir
|
|
|
|
// EMIT_MIR retag.{impl#0}-foo_shr.SimplifyCfg-pre-optimizations.after.mir
|
2018-10-24 06:38:09 -05:00
|
|
|
impl Test {
|
|
|
|
// Make sure we run the pass on a method, not just on bare functions.
|
2020-03-19 06:40:38 -05:00
|
|
|
fn foo<'x>(&self, x: &'x mut i32) -> &'x mut i32 {
|
|
|
|
x
|
|
|
|
}
|
|
|
|
fn foo_shr<'x>(&self, x: &'x i32) -> &'x i32 {
|
|
|
|
x
|
|
|
|
}
|
2018-10-24 06:38:09 -05:00
|
|
|
}
|
|
|
|
|
2020-07-27 14:22:43 -05:00
|
|
|
// EMIT_MIR core.ptr-drop_in_place.Test.SimplifyCfg-make_shim.after.mir
|
2020-04-02 16:09:01 -05:00
|
|
|
|
2019-08-05 15:27:00 -05:00
|
|
|
impl Drop for Test {
|
|
|
|
fn drop(&mut self) {}
|
|
|
|
}
|
|
|
|
|
2024-03-17 13:36:55 -05:00
|
|
|
// EMIT_MIR retag.main.SimplifyCfg-pre-optimizations.after.mir
|
|
|
|
// EMIT_MIR retag.main-{closure#0}.SimplifyCfg-pre-optimizations.after.mir
|
2024-01-30 21:28:28 -06:00
|
|
|
pub fn main() {
|
2018-10-24 06:38:09 -05:00
|
|
|
let mut x = 0;
|
|
|
|
{
|
|
|
|
let v = Test(0).foo(&mut x); // just making sure we do not panic when there is a tuple struct ctor
|
|
|
|
let w = { v }; // assignment
|
2018-11-06 04:04:10 -06:00
|
|
|
let w = w; // reborrow
|
|
|
|
// escape-to-raw (mut)
|
|
|
|
let _w = w as *mut _;
|
2018-10-24 06:38:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Also test closures
|
2020-03-19 06:40:38 -05:00
|
|
|
let c: fn(&i32) -> &i32 = |x: &i32| -> &i32 {
|
|
|
|
let _y = x;
|
|
|
|
x
|
|
|
|
};
|
2018-10-24 06:38:09 -05:00
|
|
|
let _w = c(&x);
|
|
|
|
|
|
|
|
// need to call `foo_shr` or it doesn't even get generated
|
|
|
|
Test(0).foo_shr(&0);
|
2018-11-06 04:04:10 -06:00
|
|
|
|
|
|
|
// escape-to-raw (shr)
|
|
|
|
let _w = _w as *const _;
|
2021-05-07 02:36:45 -05:00
|
|
|
|
|
|
|
array_casts();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Casting directly to an array should also go through `&raw` and thus add appropriate retags.
|
2024-03-17 13:36:55 -05:00
|
|
|
// EMIT_MIR retag.array_casts.SimplifyCfg-pre-optimizations.after.mir
|
2021-05-07 02:36:45 -05:00
|
|
|
fn array_casts() {
|
|
|
|
let mut x: [usize; 2] = [0, 0];
|
|
|
|
let p = &mut x as *mut usize;
|
2024-06-02 19:18:33 -05:00
|
|
|
unsafe {
|
|
|
|
*p.add(1) = 1;
|
|
|
|
}
|
2021-05-07 02:36:45 -05:00
|
|
|
|
|
|
|
let x: [usize; 2] = [0, 1];
|
|
|
|
let p = &x as *const usize;
|
|
|
|
assert_eq!(unsafe { *p.add(1) }, 1);
|
2018-10-24 06:38:09 -05:00
|
|
|
}
|
2024-03-17 11:02:38 -05:00
|
|
|
|
|
|
|
// EMIT_MIR retag.box_to_raw_mut.SimplifyCfg-pre-optimizations.after.mir
|
|
|
|
fn box_to_raw_mut(x: &mut Box<i32>) -> *mut i32 {
|
|
|
|
std::ptr::addr_of_mut!(**x)
|
|
|
|
}
|