rust-lang / rust-lang/rust

The compiler is unable to select `PartialOrd` implementation to compare `&[u8]` and `&[u8;32]` if `bytes` crate is imported

Open
#130,464 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-trait-system C-bug T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

use bytes::Bytes;

fn main() {
    let hash = Vec::<u8>::new();
    assert!(hash.as_slice() < &[0u8; 32]);
}

playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b54926ee34df82189cedb9ef01de3afb

I expected to see this happen: The code should compile

Instead, this happened: Compilation fails:

error[E0277]: can't compare `[u8]` with `[u8; 32]`
 --> src/main.rs:7:29
  |
7 |     assert!(hash.as_slice() < &[0u8; 32]);
  |                             ^ no implementation for `[u8] < [u8; 32]` and `[u8] > [u8; 32]`
  |
  = help: the trait `PartialOrd<[u8; 32]>` is not implemented for `[u8]`, which is required by `&[u8]: PartialOrd<&[u8; 32]>`
  = help: the following other types implement trait `PartialOrd<Rhs>`:
            `&[u8]` implements `PartialOrd<BytesMut>`
            `&[u8]` implements `PartialOrd<bytes::Bytes>`
            `[u8]` implements `PartialOrd<BytesMut>`
            `[u8]` implements `PartialOrd<bytes::Bytes>`
  = note: required for `&[u8]` to implement `PartialOrd<&[u8; 32]>`

The code compiles after removing use bytes::Bytes;

Meta

rustc --version --verbose:

❯ rustc --version --verbose
rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-unknown-linux-gnu
release: 1.81.0
LLVM version: 18.1.7

It also fails with:

  • beta 1.82.0-beta.3 (2024-09-09 4976ae480e2b29cc46f4)
  • nightly 1.83.0-nightly (2024-09-16 c52c23b6f44cd1971872)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the playground reproduction in src/main.rs and confirm the comparison fails when bytes::Bytes is imported but succeeds without it. Compare the behavior on stable, beta, and nightly as reported; done means the example compiles with the import present and the relevant compiler tests cover the regression.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.