rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.34k stars 12.59k forks source link

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

Open laurmaedje opened 2 years ago

laurmaedje commented 2 years ago

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(>::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(f: for<'a> fn(>::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

fmease commented 8 months ago

Fixed in the next-gen trait solver (-Znext-solver).