Skip to content

Struct + Fn fulfills Send, but sugary version |&: ...| does not #16560

Closed
@japaric

Description

@japaric

STR

#![feature(unboxed_closures)]

use std::mem;
use std::ops::Fn;

// Unsugared closure
struct Closure(u8);

impl Fn<(u8,), u8> for Closure {
    extern "rust-call" fn call(&self, (y,): (u8,)) -> u8 {
        let &Closure(x) = self;

        x + y
    }
}

fn main() {
    let y = 0u8;
    let closure = |&: x| y + x;
    let unsugared_closure = Closure(y);

    // Check that both closures are capturing by value
    println!("{}", mem::size_of_val(&closure));  // prints 1
    println!("{}", mem::size_of_val(&unsugared_closure));  // prints 1

    spawn(proc() {
        let ok = unsugared_closure;
        let err = closure;
    })
}

Output

closure.rs:28:19: 28:26 error: cannot capture variable of type `closure`, which does not fulfill `'static+Send`, in a bounded closure [E0146]
closure.rs:28         let err = closure;
                                ^~~~~~~
closure.rs:28:19: 28:26 note: this closure's environment must satisfy `'static+Send`
closure.rs:28         let err = closure;
                                ^~~~~~~
error: aborting due to previous error

Version

rustc 0.12.0-pre (eff87bc9d 2014-08-17 13:11:06 +0000)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions