Skip to content

Extremely confusing error around function pointers with inferred arguments #76353

Open
@jyn514

Description

@jyn514

Consider the following code (playground):

struct S<'a>(&'a str);

fn f(inner: fn(&str, &S)) {
}

#[allow(unreachable_code)]
fn main() {
    let inner: fn(_, _) = unimplemented!();
    f(inner);
}

Rustc gives an error:

error[E0308]: mismatched types
 --> src/main.rs:9:7
  |
9 |     f(inner);
  |       ^^^^^ one type is more general than the other
  |
  = note: expected fn pointer `for<'r, 's, 't0> fn(&'r str, &'s S<'t0>)`
             found fn pointer `fn(&str, &S<'_>)`

Here is the fixed code (playground):

    let inner: fn(&str, &S<'_>) = unimplemented!();
    f(inner);

In particular, the only thing that changed was fn(_, _) -> fn(&str, &S<'_>). But this is exactly the type that was printed in the diagnostic originally! What changed?

I don't understand why this error was emitted, but it would at least be nice to suggest the transformation that makes the code work.

Reduced from rust-lang/crater#542.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceA-type-systemArea: Type systemD-confusingDiagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant 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