Skip to content

Uninformative error on type mismatch in enum value constexpr #13534

Closed

Description

@kmcallister
enum Foo {
    Bar = (1u & 0xff) << 1,
}
$ rustc -v
rustc 0.11-pre (cee9a83 2014-04-11 15:54:46 -0700)
host: x86_64-unknown-linux-gnu

$ rustc --crate-type lib foo.rs
foo.rs:4:11: 4:27 error: expected constant: bad operands for binary
foo.rs:4     Bar = (1u & 0xff) << 1,
                   ^~~~~~~~~~~~~~~~
error: aborting due to previous error

Dropping the shift gives a more informative error:

foo.rs:4:11: 4:22 error: expected constant: can't do this op on a uint and int
foo.rs:4     Bar = (1u & 0xff), // << 1,
                   ^~~~~~~~~~~
error: aborting due to previous error

The fix is to write 0xffu, but the first error message was no help in finding this within a more complex expression inside a macro.

By contrast, this compiles fine:

static x: uint = (1u & 0xff) << 1;

and this

static x: uint = (1u & 0xffi) << 1;

gives a more conventional type error

foo.rs:1:24: 1:29 error: mismatched types: expected `uint` but found `int` (expected uint but found int)
foo.rs:1 static x: uint = (1u & 0xffi) << 1;

Activity

  1. Stebalien commented on Aug 11, 2015

    @Stebalien
    Contributor

    The error now reads:

    tmp.rs:2:12: 2:25 error: constant evaluation error: can't do this op on a usize and isize [E0080]
    tmp.rs:2     Bar = (1usize & 0xff) << 1,
                        ^~~~~~~~~~~~~
    tmp.rs:2:12: 2:25 help: run `rustc --explain E0080` to see a detailed explanation
    error: aborting due to previous error
    

    So this is probably fixed.

  2. steveklabnik commented on Nov 5, 2015

    @steveklabnik
    Contributor

    Yup! Same error today, seems fine.

  3. added a commit that references this issue on Oct 18, 2024
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-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions