Skip to content

Commit

Permalink
gccrs: Fix match-expression code-gen
Browse files Browse the repository at this point in the history
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 Rust-GCC#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]>
  • Loading branch information
philberty authored and CohenArthur committed Jan 16, 2024
1 parent 83feca3 commit 3afb921
Show file tree
Hide file tree
Showing 2 changed files with 567 additions and 1 deletion.
16 changes: 15 additions & 1 deletion gcc/rust/backend/rust-compile-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
Loading

0 comments on commit 3afb921

Please sign in to comment.