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

Cannot call a Box<FnBox(_)> with a reference parameter #25647

Closed
KyleMayes opened this issue May 20, 2015 · 5 comments
Closed

Cannot call a Box<FnBox(_)> with a reference parameter #25647

KyleMayes opened this issue May 20, 2015 · 5 comments
Labels
A-closures Area: Closures (`|…| { … }`) C-bug Category: This is a bug.

Comments

@KyleMayes
Copy link
Contributor

#![feature(core)]

use std::boxed::{FnBox};

type Closure = Box<FnBox(i32)>;
type ClosureReference = Box<FnBox(&i32)>;

fn works(closure: Closure) {
    closure(42);
}

fn does_not_work(closure: ClosureReference) {
    closure(&42); // <- the error
}

fn main() {
    works(Box::new(|integer| println!("{}", integer)));

    does_not_work(Box::new(|integer: &i32| println!("{}", integer)));
}

Rust Playpen

Error

<anon>:13:5: 13:17 error: expected function, found `Box<for<'r> alloc::boxed::FnBox<(&'r i32,), Output=()>>`
<anon>:13     closure(&42); // <- the error
              ^~~~~~~~~~~~

On a semi-related note, the closure argument type is inferred for the Closure passed to works but not for the ClosureReference passed to does_not_work, but I think this is likely an extension of #24680.

Meta

rustc 1.2.0-nightly (c23a9d42e 2015-05-17) (built 2015-05-17)
binary: rustc
commit-hash: c23a9d42ea082830593a73d25821842baf9ccf33
commit-date: 2015-05-17
build-date: 2015-05-17
host: x86_64-pc-windows-gnu
release: 1.2.0-nightly
@Thiez
Copy link
Contributor

Thiez commented May 20, 2015

Strangely, the function does work if you change the body to

closure.call_box((&42,)); // <- no longer an error

Looks like the sugar confuses the compiler?

@KyleMayes KyleMayes changed the title Cannot call a Box<FnOnce(_)> with a reference parameter Cannot call a Box<FnBox(_)> with a reference parameter May 20, 2015
@steveklabnik steveklabnik added the A-closures Area: Closures (`|…| { … }`) label May 21, 2015
@abonander
Copy link
Contributor

This is still a problem in the 1.7 nightly.

@shepmaster
Copy link
Member

This is still a problem in the Rust 1.26 nightly.

@smcmurray
Copy link

This is still a problem in Rust 1.28 nightly.

@KyleMayes
Copy link
Contributor Author

It seems to work now.
Plus, FnBox is no longer needed in the latest stable version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

7 participants