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

str.word_iter().collect() allows slices to escape the borrowck #7713

Closed
huonw opened this issue Jul 11, 2013 · 6 comments
Closed

str.word_iter().collect() allows slices to escape the borrowck #7713

huonw opened this issue Jul 11, 2013 · 6 comments
Labels
A-lifetimes Area: Lifetimes / regions

Comments

@huonw
Copy link
Member

huonw commented Jul 11, 2013

fn main() {
    let v = {
        let s = ~"foo bar";
        s.word_iter().collect::<~[&str]>()
    };
    println(v.to_str());
}
// prints [foo, , ^@]

Changing the v.to_str() to fmt!("%?", v) still compiles and outputs

rust: task failed at 'left: 0 does not equal right: 128', /home/huon/rust/src/libstd/str.rs:1727
rust: domain main @0x25ba1b0 root task failed
@nikomatsakis
Copy link
Contributor

Investigating.

@nikomatsakis
Copy link
Contributor

I believe this is due to #5781

@huonw
Copy link
Member Author

huonw commented Aug 7, 2013

This appears to be fixed:

<anon>:5:28: 5:29 error: borrowed value does not live long enough
<anon>:5         let s = ~"foo bar"; s.word_iter().collect::<~[&str]>()
                                     ^
<anon>:3:10: 8:1 note: borrowed pointer must be valid for the block at 3:10...
<anon>:3 fn main() {
<anon>:4     let r = {
<anon>:5         let s = ~"foo bar"; s.word_iter().collect::<~[&str]>()
<anon>:6     };
<anon>:7     println(fmt!("%?", r))
<anon>:8 }
<anon>:4:12: 6:5 note: ...but borrowed value is only valid for the block at 4:12
<anon>:4     let r = {
<anon>:5         let s = ~"foo bar"; s.word_iter().collect::<~[&str]>()
<anon>:6     };
error: aborting due to previous error
application terminated with error code 101

But I'm not sure why: @nikomatsakis do you know of anything that would've fixed this?

@emberian
Copy link
Member

emberian commented Nov 4, 2013

This does compile now, and outputs [[oo, , r]

@nikomatsakis
Copy link
Contributor

I still suspect the error is #5781. If I find the time, I'll probe a bit more deeply this week.

@alexcrichton
Copy link
Member

Closing, this compiles with a correct error now:

fn main() {
    let v = {
        let s = "foo bar".to_string();
        s.as_slice().words().collect::<Vec<&str>>()
    };
    println!("{}", v.to_string());
}

Additionally, the cc'd issue #5781, is closed, so I believe the relevant tests are all checked in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions
Projects
None yet
Development

No branches or pull requests

4 participants