Skip to content
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

Closed
erickt opened this issue Aug 21, 2012 · 14 comments · Fixed by #11889
Closed

stack allocated vecs can be returned from functions #3243

erickt opened this issue Aug 21, 2012 · 14 comments · Fixed by #11889
Assignees
Labels
A-lifetimes Area: lifetime related I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.
Milestone

Comments

@erickt
Copy link
Contributor

erickt commented Aug 21, 2012

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.

@ghost ghost assigned nikomatsakis Aug 21, 2012
@catamorphism
Copy link
Contributor

Reproduced as of d2ad028

@graydon
Copy link
Contributor

graydon commented Dec 11, 2012

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..

@ghost
Copy link

ghost commented Dec 12, 2012

@graydon Well, that example is a bit unfortunate but the following compiles too:

fn main() {
    for (|| &[1, 2, 3])().each |i| {
        io::println(fmt!("%d", *i));
    }
}

and prints out rubbish for me.

@graydon
Copy link
Contributor

graydon commented Dec 13, 2012

That's also fine. &[1,2,3] is a slice to a static array (i.e. with unlimited lifetime). Or .. it should be!

@graydon
Copy link
Contributor

graydon commented Dec 13, 2012

Oh. I guess if it prints out rubbish it's probably compiling as non-static. Yeah, that's bad.

@thestinger
Copy link
Contributor

Stack arrays should be returned by-value (so through an output parameter). They can't be static because they're mutable.

@pnkfelix
Copy link
Member

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:

Direct
1
2
3
Indirect global
1
2
3
Indirect local
24
140552821560312
24

@graydon
Copy link
Contributor

graydon commented Mar 26, 2013

mhm. definitely disappointing and worth fixing (keeping those importance labels) but I don't think it's a showstopper for 0.6. removing milestone.

@metajack
Copy link
Contributor

Still reproducible,, thought I get slightly different output.

Nominating for production ready.

@graydon
Copy link
Contributor

graydon commented Jun 20, 2013

accepted for production-ready milestone

@lilyball
Copy link
Contributor

This is a pretty nasty and subtle bug. Just spent a while trying to figure out how the heck I was getting [0] back, and finally tracked it down to returning the results of bytes!(".") and having that transform into &[0] behind my back. Given how easy it is to accidentally trigger (especially with bytes!()) I think it should be a bit higher priority.

@nikomatsakis
Copy link
Contributor

@kballard yes, you're right. I'll take a look, I suspect the fix is fairly straightforward.

@pnkfelix
Copy link
Member

accepted for P-backcompat-lang.

@nikomatsakis
Copy link
Contributor

This was fixed as part of #3511. However, no test case. I'll add one.

bors added a commit that referenced this issue Jan 29, 2014
…r=nikomatsakis

(Lifetime of stack allocated vectors was not being enforced)

Closes #3243.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: lifetime related I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants