Skip to content

Commit 0114e3e

Browse files
committed
Add constant folding to ArrayIndexExpr
This ensures that constexpr is enforced for array-index-exprs resulting in gimple like this: ```rust fn main() { const A: [i32; 3] = [1, 2, 3]; const B: i32 = A[1]; const C: usize = 42; const D: i32 = 7; let _a = C; let _b: [i32; C] = [0; C]; let _c = B + D; } ``` ```c void main () { const usize _a; i32 _b[42]; const i32 _c; try { _a = 42; _b = {}; _1 = 2; _c = _1 + 7; } finally { _b = {CLOBBER}; } } ```
1 parent 657a973 commit 0114e3e

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

gcc/rust/typecheck/rust-hir-const-fold.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,15 @@ class ConstFoldExpr : public ConstFoldBase
427427
= ctx->get_backend ()->negation_expression (op, negated_expr, location);
428428
}
429429

430+
void visit (HIR::ArrayIndexExpr &expr) override
431+
{
432+
Bexpression *array = ConstFoldExpr::fold (expr.get_array_expr ());
433+
Bexpression *index = ConstFoldExpr::fold (expr.get_index_expr ());
434+
435+
folded = ctx->get_backend ()->array_index_expression (array, index,
436+
expr.get_locus ());
437+
}
438+
430439
private:
431440
ConstFoldExpr ()
432441
: ConstFoldBase (), folded (ctx->get_backend ()->error_expression ())
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn main() {
2+
const A: [i32; 3] = [1, 2, 3];
3+
const B: i32 = A[1];
4+
const C: usize = 42;
5+
const D: i32 = 7;
6+
7+
let _a = C;
8+
let _b: [i32; C] = [0; C];
9+
let _c = B + D;
10+
}

0 commit comments

Comments
 (0)