Skip to content

Function type with HRTB and associated type argument unifies inconsistently #101794

Open
@laurmaedje

Description

@laurmaedje

This is a similar issue as #84937, #89436, and #90875. Those were fixed by #90887, but it seems like the issue still exists (on stable and nightly). It is pretty flakey: In this case, it can be fixed by adding a type annotation and even by changing the order of the exec function's arguments.

fn main() {
    // error: mismatched types
    //   expected fn pointer `for<'a> fn(<u32 as Input>::Projected<'a>) -> _`
    //         found fn item `fn(u32) -> u32 {double}`
    //
    // Two ways to fix the problem here:
    // - Add `::<u32, _>` below
    // - Switch the order of the parameters on `exec`
    exec(double, 10);
}

fn double(x: u32) -> u32 {
    2 * x
}

fn exec<In, Out>(f: for<'a> fn(<In as Input<'a>>::Projected) -> Out, input: In) -> Out
where
    In: for<'a> Input<'a>,
{
    todo!()
}

trait Input<'a> {
    type Projected;
}

impl<'a> Input<'a> for u32 {
    type Projected = u32;
}

Full Diagnostic

error[E0308]: mismatched types
  --> src/lib.rs:26:11
   |
26 |     query(double, 10);
   |     ----- ^^^^^^ expected associated type, found `u32`
   |     |
   |     arguments to this function are incorrect
   |
   = note: expected fn pointer `for<'a> fn(<u32 as Input<'a>>::Projected) -> _`
                 found fn item `fn(u32) -> u32 {double}`
   = help: consider constraining the associated type `<_ as Input<'a>>::Projected` to `u32` or calling a method that returns `<_ as Input<'a>>::Projected`
   = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
note: function defined here
  --> src/lib.rs:33:4
   |
33 | fn query<In, Out>(f: for<'a> fn(<In as Input<'a>>::Projected) -> Out, input: In) -> Out
   |    ^^^^^          --------------------------------------------------

For more information about this error, try `rustc --explain E0308`.

Meta

rustc --version --verbose:

rustc 1.65.0-nightly (17cbdfd07 2022-09-13)
binary: rustc
commit-hash: 17cbdfd07178349d0a3cecb8e7dde8f915666ced
commit-date: 2022-09-13
host: aarch64-apple-darwin
release: 1.65.0-nightly
LLVM version: 15.0.0

cc @jackh726

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-higher-rankedArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)A-lazy-normalizationArea: Lazy normalization (tracking issue: #60471)A-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemC-bugCategory: This is a bug.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.fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions