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

Confusing error: cannot infer type for type u8 #69683

Closed
is8ac opened this issue Mar 3, 2020 · 0 comments · Fixed by #71960
Closed

Confusing error: cannot infer type for type u8 #69683

is8ac opened this issue Mar 3, 2020 · 0 comments · Fixed by #71960
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@is8ac
Copy link

is8ac commented Mar 3, 2020

rustc gives confusing error, suggesting that I give b an explicit type when b already has an explicit type.

Replacing 0u16.foo(b) with <u16 as Foo<[(); 3]>>::foo(0u16, b) fixes it.

This may be a duplicate of #69123

Source

pub trait Element<S> {
    type Array;
}

impl<T> Element<()> for T {
    type Array = T;
}

impl<T: Element<S>, S> Element<[S; 3]> for T {
    type Array = [T::Array; 3];
}

trait Foo<I>
where
    u8: Element<I>,
{
    fn foo(self, x: <u8 as Element<I>>::Array);
}

impl<I> Foo<I> for u16
where
    u8: Element<I>,
{
    fn foo(self, _: <u8 as Element<I>>::Array) {}
}

fn main() {
    let b: [u8; 3] = [0u8; 3];

    0u16.foo(b);
    //<u16 as Foo<[(); 3]>>::foo(0u16, b);
}

Log

$ rustc src/bin/type_param_demo.rs 
error[E0284]: type annotations needed for `[u8; 3]`
  --> src/bin/type_param_demo.rs:30:10
   |
28 |     let b: [u8; 3] = [0u8; 3];
   |         - consider giving `b` the explicit type `[u8; 3]`, where the type parameter `u8` is specified
29 | 
30 |     0u16.foo(b);
   |          ^^^ cannot infer type for type `u8`
   |
   = note: cannot resolve `<u8 as Element<_>>::Array == [u8; 3]`

error: aborting due to previous error

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

Meta

Same issue occurs in both latest and nightly and stable.

$ rustc --version --verbose
rustc 1.43.0-nightly (18c275b42 2020-03-02)
binary: rustc
commit-hash: 18c275b423f9f13c0e404ae3804967d2ab66337c
commit-date: 2020-03-02
host: x86_64-unknown-linux-gnu
release: 1.43.0-nightly
LLVM version: 9.0
$ rustc --version --verbose
rustc 1.41.1 (f3e1a954d 2020-02-24)
binary: rustc
commit-hash: f3e1a954d2ead4e2fc197c7da7d71e6c61bad196
commit-date: 2020-02-24
host: x86_64-unknown-linux-gnu
release: 1.41.1
LLVM version: 9.0
@sfackler sfackler added the A-diagnostics Area: Messages for errors, warnings, and lints label Mar 4, 2020
@estebank estebank added D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. and removed A-diagnostics Area: Messages for errors, warnings, and lints labels Mar 4, 2020
@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Mar 8, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue May 7, 2020
@bors bors closed this as completed in a7b03ad May 8, 2020
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-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants