Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rustc complains about not being able to infer the wrong type parameter #105797

Closed
sfackler opened this issue Dec 16, 2022 · 1 comment
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@sfackler
Copy link
Member

sfackler commented Dec 16, 2022

I tried this code:

trait ToFoo<T, W> {
    type Foo: Foo<W>;
    
    fn to_foo(value: T) -> Self::Foo;
}

trait Foo<W> {}

struct Fooer<T>(T);

impl<T, W> Foo<W> for Fooer<T> {}

struct ToFooer;

impl<T, W> ToFoo<T, W> for ToFooer {
    type Foo = Fooer<T>;
    
    fn to_foo(value: T) -> Self::Foo {
        Fooer(value)
    }
}

fn main() {
    let _foo = <ToFooer as ToFoo<_, _>>::to_foo(());
}

I expected to see this happen: The compiler complain that the W parameter of ToFoo couldn't be inferred.

Instead, this happened: It instead complains about T.

error[[E0282]](https://doc.rust-lang.org/stable/error-index.html#E0282): type annotations needed
  --> src/main.rs:24:16
   |
24 |     let _foo = <ToFooer as ToFoo<_, _>>::to_foo(());
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the trait `ToFoo`

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

T should be inferred from the argument being passed to to_foo, but you get the same error message even if you explicitly specify it:

error[[E0282]](https://doc.rust-lang.org/stable/error-index.html#E0282): type annotations needed
  --> src/main.rs:24:16
   |
24 |     let _foo = <ToFooer as ToFoo<(), _>>::to_foo(());
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the trait `ToFoo`

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

It successfully compiles when the second type parameter (i.e. W) is explicitly specified.

Meta

rustc --version --verbose:

rustc 1.66.0 (69f9c33d7 2022-12-12)
binary: rustc
commit-hash: 69f9c33d71c871fc16ac445211281c6e7a340943
commit-date: 2022-12-12
host: x86_64-unknown-linux-gnu
release: 1.66.0
LLVM version: 15.0.2

Same thing happens on nightly.

@sfackler sfackler added the C-bug Category: This is a bug. label Dec 16, 2022
@fmease fmease added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. and removed needs-triage-legacy labels Jan 25, 2024
@fmease
Copy link
Member

fmease commented Jan 25, 2024

There was an off-by-one in the compiler. I fixed that one a while ago, namely in #109957.
Output is now:

error[E0282]: type annotations needed
  --> off.rs:24:16
   |
24 |     let _foo = <ToFooer as ToFoo<_, _>>::to_foo(());
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `W` declared on the trait `ToFoo`

Closing as fixed.

@fmease fmease closed this as completed Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants