aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust
diff options
context:
space:
mode:
authorPhilip Herron <herron.philip@googlemail.com>2023-09-04 15:28:46 +0100
committerPhilip Herron <philip.herron@embecosm.com>2023-09-04 16:56:29 +0000
commitfe9e3efdc04e1f99f7089b8cc03b30af5e9d45dd (patch)
tree357623e6b4ef960126aabce78ed1ea7855940031 /gcc/rust
parent997fc94e109d2c2ca9731c12001f29cded32846b (diff)
downloadgcc-fe9e3efdc04e1f99f7089b8cc03b30af5e9d45dd.zip
gcc-fe9e3efdc04e1f99f7089b8cc03b30af5e9d45dd.tar.gz
gcc-fe9e3efdc04e1f99f7089b8cc03b30af5e9d45dd.tar.bz2
gccrs: Fix match-expression code-gen
We were massing the match scruitinee expression as a way to access the result of the expression. This is wrong and needs to be stored in a temporary otherwise it will cause the code to be regnerated for each time it is used. This is not an issue in the case where the expression is only used once. Fixes #1895 gcc/rust/ChangeLog: * backend/rust-compile-expr.cc (CompileExpr::visit): use a temp for the value gcc/testsuite/ChangeLog: * rust/execute/torture/iter1.rs: New test. Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Diffstat (limited to 'gcc/rust')
-rw-r--r--gcc/rust/backend/rust-compile-expr.cc16
1 files changed, 15 insertions, 1 deletions
diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc
index db9150c..35acce1 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -1341,9 +1341,23 @@ CompileExpr::visit (HIR::MatchExpr &expr)
ctx->add_statement (ret_var_stmt);
// lets compile the scrutinee expression
- tree match_scrutinee_expr
+ tree match_scrutinee_rval
= CompileExpr::Compile (expr.get_scrutinee_expr ().get (), ctx);
+ Bvariable *match_scrutinee_tmp_var = ctx->get_backend ()->temporary_variable (
+ fnctx.fndecl, enclosing_scope, TREE_TYPE (match_scrutinee_rval), NULL,
+ is_address_taken, expr.get_locus (), &ret_var_stmt);
+ ctx->add_statement (ret_var_stmt);
+
+ tree match_scrutinee_expr = match_scrutinee_tmp_var->get_tree (
+ expr.get_scrutinee_expr ()->get_locus ());
+
+ tree assignment
+ = ctx->get_backend ()->assignment_statement (match_scrutinee_expr,
+ match_scrutinee_rval,
+ expr.get_locus ());
+ ctx->add_statement (assignment);
+
tree match_scrutinee_expr_qualifier_expr;
if (TyTy::is_primitive_type_kind (scrutinee_kind))
{