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 coerce to FnBox with references in arguments #28222

Closed
ebfull opened this issue Sep 4, 2015 · 2 comments
Closed

Cannot coerce to FnBox with references in arguments #28222

ebfull opened this issue Sep 4, 2015 · 2 comments
Labels
A-type-system Area: Type system

Comments

@ebfull
Copy link
Contributor

ebfull commented Sep 4, 2015

It doesn't appear to be possible to return a FnBox which accepts references:

#![feature(fnbox)]

use std::boxed::FnBox;

type Callback = Box<FnBox(&mut usize, &usize)>;

fn get_cb() -> Callback {
    Box::new(|a, b| {})
}

fn main() { }

produces the following error on rustc 1.4.0-nightly (cd138dc44 2015-09-02)

<anon>:8:5: 10:7 error: type mismatch resolving `for<'r, 'r> <[closure@<anon>:8:14: 10:6] as alloc::boxed::FnBox<(&'r mut usize, &'r usize)>>::Output == ()`:
 expected bound lifetime parameter ,
    found concrete lifetime [E0271]
<anon>: 8     Box::new(|a, b| {
<anon>: 9         
<anon>:10     })
<anon>:8:5: 10:7 help: see the detailed explanation for E0271
<anon>:8:5: 10:7 note: required for the cast to the object type `for<'r, 'r> alloc::boxed::FnBox<(&'r mut usize, &'r usize), Output=()> + 'static`
<anon>: 8     Box::new(|a, b| {
<anon>: 9         
<anon>:10     })
<anon>:8:5: 10:7 error: type mismatch: the type `[closure@<anon>:8:14: 10:6]` implements the trait `core::ops::FnOnce<(_, _)>`, but the trait `for<'r, 'r> core::ops::FnOnce<(&'r mut usize, &'r usize)>` is required (expected concrete lifetime, found bound lifetime parameter ) [E0281]
<anon>: 8     Box::new(|a, b| {
<anon>: 9         
<anon>:10     })
<anon>:8:5: 10:7 help: see the detailed explanation for E0281
<anon>:8:5: 10:7 note: required for the cast to the object type `for<'r, 'r> alloc::boxed::FnBox<(&'r mut usize, &'r usize), Output=()> + 'static`
<anon>: 8     Box::new(|a, b| {
<anon>: 9         
<anon>:10     })
error: aborting due to 2 previous errors

#25647 is a similar bug when calling FnBox.

@eefriedman
Copy link
Contributor

This seems vaguely familiar... you can get around the error by annotating the types of the variables:

#![feature(fnbox)]
use std::boxed::FnBox;
type Callback = Box<FnBox(&mut usize, &usize)>;
fn get_cb() -> Callback {
    Box::new(|a: &mut _, b: &_| {})
}
fn main() { }

@arielb1
Copy link
Contributor

arielb1 commented Jan 10, 2016

Duplicate of #20841.

@arielb1 arielb1 closed this as completed Jan 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

4 participants