[COMMITTED 34/43] gccrs: Add missing maybe fold reference to constexpr from cp/constcexpr.cc

arthur.cohen@opensrcsec.com arthur.cohen@opensrcsec.com
Thu Sep 10 08:19:47 GMT 2026


From: Philip Herron <herron.philip@googlemail.com>

Fixes Rust-GCC/gccrs#4631

gcc/rust/ChangeLog:

	* backend/rust-constexpr.cc (maybe_fold_reference_address_to_pointer): port over from cp
	(eval_binary_expression): likewise

gcc/testsuite/ChangeLog:

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

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
---
 gcc/rust/backend/rust-constexpr.cc       | 24 +++++++++++++++++++++++-
 gcc/testsuite/rust/compile/issue-4631.rs | 13 +++++++++++++
 2 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/rust/compile/issue-4631.rs

diff --git a/gcc/rust/backend/rust-constexpr.cc b/gcc/rust/backend/rust-constexpr.cc
index e32ba3abd4f..908064394e7 100644
--- a/gcc/rust/backend/rust-constexpr.cc
+++ b/gcc/rust/backend/rust-constexpr.cc
@@ -553,6 +553,7 @@ static tree fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
 					  bool *non_constant_p,
 					  bool *overflow_p, tree *jump_target);
 static tree maybe_fold_addr_pointer_plus (tree t);
+static tree maybe_fold_reference_address_to_pointer (tree t);
 
 /* Variables and functions to manage constexpr call expansion context.
    These do not need to be marked for PCH or GC.  */
@@ -3157,6 +3158,9 @@ eval_binary_expression (const constexpr_ctx *ctx, tree t, bool lval,
   if (r == NULL_TREE && TREE_CODE_CLASS (code) == tcc_comparison
       && POINTER_TYPE_P (TREE_TYPE (lhs)))
     {
+      lhs = maybe_fold_reference_address_to_pointer (lhs);
+      rhs = maybe_fold_reference_address_to_pointer (rhs);
+
       if (tree lhso = maybe_fold_addr_pointer_plus (lhs))
 	lhs = fold_convert (TREE_TYPE (lhs), lhso);
       if (tree rhso = maybe_fold_addr_pointer_plus (rhs))
@@ -3180,7 +3184,10 @@ eval_binary_expression (const constexpr_ctx *ctx, tree t, bool lval,
 
   if (r == NULL_TREE)
     {
-      r = fold_binary_loc (loc, code, type, lhs, rhs);
+      if (ctx->manifestly_const_eval && TREE_CODE (type) != REAL_TYPE)
+	r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
+      else
+	r = fold_binary_loc (loc, code, type, lhs, rhs);
     }
 
   if (r == NULL_TREE && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
@@ -6870,6 +6877,21 @@ maybe_fold_addr_pointer_plus (tree t)
   return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r);
 }
 
+static tree
+maybe_fold_reference_address_to_pointer (tree t)
+{
+  if (!CONVERT_EXPR_P (t) || !POINTER_TYPE_P (TREE_TYPE (t)))
+    return t;
+
+  tree op = TREE_OPERAND (t, 0);
+  if (TREE_CODE (op) != ADDR_EXPR || !TYPE_REF_P (TREE_TYPE (op)))
+    return t;
+
+  return build_fold_addr_expr_with_type_loc (EXPR_LOCATION (t),
+					     TREE_OPERAND (op, 0),
+					     TREE_TYPE (t));
+}
+
 } // namespace Compile
 } // namespace Rust
 
diff --git a/gcc/testsuite/rust/compile/issue-4631.rs b/gcc/testsuite/rust/compile/issue-4631.rs
new file mode 100644
index 00000000000..8b4cea77922
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4631.rs
@@ -0,0 +1,13 @@
+#![feature(no_core)]
+#![no_core]
+#![feature(lang_items)]
+
+#[lang = "sized"]
+trait Sized {}
+
+pub static FOO: i32 = 42;
+pub static BAR: i32 = 42;
+
+pub static BAZ: bool = { (&FOO as *const i32) == (&BAR as *const i32) };
+
+pub fn main() {}
-- 
2.50.1



More information about the Gcc-rust mailing list