Skip to content

Inconsistent failure in byte slice &[u8] comparison caused by unrelated code #77314

Open
@aidanhs

Description

@aidanhs

I tried this code:

[package]
name = "rbup"
version = "0.1.0"
authors = ["Aidan Hobson Sayers <aidanhs@cantab.net>"]
edition = "2018"

[dependencies]
assert_fs = "1.0"
use std::os::unix::ffi::OsStrExt;
use std::path::PathBuf;

fn main() {}

pub fn foo() {
    //let temp = assert_fs::TempDir::new().unwrap(); // XXX
}

pub fn x() {
    let prefix: &[u8] = &[];
    let name: &Vec<u8> = &vec![];
    let _ = prefix > name.as_slice(); // works
    let _ = prefix > name; // broken iff XXX is uncommented
}

pub fn y() {
    let lastfile: &PathBuf = &PathBuf::new();
    let path: PathBuf = PathBuf::new();
    assert!(lastfile.as_os_str().as_bytes() > path.as_os_str().as_bytes()); // works
    assert!(lastfile.as_os_str().as_bytes() > &path.as_os_str().as_bytes()); // broken iff XXX is uncommented
}

I expected to see this happen: the code either consistently compiles or fails to compile.

Instead, this happened: depending on whether the line marked XXX is commented or not, the unrelated lines marked broken cause compilation failures (in turn, commenting those out allows compilation to succeed). i.e. broken and XXX lines are mutually exclusive.

Meta

rustc --version --verbose:

rustc 1.48.0-nightly (623fb90b5 2020-09-26)
binary: rustc
commit-hash: 623fb90b5a1f324e0ec44085116bf858cef19a00
commit-date: 2020-09-26
host: x86_64-unknown-linux-gnu
release: 1.48.0-nightly
LLVM version: 11.0
Backtrace

+ cargo --color=always build --release
   Compiling rbup v0.1.0 (/tmp/x)
error[E0277]: can't compare `[u8]` with `Vec<u8>`
  --> src/main.rs:14:20
   |
14 |     let _ = prefix > name; // broken iff XXX is uncommented
   |                    ^ no implementation for `[u8] < Vec<u8>` and `[u8] > Vec<u8>`
   |
   = help: the trait `PartialOrd<Vec<u8>>` is not implemented for `[u8]`
   = note: required because of the requirements on the impl of `PartialOrd<&Vec<u8>>` for `&[u8]`

error[E0277]: can't compare `[u8]` with `&[u8]`
  --> src/main.rs:21:45
   |
21 |     assert!(lastfile.as_os_str().as_bytes() > &path.as_os_str().as_bytes()); // broken iff XXX is uncommented
   |                                             ^ no implementation for `[u8] < &[u8]` and `[u8] > &[u8]`
   |
   = help: the trait `PartialOrd<&[u8]>` is not implemented for `[u8]`
   = note: required because of the requirements on the impl of `PartialOrd<&&[u8]>` for `&[u8]`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
error: could not compile `rbup`

To learn more, run the command again with --verbose.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceA-sliceArea: `[T]`A-type-systemArea: Type systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions