Skip to content

Unhelpful "type annotations needed" when it seems like rustc should know the type #105029

Open
@keysmashes

Description

@keysmashes

I can't trivially reproduce this on the playground since I guess it depends on the funky DebounceEventHandler trait stuff that notify-debouncer-mini (which isn't on the playground) does; the following code is available at https://github.com/sersorrel/sd/tree/bad-rust-diagnostic.

Given the following code:

use std::{path::PathBuf, time::Duration};

#[derive(Debug)]
enum Event {
    NewScreenshot(PathBuf),
    Exit,
}

fn main() {
    notify_debouncer_mini::new_debouncer(Duration::from_millis(500), None, move |r| match r {
        Ok(events) => {
            for event in events {
                Event::NewScreenshot(event.path);
            }
        }
        Err(errors) => todo!(),
    })
    .unwrap();
}

The current output is:

$ cargo run
   Compiling sd v0.1.0 (/home/ash/src/sd)
error[E0282]: type annotations needed
  --> src/main.rs:15:38
   |
15 |                 Event::NewScreenshot(event.path);
   |                                      ^^^^^ cannot infer type

For more information about this error, try `rustc --explain E0282`.
error: could not compile `sd` due to previous error

Ideally the output should look like: well, I guess "something that tells me why rustc can't tell what the type is"*; my editor's (rust-analyzer-powered) inlay hints can certainly tell that r is a Result<Vec<DebouncedEvent>, …>, and that events and event are the obvious unwrappings of that, but through application of e.g. let x: () = r, it seems like rustc thinks that r is... a Result<_, _>??

This feels particularly weird because even if rustc doesn't already know, somehow, that event.path is a PathBuf, it should presumably be able to see that I'm trying to put it in a PathBuf-shaped hole (in Event::NewScreenshot)...

*alternatively "a suggested fix" would be ok, though without generalised type ascription I guess this is tricky

I see the same diagnostic on nightly (cargo 1.67.0-nightly (ba607b23d 2022-11-22)).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceT-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