Skip to content

Indirect inference for closure arguments fails depending on the contents of the closure #53244

@elinorbgr

Description

@elinorbgr

Minimal example I could create:

trait Callback<Args> {
    fn call(&mut self, args: Args);
}

impl<F, Args> Callback<Args> for F where F: FnMut(Args) {
    fn call(&mut self, args: Args) {
        self(args)
    }
}

#[derive(Debug)]
struct Wrapper<T>(T);

#[derive(Debug)]
enum Foo {
    Bar,
    Baz,
    Baq
}

fn use_cb<C: Callback<Wrapper<Foo>>>(mut c: C) {
    c.call(Wrapper(Foo::Baz))
}

fn main() {
    // this one works
    use_cb(|variant| {
        println!("the variant is: {:?}", variant);
    });
    // this one fails to infer
    use_cb(|variant| {
        println!("the variant is: {:?}", variant.0);
    });
    // with these annotations it works
    use_cb(|variant: Wrapper<_>| {
        println!("the variant is: {:?}", variant.0);
    });
}

note that the sole difference between the first and second closure is that the first prints variant while the other tries to access its inner field.

It reproduces on both stable and nightly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions