-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Drop allowed while active borrows still in scope #12223
Comments
What platform are you running this on? This code compiles and prints fn main() {
let foo = ~"hello";
let foo: ~[&str] = foo.words().collect();
let invalid_string = foo[0];
println!("{}", invalid_string);
} |
I hadn't actually tried in my own rustc, I created the minimized version on rusti: let foo = ~"hello"; let foo: ~[&str] = foo.words().collect(); foo[0] Inspired by an actual bug that I got after trying to read some stuff from a file. It seems what I posted originally does actually compile and work properly on my system as well, I guess I should have tested that... |
|
This one asserts (sometimes, it's random) for real on my system, most recent build of master (debian 64-bit, rustc 0.10-pre 58eeb07 2014-02-12 14:51:48 -0800) use std::io::stdio::{stdin};
fn main() {
let contents = stdin().read_to_str();
std::io::println(contents.clone().unwrap());
let contents: ~[&str] = contents.unwrap().lines().collect();
for row in contents.iter() {
println!("Row: {:?}",*row);
}
} Assuming the executable is named bug: run with
Otherwise it appears random, usually asserting:
|
Quite plausibly due to #5781, though I haven't dug in in detail. |
It seems the bug persists even without the shadowing so perhaps this is related to #10683, except for the 'match' part? |
It appears that we allow this code to compile: fn main() {
let a = ~"";
let b: ~[&str] = a.lines().collect();
drop(a);
for s in b.iter() {
println!("{}", *s);
}
} Nominating, that's bad. |
Any suggestions for a better name for this issue? The current name is misleading. Perhaps something along the lines of "Stale pointers can be created in safe code"? |
Updated title and description |
cc me |
cc @flaper87 |
Interestingly enough, the compiler rejects this code: fn main() {
let a = ~"";
let b = a.lines().to_owned_vec();
drop(a);
for s in b.iter() {
println!("{}", *s);
}
} It appears that the type ascription is throwing off the compiler? |
Another example taken from #12568 fn main() {
let arr : ~[&str] = std::os::args()[1].split_str("::").collect();
std::io::println("first " + arr[0]);
std::io::println("first again " + arr[0]);
} |
1.0, P-backcompat-lang |
The compiler correctly rejects: fn main() {
let a = ~"";
let b: Vec<&str> = a.lines().collect(); // note: borrow of `a[..]` occurs here
drop(a); // error: cannot move out of `a` because it is borrowed
} |
cc me |
#12828 has taken a major detour to fix other two more fundamental bugs first. On top of that fix, the root cause of this bug is also becoming clearer. Case 1: let b: ~[&str] = a.lines().to_owned_vec();
drop(a); Case 2: let b: ~[&str] = a.lines().collect();
drop(a); Case 3: let b: Vec<&str> = a.lines().collect();
drop(a); The difference here is that the RHS is directly assignable to LHS in case 1 but not in case 2 + 3 so But the variance inference adds another twist to case 3. The result of variance inference can be used to do type parameter substitution, which is not enabled yet. The net effect is two negatives become positive; the current So I guess the fix for this bug must wait for the PR 12828 to land. |
…crichton with overloaded calls. This enforces the mutability and borrow restrictions around overloaded calls. [breaking-change] Closes #12223. r? @alexcrichton
internal: Rename primeCaches config keys
Updated bug
The compiler allows this code when it shouldn't
Original description
The following code is buggy:
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 oldfoo
no longer exists,&str
cannot have a lifetime), or it should let the firstfoo
live until the end of the current scope, and make the above work.The text was updated successfully, but these errors were encountered: