]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix ICE when doing shift checks on const decl
authorPhilip Herron <herron.philip@googlemail.com>
Mon, 31 Mar 2025 16:58:24 +0000 (17:58 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 8 Apr 2025 08:17:11 +0000 (10:17 +0200)
Const decls are just delcarations wrapping the value into the DECL_INITIAL
and the shift checks we have assume no decls are involved and its just flat
values. This patch simply unwraps the constant values if they exist.

Fixes Rust-GCC#3665

gcc/rust/ChangeLog:

* rust-gcc.cc (arithmetic_or_logical_expression): unwrap const decls

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/rust-gcc.cc
gcc/testsuite/rust/compile/issue-3665.rs [new file with mode: 0644]

index a6e8ea904a8a5b03d7f82378958f117002d5bcab..72aef08e35a0df9db2b6b89be7b9e31549416c06 100644 (file)
@@ -1074,6 +1074,12 @@ arithmetic_or_logical_expression (ArithmeticOrLogicalOperator op, tree left,
   if (left == error_mark_node || right == error_mark_node)
     return error_mark_node;
 
+  // unwrap the const decls if set
+  if (TREE_CODE (left) == CONST_DECL)
+    left = DECL_INITIAL (left);
+  if (TREE_CODE (right) == CONST_DECL)
+    right = DECL_INITIAL (right);
+
   /* We need to determine if we're doing floating point arithmetics of integer
      arithmetics. */
   bool floating_point = is_floating_point (left);
diff --git a/gcc/testsuite/rust/compile/issue-3665.rs b/gcc/testsuite/rust/compile/issue-3665.rs
new file mode 100644 (file)
index 0000000..d66a81f
--- /dev/null
@@ -0,0 +1,6 @@
+pub const uint_val: usize = 1;
+pub const uint_expr: usize = 1 << uint_val;
+
+pub fn test() -> usize {
+    uint_expr
+}