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

Nonsensical help message in error E0277 #113447

Closed
ElectrifyPro opened this issue Jul 7, 2023 · 4 comments · Fixed by #113945
Closed

Nonsensical help message in error E0277 #113447

ElectrifyPro opened this issue Jul 7, 2023 · 4 comments · Fixed by #113945
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ElectrifyPro
Copy link
Contributor

ElectrifyPro commented Jul 7, 2023

I tried this code (while debugging):

async fn handle_binary(server_ref: &Ref<State>, uuid: Uuid, bytes: Bytes) -> Result<(), SendError<Packet>> {
    if uuid.as_bytes() == [0xaa, 0x2a, 0x15, 0xf6, 0xca, 0xb3, 0x46, 0xdf, 0xa8, 0x28, 0x25, 0x4d, 0x96, 0x4d, 0xd8, 0x57] {
        // give user fake lag
        tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
    }
}

Where Uuid is the corresponding type from the uuid crate (and everything else is my types). It has an associated function as_bytes, which returns a 16-byte slice containing the UUID. Because the right-hand-side of the comparison is not a byte slice but an array, the two expressions can't be compared and I get error E0277, shown below. Of interest is the help message at the end, which has the correct solution, but proposes a fix with invalid syntax. It should suggest placing & before [0xaa,....

error[E0277]: can't compare `&[u8; 16]` with `[{integer}; 16]`
  --> src/api/ws.rs:68:24
   |
