Skip to content

Type inference for closure gets confused without trivial help #27828

Closed
@frankmcsherry

Description

@frankmcsherry

The following falls over at the moment, but works with the commented version of the closure:

fn main() {
    let f = |&x| x as u64;         // <-- doesn't work
    // let f = |&x: &_| x as u64;  // <-- does work
    test_me0(&f);
    test_me1(&f);
}

fn test_me0<F: Fn(&u32) -> u64>(func: &F) -> u64 { func(&0) }
fn test_me1<F: Fn(&u32) -> u64>(func: &F) -> u64 { func(&1) }

@eddyb's thinking is that a &x pattern may not result in a &T type hint, same for &mut x. His thinking was that way back when &x would also match &mut which is no longer possible, but attempts to get playbot to verify this for us in public did not go as planned.

In any case, it was there determined that &x: _ and &x: &_ should be identical, and that this is not expected. :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-type-systemArea: Type system

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions