Comments (9)
from linked-list.
Here's a simpler test case:
let mut m = LinkedList::new();
m.push_back(2);
m.push_back(3);
assert_eq!(m.pop_front(), Some(2));
which fails with:
error[E0080]: constant evaluation error: no item to reborrow for Unique from tag 105256 found in borrow stack
--> /Users/jrmuizel/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore/mem.rs:1329:34
|
1329 | ManuallyDrop::into_inner(self.value)
| ^^^^^^^^^^ no item to reborrow for Unique from tag 105256 found in borrow stack
|
= note: inside call to `std::mem::MaybeUninit::<std::option::Option<std::boxed::Box<Node<i32>>>>::assume_init` at /Users/jrmuizel/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore/ptr.rs:576:5
= note: inside call to `std::ptr::read::<std::option::Option<std::boxed::Box<Node<i32>>>>` at /Users/jrmuizel/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore/ptr.rs:359:17
= note: inside call to `std::ptr::swap_nonoverlapping_one::<std::option::Option<std::boxed::Box<Node<i32>>>>` at /Users/jrmuizel/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore/mem.rs:651:9
= note: inside call to `std::mem::swap::<std::option::Option<std::boxed::Box<Node<i32>>>>` at /Users/jrmuizel/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore/mem.rs:709:5
= note: inside call to `std::mem::replace::<std::option::Option<std::boxed::Box<Node<i32>>>>` at /Users/jrmuizel/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore/option.rs:850:9
note: inside call to `std::option::Option::<std::boxed::Box<Node<i32>>>::take` at src/lib.rs:204:9
--> src/lib.rs:204:9
|
204 | self.head.take().map(|mut head| {
| ^^^^^^^^^^^^^^^^
note: inside call to `LinkedList::<i32>::pop_front` at src/lib.rs:877:20
--> src/lib.rs:877:20
|
877 | assert_eq!(m.pop_front(), Some(2));
| ^^^^^^^^^^^^^
note: inside call to `tests::test_basic` at src/lib.rs:873:5
--> src/lib.rs:873:5
|
873 | / fn test_basic() {
874 | | let mut m = LinkedList::new();
875 | | m.push_back(2);
876 | | m.push_back(3);
... |
903 | | assert_eq!(n.pop_front(), Some(1));*/
904 | | }
| |_____^
from linked-list.
A further simplification:
let ptr: *mut Node<_>;
let mut head;
{
let mut node = Box::new(Node::new(2));
// unconditionally make the new node the new tail
ptr = &mut *node;
head = Some(node);
}
{
let mut node = Box::new(Node::new(3));
let tail = unsafe { &mut *(ptr as *mut Node<_>) };
node.prev = Raw::some(tail);
tail.next = Some(node);
}
head.take();
from linked-list.
And here's the essence of what's going on:
let mut node = Box::new(Node::new(2));
let mut n2 = Box::new(Node::new(3));
let ptr: *mut Node<_> = &mut *node;
let tail = unsafe { &mut *(ptr as *mut Node<_>) };
let mut head = Some(node);
tail.next = Some(n2);
If we swap the order of let mut head = Some(node);
and tail.next = Some(n2);
everything is fine.
It's not clear to me why miri's is complaining about this:
error[E0080]: constant evaluation error: no item to reborrow for Unique from tag 105226 found in borrow stack
--> /Users/jrmuizel/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore/ptr.rs:193:1
|
193 | / unsafe fn real_drop_in_place<T: ?Sized>(to_drop: &mut T) {
194 | | // Code here does not matter - this is replaced by the
195 | | // real drop glue by the compiler.
196 | | real_drop_in_place(to_drop)
197 | | }
| |_^ no item to reborrow for Unique from tag 105226 found in borrow stack
|
note: inside call to `std::ptr::real_drop_in_place::<std::option::Option<std::boxed::Box<Node<i32>>>> - shim(Some(std::option::Option<std::boxed::Box<Node<i32>>>))` at src/lib.rs:888:9
--> src/lib.rs:888:9
|
888 | tail.next = Some(n2);
| ^^^^^^^^^
note: inside call to `tests::test_basic` at src/lib.rs:880:5
from linked-list.
Can you make this into a self-contained example for the playground? That'd make analysis much easier for me.
from linked-list.
Yep. Will do.
from linked-list.
@RalfJung https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f4e643db9511b26a96692b5a6869c853
from linked-list.
node
is a unique pointer (a Box
). When you use it, you assert that there are no aliases that point to the same memory. If any aliases exist, they become invalid at this point.
In your program, tail
is such an alias -- and hence becomes invalid.
from linked-list.
It seems like the use of Box in this crate basically needs to go. It looks like this happened in std's LinkedList in rust-lang/rust#34608
from linked-list.
Related Issues (7)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from linked-list.