68 |     if uuid.as_bytes() == [0xaa, 0x2a, 0x15, 0xf6, 0xca, 0xb3, 0x46, 0xdf, 0xa8, 0x28, 0x25, 0x4d, 0x96, 0x4d, 0xd8, 0x57] {
   |                        ^^ no implementation for `&[u8; 16] == [{integer}; 16]`
   |
   = help: the trait `PartialEq<[{integer}; 16]>` is not implemented for `&[u8; 16]`
   = help: the following other types implement trait `PartialEq<Rhs>`:
             <&[B] as PartialEq<[A; N]>>
             <&[FormatItem<'_>] as PartialEq<FormatItem<'_>>>
             <&[OwnedFormatItem] as PartialEq<OwnedFormatItem>>
             <&[T] as PartialEq<Vec<U, A>>>
             <&[u8] as PartialEq<BytesMut>>
             <&[u8] as PartialEq<bytes::Bytes>>
             <&mut [B] as PartialEq<[A; N]>>
             <&mut [T] as PartialEq<Vec<U, A>>>
           and 11 others
help: convert the array to a `&[u8]` slice instead
   |
68 |     if uuid.as_bytes() &==[..] [0xaa, 0x2a, 0x15, 0xf6, 0xca, 0xb3, 0x46, 0xdf, 0xa8, 0x28, 0x25, 0x4d, 0x96, 0x4d, 0xd8, 0x57] {
   |                        +  ++++

Meta

rustc --version --verbose:

rustc 1.71.0-beta.4 (dbf31f17d 2023-06-24)
binary: rustc
commit-hash: dbf31f17d020475885f50fb7d5fc15ff239843d8
commit-date: 2023-06-24
host: x86_64-unknown-linux-gnu
release: 1.71.0-beta.4
LLVM version: 16.0.5

I could not test on nightly, as I got a different compile error, apparently related to procedural macros:

Error

warning: some crates are on edition 2021 which defaults to `resolver = "2"`, but virtual workspaces default to `resolver = "1"`
note: to keep the current resolver, specify `workspace.resolver = "1"` in the workspace root's manifest
note: to use the edition 2021 resolver, specify `workspace.resolver = "2"` in the workspace root's manifest
   Compiling proc-macro2 v1.0.48
   Compiling cc v1.0.78
   Compiling parking_lot_core v0.9.5
   Compiling lock_api v0.4.9
   Compiling slab v0.4.7
   Compiling tokio v1.23.0
    Checking futures-sink v0.3.25
   Compiling generic-array v0.14.6
    Checking mio v0.8.5
    Checking socket2 v0.4.7
error[E0422]: cannot find struct, variant or union type `LineColumn` in crate `proc_macro`
   --> /home/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.48/src/wrapper.rs:479:33
    |
479 |                 let proc_macro::LineColumn { line, column } = s.start();
    |                                 ^^^^^^^^^^ not found in `proc_macro`
    |
help: consider importing one of these items
    |
1   + use crate::LineColumn;
    |
1   + use crate::fallback::LineColumn;
    |
help: if you import `LineColumn`, refer to it directly
    |
479 -                 let proc_macro::LineColumn { line, column } = s.start();
479 +                 let LineColumn { line, column } = s.start();
    |

error[E0422]: cannot find struct, variant or union type `LineColumn` in crate `proc_macro`
   --> /home/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.48/src/wrapper.rs:496:33
    |
496 |                 let proc_macro::LineColumn { line, column } = s.end();
    |                                 ^^^^^^^^^^ not found in `proc_macro`
    |
help: consider importing one of these items
    |
1   + use crate::LineColumn;
    |
1   + use crate::fallback::LineColumn;
    |
help: if you import `LineColumn`, refer to it directly
    |
496 -                 let proc_macro::LineColumn { line, column } = s.end();
496 +                 let LineColumn { line, column } = s.end();
    |

error[E0635]: unknown feature `proc_macro_span_shrink`
  --> /home/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.48/src/lib.rs:92:30
   |
92 |     feature(proc_macro_span, proc_macro_span_shrink)
   |                              ^^^^^^^^^^^^^^^^^^^^^^

    Checking signal-hook-registry v1.4.0
    Checking num_cpus v1.14.0
    Checking parking_lot v0.12.1

@ElectrifyPro ElectrifyPro added the C-bug Category: This is a bug. label Jul 7, 2023
@jieyouxu
Copy link
Member

jieyouxu commented Jul 7, 2023

For the proc_macro_span_shrink error, see #113152.

It is fixed in proc-macro2 1.0.60+ dtolnay/proc-macro2#391

Could you try to bump your proc-macro2 version? Also, could you maybe provide either the full code or a smaller reproduction of the suggestion (if possible), I can't seem to reproduce the suggestion for your pattern on stable.

@ElectrifyPro
Copy link
Contributor Author

ElectrifyPro commented Jul 7, 2023

Thanks for the notice. I just bumped proc-macro2 to 1.0.63 and retried on nightly:

rustc 1.72.0-nightly (85bf07972 2023-07-06)
binary: rustc
commit-hash: 85bf07972a1041b9e25393b803d0e006bec3eaaf
commit-date: 2023-07-06
host: x86_64-unknown-linux-gnu
release: 1.72.0-nightly
LLVM version: 16.0.5

Unfortunately the exact same misleading help message still appears.

Also, could you maybe provide either the full code or a smaller reproduction of the suggestion (if possible), I can't seem to reproduce the suggestion for your pattern on stable.

I managed to reproduce it here: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=138ae39d401a51710bb27978cf730f49

The bytes::Bytes import or Bytes argument is actually necessary to cause the bug. If you remove both, the help message doesn't appear. Perhaps this is related to the uuid crate, which I noticed uses a type alias called Bytes (pub type Bytes = [u8; 16]), unrelated to bytes::Bytes.

@jieyouxu
Copy link
Member

jieyouxu commented Jul 7, 2023

Smaller repro but still requires bytes dependency:

use bytes::Bytes;

fn main() {
    &[0u8] == [0xAA];
}

Am investigating how to get rid of the bytes dependency.

@jieyouxu
Copy link
Member

jieyouxu commented Jul 7, 2023

Repro without dependencies:

pub struct Bytes;

impl Bytes {
    pub fn as_slice(&self) -> &[u8] {
        todo!()
    }
}

impl PartialEq<[u8]> for Bytes {
    fn eq(&self, other: &[u8]) -> bool {
        self.as_slice() == other
    }
}

impl PartialEq<Bytes> for &[u8] {
    fn eq(&self, other: &Bytes) -> bool {
        *other == **self
    }
}

fn main() {
    &[0u8] == [0xAA];
}

@chenyukang chenyukang self-assigned this Jul 14, 2023
@ChrisDenton ChrisDenton added the needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. label Jul 16, 2023
@Noratrieb Noratrieb 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. and removed needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. labels Jul 17, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 3, 2023
…-2, r=cjgillot

Fix wrong span for trait selection failure error reporting

Fixes rust-lang#113447
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 3, 2023
…-2, r=cjgillot

Fix wrong span for trait selection failure error reporting

Fixes rust-lang#113447
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 4, 2023
…-2, r=cjgillot

Fix wrong span for trait selection failure error reporting

Fixes rust-lang#113447
@bors bors closed this as completed in 5054e41 Aug 4, 2023
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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
5 participants