Skip to content

Type inference failing #125329

Open
Open

Description

I tried this code:

fn foo() -> Option<Box<u8>> {
    todo!()
}

fn bar(_: Option<&u8>) {}

fn main() {
    let mut v = vec![];
    bar(v.last().map(|x| &**x));
    v.push(foo());
}

I would expect v to have a knowable type but it's failing with this error:

error[E0282]: type annotations needed for `&T`
  --> src/main.rs:11:27
   |
11 |         bar(v.last().map(|x| &**x));
   |                           ^   --- type must be known at this point
   |
help: consider giving this closure parameter an explicit type, where the type for type parameter `T` is specified
   |
11 |         bar(v.last().map(|x: &T| &**x));
   |                            ++++

For more information about this error, try `rustc --explain E0282`.
error: could not compile `tests` (bin "tests") due to 1 previous error

It's able to infer it if we swap the lines

    v.push(foo());
    bar(v.last().map(|x| &**x));

Meta

Tried on nightly and stable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceArea: Type inferenceC-bugCategory: This is a bug.Category: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types 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