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

Closed
MahadMuhammad opened this issue Jul 7, 2023 · 3 comments · Fixed by #3279
Closed

Failed to evaluate constant value #2394

MahadMuhammad opened this issue Jul 7, 2023 · 3 comments · Fixed by #3279
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
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.

@philberty philberty added this to the Remaining typecheck issues milestone Dec 2, 2024
philberty added a commit that referenced this issue Dec 2, 2024
These are ported from the c-family code c-warn.cc and c-typchk.cc

Fixes #2394

gcc/rust/ChangeLog:

	* backend/rust-constexpr.cc (eval_store_expression): check for null
	(eval_call_expression): formatting
	(eval_loop_expr): formatting
	* rust-gcc.cc (arithmetic_or_logical_expression): add warnings

gcc/testsuite/ChangeLog:

	* rust/compile/issue-2394.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
philberty added a commit that referenced this issue Dec 2, 2024
These are ported from the c-family code c-warn.cc and c/c-typchk.cc

Fixes #2394

gcc/rust/ChangeLog:

	* backend/rust-constexpr.cc (eval_store_expression): check for null
	(eval_call_expression): remove bad warning
	* rust-gcc.cc (arithmetic_or_logical_expression): add warnings

gcc/testsuite/ChangeLog:

	* rust/compile/issue-2394.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
github-merge-queue bot pushed a commit that referenced this issue Dec 2, 2024
These are ported from the c-family code c-warn.cc and c/c-typchk.cc

Fixes #2394

gcc/rust/ChangeLog:

	* backend/rust-constexpr.cc (eval_store_expression): check for null
	(eval_call_expression): remove bad warning
	* rust-gcc.cc (arithmetic_or_logical_expression): add warnings

gcc/testsuite/ChangeLog:

	* rust/compile/issue-2394.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
@github-project-automation github-project-automation bot moved this from Todo to Done in libcore 1.49 Dec 2, 2024
tschwinge pushed a commit that referenced this issue Dec 4, 2024
These are ported from the c-family code c-warn.cc and c/c-typchk.cc

Fixes #2394

gcc/rust/ChangeLog:

	* backend/rust-constexpr.cc (eval_store_expression): check for null
	(eval_call_expression): remove bad warning
	* rust-gcc.cc (arithmetic_or_logical_expression): add warnings

gcc/testsuite/ChangeLog:

	* rust/compile/issue-2394.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Kamiinarii78 pushed a commit to Kamiinarii78/gccrs that referenced this issue Dec 12, 2024
These are ported from the c-family code c-warn.cc and c/c-typchk.cc

Fixes Rust-GCC#2394

gcc/rust/ChangeLog:

	* backend/rust-constexpr.cc (eval_store_expression): check for null
	(eval_call_expression): remove bad warning
	* rust-gcc.cc (arithmetic_or_logical_expression): add warnings

gcc/testsuite/ChangeLog:

	* rust/compile/issue-2394.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
matthewjasper pushed a commit to matthewjasper/gccrs that referenced this issue Dec 21, 2024
These are ported from the c-family code c-warn.cc and c/c-typchk.cc

Fixes Rust-GCC#2394

gcc/rust/ChangeLog:

	* backend/rust-constexpr.cc (eval_store_expression): check for null
	(eval_call_expression): remove bad warning
	* rust-gcc.cc (arithmetic_or_logical_expression): add warnings

gcc/testsuite/ChangeLog:

	* rust/compile/issue-2394.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants