Closed uselessgoddess closed 1 year ago
See also #164 and #168.
The problem is that this code shouldn't compile in the first place. I think the solution is to add a lifetime parameter to IntoIter
(and all the other consuming iterators).
I think it would be interesting if it were possible to return an IntoIter
that would extend the lifetime of the Bump
Fixed in #181
@uselessgoddess hi I felt quite confused about this issue. It seem that in the implementation of into_iter
, it already prevented bump
from being dropped by mem::forget()
; however, why is it still dropped in foo()
?
@shinmao In fact, it is not dropped because of the .into_iter()
call. At the end of any scope, local objects are always being dropped. The foo
function actually illustrated the undefined behavior resulting from an incorrect implementation of IntoIterator
s lifetimes. Now this function should not compile, the issue has been closed
Fix missing lifetimes PR: https://github.com/fitzgen/bumpalo/pull/181
fn foo() -> impl Iterator<Item = usize> {
let arena = Bump::new();
let v = vec![in &arena; 1, 2, 3];
v.into_iter()
// <-- bump drop here and free our memory
// but ptrs in `IntoIter` still point to this memory
}
@uselessgoddess thanks for the response. Does it mean v
is forgot by into_iter()
here, but bump
is still dropped? If we insert a line of mem::forget(arena)
after v
, could it also solve the problem?
I don't know how to study it properly, I recommend studying moving and Drop trait. Also try running this code with the new version of bumpalo
, I hope the compilation error will let you figure it out.
Miri humiliate the following code:
Why is that? Burrow into bumpalo
into_iter
implementations.