Skip to content

Boxed closures without explicit type info produces unhelpful error messages #20528

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

Closed
Munksgaard opened this issue Jan 4, 2015 · 1 comment
Closed
Labels
A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints A-type-system Area: Type system

Comments

@Munksgaard
Copy link
Contributor

����The following piece of code works fine:

#![feature(default_type_params)]

use std::thunk::Invoke;

fn foo(x: Box<for <'a>Invoke<&'a int, ()>+'static>) {
    x.invoke(&42)
}

fn main() {
    foo(box move |: _: &int| () )
}

However, if you leave out the explicit type information in the creation of the boxed closure, like this:

#![feature(default_type_params)]

use std::thunk::Invoke;

fn foo(x: Box<for <'a>Invoke<&'a int, ()>+'static>) {
    x.invoke(&42)
}

fn main() {
    foo(box move |: _| () )
}

you get this rather unhelpful error message:

$ rustc foo.rs
foo.rs:10:9: 10:28 error: type mismatch: the type `closure[foo.rs:10:18: 10:28]` implements the trait `core::ops::FnOn
ce(_)`, but the trait `for<'a> core::ops::FnOnce(&'a int)` is required (expected concrete lifetime, found bound lifeti
me parameter 'a)                                                                                                     
foo.rs:10     foo(box move |: _| () )
                  ^~~~~~~~~~~~~~~~~~~
foo.rs:10:9: 10:28 note: required for the cast to the object type `for<'a> std::thunk::Invoke<&'a int> + 'static`
foo.rs:10     foo(box move |: _| () )
                  ^~~~~~~~~~~~~~~~~~~
error: aborting due to previous error

Maybe it should say something along the lines of boxed closures require explicit type information, or be able to infer the type by itself.

@kmcallister kmcallister added A-type-system Area: Type system A-diagnostics Area: Messages for errors, warnings, and lints labels Jan 14, 2015
@nikomatsakis nikomatsakis added the A-closures Area: Closures (`|…| { … }`) label Jan 18, 2015
@steveklabnik
Copy link
Member

Traige: closures have changed, std::thunk is gone, and nobody has commented in over a year. @Munksgaard if you can still reproduce this, let me know, but I think it's a non-issue now, or at least, don't know how to reproduce it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

4 participants