Skip to content

Commit a263235

Browse files
committed
gccrs: Port over missing GOTO handling from cp/constexpr.cc
Fixes #1553 gcc/rust/ChangeLog: * backend/rust-constexpr.cc (eval_constant_expression): port over goto (potential_constant_expression_1): likewise gcc/testsuite/ChangeLog: * rust/compile/issue-1553.rs: New test. Signed-off-by: Philip Herron <herron.philip@googlemail.com>
1 parent 0029e0e commit a263235

2 files changed

Lines changed: 61 additions & 3 deletions

File tree

‎gcc/rust/backend/rust-constexpr.cc‎

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,9 @@ eval_constant_expression (const constexpr_ctx *ctx, tree t, bool lval,
19181918
bool *non_constant_p, bool *overflow_p,
19191919
tree *jump_target)
19201920
{
1921+
if (t == NULL_TREE)
1922+
return NULL_TREE;
1923+
19211924
if (jump_target && *jump_target)
19221925
{
19231926
/* If we are jumping, ignore all statements/expressions except those
@@ -1951,9 +1954,6 @@ eval_constant_expression (const constexpr_ctx *ctx, tree t, bool lval,
19511954

19521955
location_t loc = EXPR_LOCATION (t);
19531956

1954-
if (t == NULL_TREE)
1955-
return NULL_TREE;
1956-
19571957
if (CONSTANT_CLASS_P (t))
19581958
{
19591959
if (TREE_OVERFLOW (t))
@@ -2390,6 +2390,29 @@ eval_constant_expression (const constexpr_ctx *ctx, tree t, bool lval,
23902390
jump_target);
23912391
break;
23922392

2393+
case GOTO_EXPR:
2394+
{
2395+
tree target = TREE_OPERAND (t, 0);
2396+
if (breaks (&target) || continues (&target) || returns (&target)
2397+
|| (TREE_CODE (target) == LABEL_DECL && DECL_ARTIFICIAL (target)))
2398+
{
2399+
if (jump_target)
2400+
*jump_target = target;
2401+
else
2402+
{
2403+
gcc_assert (ctx->quiet);
2404+
*non_constant_p = true;
2405+
}
2406+
}
2407+
else
2408+
{
2409+
if (!ctx->quiet)
2410+
error_at (loc, "%<goto%> is not a constant expression");
2411+
*non_constant_p = true;
2412+
}
2413+
}
2414+
break;
2415+
23932416
case LOOP_EXPR:
23942417
case WHILE_STMT:
23952418
case FOR_STMT:
@@ -6655,6 +6678,21 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
66556678
/* We can see these in statement-expressions. */
66566679
return true;
66576680

6681+
case GOTO_EXPR:
6682+
{
6683+
tree *target = &TREE_OPERAND (t, 0);
6684+
if (breaks (target) || continues (target) || returns (target))
6685+
{
6686+
*jump_target = *target;
6687+
return true;
6688+
}
6689+
if (TREE_CODE (*target) == LABEL_DECL && DECL_ARTIFICIAL (*target))
6690+
return true;
6691+
if (flags & tf_error)
6692+
error_at (loc, "%<goto%> is not a constant expression");
6693+
return false;
6694+
}
6695+
66586696
case LABEL_EXPR:
66596697
t = LABEL_EXPR_LABEL (t);
66606698
if (DECL_ARTIFICIAL (t))
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// { dg-options "-w -O0 -fdump-tree-gimple" }
2+
#![feature(no_core)]
3+
#![no_core]
4+
5+
const fn test(x: i32) -> i32 {
6+
return match x {
7+
0 => 100,
8+
_ => 200,
9+
};
10+
}
11+
12+
const X: i32 = test(0);
13+
const Y: i32 = test(1);
14+
15+
fn main() {
16+
// { dg-final { scan-tree-dump-times {x = 100} 1 gimple } }
17+
let x = X;
18+
// { dg-final { scan-tree-dump-times {y = 200} 1 gimple } }
19+
let y = Y;
20+
}

0 commit comments

Comments
 (0)