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

Failed to evaluate constant value #2394

Open
MahadMuhammad opened this issue Jul 7, 2023 · 3 comments
Open

Failed to evaluate constant value #2394

MahadMuhammad opened this issue Jul 7, 2023 · 3 comments
Assignees
Labels

Comments

@MahadMuhammad
Copy link
Contributor

MahadMuhammad commented Jul 7, 2023

Failed to evaluate constant value E0080

  • Fixing this issue, help gccrs to emit more user friendly error code and error emission similiar to rustc.
  • You can view the same on compiler-explorer

I tried this code from E0080:

#![allow(unused)]
fn main() {
enum Enum {
    X = (1 << 500),
    Y = (1 / 0),
}
}

I expected to see this happen:

  • Emit error message, similiar to rustc .i.e.,
    • evaluation of constant value failed
  • Error message emitted by rustc:
error[E0080]: evaluation of constant value failed
 --> <source>:4:9
  |
4 |     X = (1 << 500),
  |         ^^^^^^^^^^ attempt to shift left by `500_i32`, which would overflow

error[E0080]: evaluation of constant value failed
 --> <source>:5:9
  |
5 |     Y = (1 / 0),
  |         ^^^^^^^ attempt to divide `1_isize` by zero

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0080`.
Compiler returned: 1

Instead, this happened:

  • Compiled Successfully.
  • This is the output of gccrs:
Compiler returned: 0

Meta

  • What version of Rust GCC were you using, git sha b9566fd.
  • gccrs (Compiler-Explorer-Build-gcc-2f91d511200bf85558c9013b09a458c06edd1e02-binutils-2.40) 13.0.1 20230417 (experimental)

@philberty philberty self-assigned this Jul 30, 2023
@philberty
Copy link
Member

There are a few parts to this issue:

  1. We are not doing const-eval on the enum discriminant values during compilation of the type
  2. GCC is not doing overflow checking on shift expression see: fold_convert_loc is simply folding the value and not marking TREE_OVERFLOW

We should probably be calling build2 instead of fold_build2 when inside a const context to get the constexpr code to do the overflow checking for us might fix this

@philberty
Copy link
Member

We actually need to port over code from c/c-fold.cc to get support for detecting this case the default GCC middle end does not set TREE_OVERFLOW for us.

@dkm
Copy link
Member

dkm commented Jul 31, 2023

Side note, when looking at the precise version from Compiler-Explorer for nightly compilers, do NOT use the tooltip at the bottom. These are pre-computed offline and will not be updated along with the compiler every night. It only gets updated when we release a new version of compiler explorer (can be several times a week, sometimes more, sometimes less...).
If you need the exact version, use --version, or check the extra information in the assembly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants