-
Couldn't load subscription status.
- Fork 13.9k
Description
Updated bug
The compiler allows this code when it shouldn't
fn main() {
let a = ~"";
let b: ~[&str] = a.lines().collect();
drop(a);
for s in b.iter() {
println!("{}", *s);
}
}Original description
The following code is buggy:
let foo = ~"hello";
let foo: ~[&str] = foo.words().collect();
let invalid_string = foo[0];Invalid string contains, as the name suggests, an invalid string; sometimes I get a string filled with \x00, sometimes just garbage, and usually it will eventually assert that the string contains invalid characters.
It appears that the first foo gets dropped when the second one is assigned?
Either the first foo is supposed to be dropped as happens now, and the borrow checker should forbid this code (since the old foo no longer exists, &str cannot have a lifetime), or it should let the first foo live until the end of the current scope, and make the above work.