Skip to content

Commit fe9e3ef

Browse files
committed
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 <[email protected]>
1 parent 997fc94 commit fe9e3ef

File tree

2 files changed

+567
-1
lines changed

2 files changed

+567
-1
lines changed

gcc/rust/backend/rust-compile-expr.cc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1341,9 +1341,23 @@ CompileExpr::visit (HIR::MatchExpr &expr)
13411341
ctx->add_statement (ret_var_stmt);
13421342

13431343
// lets compile the scrutinee expression
1344-
tree match_scrutinee_expr
1344+
tree match_scrutinee_rval
13451345
= CompileExpr::Compile (expr.get_scrutinee_expr ().get (), ctx);
13461346

1347+
Bvariable *match_scrutinee_tmp_var = ctx->get_backend ()->temporary_variable (
1348+
fnctx.fndecl, enclosing_scope, TREE_TYPE (match_scrutinee_rval), NULL,
1349+
is_address_taken, expr.get_locus (), &ret_var_stmt);
1350+
ctx->add_statement (ret_var_stmt);
1351+
1352+
tree match_scrutinee_expr = match_scrutinee_tmp_var->get_tree (
1353+
expr.get_scrutinee_expr ()->get_locus ());
1354+
1355+
tree assignment
1356+
= ctx->get_backend ()->assignment_statement (match_scrutinee_expr,
1357+
match_scrutinee_rval,
1358+
expr.get_locus ());
1359+
ctx->add_statement (assignment);
1360+
13471361
tree match_scrutinee_expr_qualifier_expr;
13481362
if (TyTy::is_primitive_type_kind (scrutinee_kind))
13491363
{

0 commit comments

Comments
 (0)