Skip to content

Confusing 'type mismatch' error when using function pointer as fn trait #73457

Open
@Aaron1011

Description

@Aaron1011

The following code:

struct MyType;

fn parse_header<T, F: FnOnce(&MyType) -> T>(parse: F) -> T {
    panic!()
}

fn make_it(val: &MyType) -> &MyType { panic!() }

pub fn from_url<'a>() -> &'a MyType {
    parse_header(make_it)
}

fn main() {}

gives this error:

error[E0271]: type mismatch resolving `for<'r> <for<'s> fn(&'s MyType) -> &'s MyType {make_it} as std::ops::FnOnce<(&'r MyType,)>>::Output == _`
  --> src/main.rs:10:5
   |
3  | fn parse_header<T, F: FnOnce(&MyType) -> T>(parse: F) -> T {
   |                                          - required by this bound in `parse_header`
...
10 |     parse_header(make_it)
   |     ^^^^^^^^^^^^ expected bound lifetime parameter, found concrete lifetime

There are several issues with this error:

  • The 'type mismatch' message contains several pieces of 'internal' rust syntax (multiple consecutive for<> binders, the weird {make_it} syntax for function pointers, and an inference variable).
  • We call the return type T a 'bound', even though it's clearly not a bound.
  • We give an confusing 'expected bound lifetime parameter, found concrete lifetime' message, which doesn't really explain what's going on
  • We don't point at make_it, even though it's part of the issue.

This was minimized from code posted by @rvolosatovs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsD-confusingDiagnostics: Confusing error or lint that should be reworked.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