2022-04-21 09:09:35 -05:00
|
|
|
// compile-flags: -Zmiri-tag-raw-pointers
|
2019-04-17 07:57:13 -05:00
|
|
|
|
2018-11-28 02:33:33 -06:00
|
|
|
trait S: Sized {
|
|
|
|
fn tpb(&mut self, _s: Self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl S for i32 {}
|
|
|
|
|
|
|
|
fn two_phase1() {
|
|
|
|
let mut x = 3;
|
|
|
|
x.tpb(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn two_phase2() {
|
|
|
|
let mut v = vec![];
|
|
|
|
v.push(v.len());
|
|
|
|
}
|
|
|
|
|
2019-01-04 03:15:53 -06:00
|
|
|
fn two_phase3(b: bool) {
|
|
|
|
let mut x = &mut vec![];
|
|
|
|
let mut y = vec![];
|
|
|
|
x.push((
|
|
|
|
{
|
2022-06-20 18:00:37 -05:00
|
|
|
if b {
|
2022-06-20 18:08:00 -05:00
|
|
|
x = &mut y;
|
|
|
|
}
|
2019-01-04 03:15:53 -06:00
|
|
|
22
|
|
|
|
},
|
|
|
|
x.len(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2019-04-17 01:25:21 -05:00
|
|
|
#[allow(unreachable_code)]
|
|
|
|
fn two_phase_raw() {
|
|
|
|
let x: &mut Vec<i32> = &mut vec![];
|
2022-06-20 18:00:37 -05:00
|
|
|
x.push({
|
|
|
|
// Unfortunately this does not trigger the problem of creating a
|
|
|
|
// raw ponter from a pointer that had a two-phase borrow derived from
|
|
|
|
// it because of the implicit &mut reborrow.
|
|
|
|
let raw = x as *mut _;
|
|
|
|
unsafe {
|
|
|
|
*raw = vec![1];
|
2019-04-17 01:25:21 -05:00
|
|
|
}
|
2022-06-20 18:00:37 -05:00
|
|
|
return;
|
|
|
|
});
|
2019-04-17 01:25:21 -05:00
|
|
|
}
|
|
|
|
|
2018-11-28 02:33:33 -06:00
|
|
|
fn two_phase_overlapping1() {
|
|
|
|
let mut x = vec![];
|
|
|
|
let p = &x;
|
|
|
|
x.push(p.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn two_phase_overlapping2() {
|
|
|
|
use std::ops::AddAssign;
|
|
|
|
let mut x = 1;
|
|
|
|
let l = &x;
|
|
|
|
x.add_assign(x + *l);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_interior_mutability() {
|
|
|
|
use std::cell::Cell;
|
|
|
|
|
|
|
|
trait Thing: Sized {
|
|
|
|
fn do_the_thing(&mut self, _s: i32) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Thing for Cell<T> {}
|
|
|
|
|
|
|
|
let mut x = Cell::new(1);
|
2019-04-17 07:57:13 -05:00
|
|
|
let l = &x;
|
2019-04-05 04:00:44 -05:00
|
|
|
|
2022-06-20 18:00:37 -05:00
|
|
|
x.do_the_thing({
|
|
|
|
x.set(3);
|
|
|
|
l.set(4);
|
|
|
|
x.get() + l.get()
|
|
|
|
});
|
2018-11-28 02:33:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
two_phase1();
|
|
|
|
two_phase2();
|
2019-01-04 03:15:53 -06:00
|
|
|
two_phase3(false);
|
|
|
|
two_phase3(true);
|
2019-04-17 01:25:21 -05:00
|
|
|
two_phase_raw();
|
2018-11-28 02:33:33 -06:00
|
|
|
with_interior_mutability();
|
2019-04-17 07:57:13 -05:00
|
|
|
two_phase_overlapping1();
|
|
|
|
two_phase_overlapping2();
|
2018-11-28 02:33:33 -06:00
|
|
|
}
|