Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uninformative error on type mismatch in enum value constexpr #13534

Closed
kmcallister opened this issue Apr 15, 2014 · 2 comments
Closed

Uninformative error on type mismatch in enum value constexpr #13534

kmcallister opened this issue Apr 15, 2014 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@kmcallister
Copy link
Contributor

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;
@kmcallister kmcallister added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 27, 2014
@Stebalien
Copy link
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.

@steveklabnik
Copy link
Member

Yup! Same error today, seems fine.

flip1995 pushed a commit to flip1995/rust that referenced this issue Oct 18, 2024
…ed-item, r=flip1995

Fix large_stack_arrays triggering when nesting const items

Fixes rust-lang#13529.

r? `@flip1995`

changelog: [`large_stack_arrays`]: No longer triggers in static/const context when using nested items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

No branches or pull requests

3 participants