Skip to content

Commit 38fc892

Browse files
committed
gccrs: Add missing maybe fold reference to constexpr from cp/constcexpr.cc
Fixes #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>
1 parent 05c8d7b commit 38fc892

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

gcc/rust/backend/rust-constexpr.cc

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ static tree fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
553553
bool *non_constant_p,
554554
bool *overflow_p, tree *jump_target);
555555
static tree maybe_fold_addr_pointer_plus (tree t);
556+
static tree maybe_fold_reference_address_to_pointer (tree t);
556557

557558
/* Variables and functions to manage constexpr call expansion context.
558559
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,
31573158
if (r == NULL_TREE && TREE_CODE_CLASS (code) == tcc_comparison
31583159
&& POINTER_TYPE_P (TREE_TYPE (lhs)))
31593160
{
3161+
lhs = maybe_fold_reference_address_to_pointer (lhs);
3162+
rhs = maybe_fold_reference_address_to_pointer (rhs);
3163+
31603164
if (tree lhso = maybe_fold_addr_pointer_plus (lhs))
31613165
lhs = fold_convert (TREE_TYPE (lhs), lhso);
31623166
if (tree rhso = maybe_fold_addr_pointer_plus (rhs))
@@ -3180,7 +3184,10 @@ eval_binary_expression (const constexpr_ctx *ctx, tree t, bool lval,
31803184

31813185
if (r == NULL_TREE)
31823186
{
3183-
r = fold_binary_loc (loc, code, type, lhs, rhs);
3187+
if (ctx->manifestly_const_eval && TREE_CODE (type) != REAL_TYPE)
3188+
r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
3189+
else
3190+
r = fold_binary_loc (loc, code, type, lhs, rhs);
31843191
}
31853192

31863193
if (r == NULL_TREE && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
@@ -6870,6 +6877,21 @@ maybe_fold_addr_pointer_plus (tree t)
68706877
return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r);
68716878
}
68726879

6880+
static tree
6881+
maybe_fold_reference_address_to_pointer (tree t)
6882+
{
6883+
if (!CONVERT_EXPR_P (t) || !POINTER_TYPE_P (TREE_TYPE (t)))
6884+
return t;
6885+
6886+
tree op = TREE_OPERAND (t, 0);
6887+
if (TREE_CODE (op) != ADDR_EXPR || !TYPE_REF_P (TREE_TYPE (op)))
6888+
return t;
6889+
6890+
return build_fold_addr_expr_with_type_loc (EXPR_LOCATION (t),
6891+
TREE_OPERAND (op, 0),
6892+
TREE_TYPE (t));
6893+
}
6894+
68736895
} // namespace Compile
68746896
} // namespace Rust
68756897

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(no_core)]
2+
#![no_core]
3+
#![feature(lang_items)]
4+
5+
#[lang = "sized"]
6+
trait Sized {}
7+
8+
pub static FOO: i32 = 42;
9+
pub static BAR: i32 = 42;
10+
11+
pub static BAZ: bool = { (&FOO as *const i32) == (&BAR as *const i32) };
12+
13+
pub fn main() {}

0 commit comments

Comments
 (0)