Skip to content

Expected type for closure arguments not inferred through two nested func calls #16473

Open
@nikomatsakis

Description

@nikomatsakis

Test case:

struct Foo;

struct FooClosure<'a>(Option<|&mut Foo|:'a>);

fn foo(closure: |&mut Foo, &int|:'static, inputs: &[int]) {
    for input in inputs.iter() {
        FooClosure(Some(|f| closure(f, input)));
    }
}

fn main() {}

This yields an error:

/home/nmatsakis/tmp/foo.rs:7:20: 7:47 error: mismatched types: expected `core::option::Option<|&mut Foo|>` but found `core::option::Option<|&mut Foo|>` (expected concrete lifetime, but found bound lifetime parameter )                                                                                                                  

which occurs because we did not propagate the expected type and thus instantiated the type of the closure parameter with a fresh variable, rather than using a bound region. Another solution to this issue would be finding a better way to decide when we can generalize lifetimes in closure parameters, rather than relying on the closure type.

There is a workaround: just manually annotate the parameter type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-inferenceArea: Type inferenceC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions