-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
stack allocated vecs can be returned from functions #3243
Comments
Reproduced as of d2ad028 |
Hm. I think this is just returning the slice, which is ok, no? The lifetime is static, and it outlives the caller lifetime as inferred.. |
@graydon Well, that example is a bit unfortunate but the following compiles too:
and prints out rubbish for me. |
That's also fine. |
Oh. I guess if it prints out rubbish it's probably compiling as non-static. Yeah, that's bad. |
Stack arrays should be returned by-value (so through an output parameter). They can't be static because they're mutable. |
Reproduced as of c202430 Here is a slightly elaborated version of fawek's example that I used to better understand the scenario: const some_array : &'static [int] = &'static [1,2,3];
fn main() {
io::println("Direct");
for [1, 2, 3].each |i| {
io::println(fmt!("%d", *i));
}
io::println("Indirect global");
for (|| &some_array)().each |i| {
io::println(fmt!("%d", *i));
}
io::println("Indirect local");
for (|| &[1, 2, 3])().each |i| {
io::println(fmt!("%d", *i));
}
} On my machine, it prints:
|
mhm. definitely disappointing and worth fixing (keeping those importance labels) but I don't think it's a showstopper for 0.6. removing milestone. |
Still reproducible,, thought I get slightly different output. Nominating for production ready. |
accepted for production-ready milestone |
This is a pretty nasty and subtle bug. Just spent a while trying to figure out how the heck I was getting |
@kballard yes, you're right. I'll take a look, I suspect the fix is fairly straightforward. |
accepted for P-backcompat-lang. |
This was fixed as part of #3511. However, no test case. I'll add one. |
…r=nikomatsakis (Lifetime of stack allocated vectors was not being enforced) Closes #3243.
See this gist: https://gist.github.com/3419521, with the attached llvm bitcode. Rust compiles it fine, but looking through the bitcode, it appears that rust is returning a stack pointer.
The text was updated successfully, but these errors were encountered: