diff options
author | Philip Herron <philip.herron@embecosm.com> | 2021-10-28 11:07:13 +0100 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2021-10-28 11:07:13 +0100 |
commit | 0114e3e5b3b222c190a640bdeda8e781042389c4 (patch) | |
tree | 4afc284f874d636b620e233f08050d607faecc59 /gcc | |
parent | 657a9735339f6e5b0723bc24f74ad55d78daae8e (diff) | |
download | gcc-0114e3e5b3b222c190a640bdeda8e781042389c4.zip gcc-0114e3e5b3b222c190a640bdeda8e781042389c4.tar.gz gcc-0114e3e5b3b222c190a640bdeda8e781042389c4.tar.bz2 |
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};
}
}
```
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/typecheck/rust-hir-const-fold.h | 9 | ||||
-rw-r--r-- | gcc/testsuite/rust/compile/torture/constant3.rs | 10 |
2 files changed, 19 insertions, 0 deletions
diff --git a/gcc/rust/typecheck/rust-hir-const-fold.h b/gcc/rust/typecheck/rust-hir-const-fold.h index b61ce7b..2bf0f2c 100644 --- a/gcc/rust/typecheck/rust-hir-const-fold.h +++ b/gcc/rust/typecheck/rust-hir-const-fold.h @@ -427,6 +427,15 @@ public: = ctx->get_backend ()->negation_expression (op, negated_expr, location); } + void visit (HIR::ArrayIndexExpr &expr) override + { + Bexpression *array = ConstFoldExpr::fold (expr.get_array_expr ()); + Bexpression *index = ConstFoldExpr::fold (expr.get_index_expr ()); + + folded = ctx->get_backend ()->array_index_expression (array, index, + expr.get_locus ()); + } + private: ConstFoldExpr () : ConstFoldBase (), folded (ctx->get_backend ()->error_expression ()) diff --git a/gcc/testsuite/rust/compile/torture/constant3.rs b/gcc/testsuite/rust/compile/torture/constant3.rs new file mode 100644 index 0000000..d2f1dd5 --- /dev/null +++ b/gcc/testsuite/rust/compile/torture/constant3.rs @@ -0,0 +1,10 @@ +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; +} |