Skip to content

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

Closed
@Munksgaard

Description

@Munksgaard

����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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsA-type-systemArea: Type system

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions