Skip to content

Uninformative error on type mismatch in enum value constexpr #13534

Closed
@kmcallister

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;

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