//@revisions: stack tree
//@[tree]compile-flags: -Zmiri-tree-borrows
use std::cell::UnsafeCell;
#[repr(C)]
#[derive(Default)]
struct Node {
_meta: UnsafeCell<usize>,
value: usize,
}
impl Node {
fn value(&self) -> &usize {
&self.value
}
}
/// This used to cause Stacked Borrows errors because of trouble around conversion
/// from Box to raw pointer.
fn main() {
unsafe {
let a = Box::into_raw(Box::new(Node::default()));
let ptr = &*a;
*UnsafeCell::raw_get(a.cast::<UnsafeCell<usize>>()) = 2;
assert_eq!(*ptr.value(), 0);
drop(Box::from_raw(a));
}
}
I saw this happen:
warning: `Box::new(_)` of default value
--> ./src/tools/miri/tests/pass/issues/issue-miri-3473.rs:22:31
|
22 | let a = Box::into_raw(Box::new(Node::default()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::default()`
|
it does not build;
error[E0282]: type annotations needed for `*mut _`
--> ./src/tools/miri/tests/pass/issues/issue-miri-3473.rs:22:13
|
22 | let a = Box::into_raw(Box::default());
| ^
...
25 | assert_eq!(*ptr.value(), 0);
| ----- type must be known at this point
|
help: consider giving `a` an explicit type, where the type for type parameter `T` is specified
|
22 | let a: *mut T = Box::into_raw(Box::default());
| ++++++++
error: aborting due to 1 previous error; 1 warning emitted
Summary
.
Lint Name
box_default
Reproducer
I tried this code:
I saw this happen:
it does not build;
Version
Additional Labels
No response