Skip to content

Compile error due to the wrong literal data type #68951

@Disasm

Description

@Disasm
Contributor

I tried this code:

fn main() {
    let array = [0x42u8; 10];
    for b in &array {
        let lo = b & 0xf;
        let hi = (b >> 4) & 0xf;
    }
}

Playground

I expected to see this happen: it should be compiled

Instead, this happened:

error[E0277]: no implementation for `u8 & i32`
 --> src/main.rs:5:27
  |
5 |         let hi = (b >> 4) & 0xf;
  |                           ^ no implementation for `u8 & i32`
  |
  = help: the trait `std::ops::BitAnd<i32>` is not implemented for `u8`

Meta

rustc --version --verbose:

rustc 1.41.0 (5e1a79984 2020-01-27)
binary: rustc
commit-hash: 5e1a799842ba6ed4a57e91f7ab9435947482f7d8
commit-date: 2020-01-27
host: x86_64-unknown-linux-gnu
release: 1.41.0
LLVM version: 9.0

The same happens on beta and nightly.

Backtrace

Compiling playground v0.0.1 (/playground)
error[E0277]: no implementation for `u8 & i32`
 --> src/main.rs:5:27
  |
5 |         let hi = (b >> 4) & 0xf;
  |                           ^ no implementation for `u8 & i32`
  |
  = help: the trait `std::ops::BitAnd<i32>` is not implemented for `u8`

error: aborting due to previous error

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

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

Activity

added
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Feb 8, 2020
pragmatrix

pragmatrix commented on Aug 26, 2020

@pragmatrix

Looks like this bug has been fixed in one of the recent Rust updates. Rust stable 1.45.2 compiles the code above without errors.

added
E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
on Aug 26, 2020
added a commit that references this issue on Oct 3, 2020
a341710
added a commit that references this issue on Oct 4, 2020
f09c962
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceC-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @pragmatrix@Disasm@jonas-schievink

      Issue actions

        Compile error due to the wrong literal data type · Issue #68951 · rust-lang/rust