Skip to content

Unboxed closures: ref-prefixed closures still capture by value #16814

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
netvl opened this issue Aug 28, 2014 · 1 comment
Closed

Unboxed closures: ref-prefixed closures still capture by value #16814

netvl opened this issue Aug 28, 2014 · 1 comment
Labels
A-closures Area: Closures (`|…| { … }`)

Comments

@netvl
Copy link
Contributor

netvl commented Aug 28, 2014

This code:

#![feature(unboxed_closures)]
#![feature(overloaded_calls)]

use std::cell::Cell;

fn main() {
    let test = Cell::new(5i);

    let do_something = ref |:| { println!("{}", test.get()) };

    test.set(6i);
    do_something();
    println!("{}", test.get());
}

Erroneously prints

5
6

This piece of code, which I though should be exact equivalent to the former one, works correctly, however:

#![feature(unboxed_closures)]
#![feature(overloaded_calls)]

use std::cell::Cell;

fn main() {
    let test = Cell::new(5i);

    let test_ref = &test;
    let do_something = |:| { println!("{}", test_ref.get()) };

    test.set(6i);
    do_something();
    println!("{}", test.get());
}

It prints

6
6

Looks like it copies Cell into the closure instead of taking it by reference.

Original question is on Stackoverflow

@aturon aturon mentioned this issue Oct 16, 2014
47 tasks
@bkoropoff
Copy link
Contributor

This was fixed and a test enabled for it in #17731 (which was actually merged despite github not indicating so).

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-closures Area: Closures (`|…| { … }`)
Projects
None yet
Development

No branches or pull requests

4 participants