From fe9e3efdc04e1f99f7089b8cc03b30af5e9d45dd Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Mon, 4 Sep 2023 15:28:46 +0100 Subject: 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 --- gcc/rust/backend/rust-compile-expr.cc | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'gcc/rust') 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)) { -- cgit v1.1