]> git.ipfire.org Git - thirdparty/gcc.git/commit
rust: Desugar IfLet* into MatchExpr
authorMarc Poulhiès <dkm@kataplop.net>
Wed, 12 Jun 2024 19:58:26 +0000 (21:58 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Fri, 21 Mar 2025 11:32:54 +0000 (12:32 +0100)
commitaa0e2abc57f4f43198e2b072a84c34c476aa2fad
tree77c6436b01133af70c4453cc4c42d7d936e0d10a
parentea163c6fe0021e0c6a6d984ebe62d77e31a44c51
rust: Desugar IfLet* into MatchExpr

Replace the "regular" AST->HIR lowering for IfLet* with a desugaring
into a MatchExpr.

Desugar a simple if let:

   if let Some(y) = some_value {
     bar();
   }

into:

   match some_value {
     Some(y) => {bar();},
     _ => ()
   }

Same applies for IfLetExprConseqElse (if let with an else block).

Desugar:

   if let Some(y) = some_value {
     bar();
   } else {
     baz();
   }

into:

   match some_value {
     Some(y) => {bar();},
     _ => {baz();}
   }

Fixes https://github.com/Rust-GCC/gccrs/issues/1177

gcc/rust/ChangeLog:

* backend/rust-compile-block.h: Adjust after removal of
HIR::IfLetExpr and HIR::IfLetExprConseqElse.
* backend/rust-compile-expr.h: Likewise.
* checks/errors/borrowck/rust-bir-builder-expr-stmt.cc
(ExprStmtBuilder::visit): Likewise.
* checks/errors/borrowck/rust-bir-builder-expr-stmt.h: Likewise.
* checks/errors/borrowck/rust-bir-builder-lazyboolexpr.h:
Likewise.
* checks/errors/borrowck/rust-bir-builder-struct.h: Likewise.
* checks/errors/borrowck/rust-function-collector.h: Likewise.
* checks/errors/privacy/rust-privacy-reporter.cc
(PrivacyReporter::visit): Likewise.
* checks/errors/privacy/rust-privacy-reporter.h: Likewise.
* checks/errors/rust-const-checker.cc (ConstChecker::visit):
Likewise.
* checks/errors/rust-const-checker.h: Likewise.
* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit):
Likewise.
* checks/errors/rust-unsafe-checker.h: Likewise.
* hir/rust-ast-lower-block.h (ASTLoweringIfLetBlock::translate):
Change return type.
* hir/rust-ast-lower.cc (ASTLoweringIfLetBlock::desugar_iflet):
New.
(ASTLoweringIfLetBlock::visit(AST::IfLetExpr &)): Adjust and use
desugar_iflet.
* hir/rust-ast-lower.h: Add comment.
* hir/rust-hir-dump.cc (Dump::do_ifletexpr): Remove.
(Dump::visit(IfLetExpr&)): Remove.
(Dump::visit(IfLetExprConseqElse&)): Remove.
* hir/rust-hir-dump.h (Dump::do_ifletexpr): Remove.
(Dump::visit(IfLetExpr&)): Remove.
(Dump::visit(IfLetExprConseqElse&)): Remove.
* hir/tree/rust-hir-expr.h (class IfLetExpr): Remove.
(class IfLetExprConseqElse): Remove.
* hir/tree/rust-hir-full-decls.h (class IfLetExpr): Remove.
(class IfLetExprConseqElse): Remove.
* hir/tree/rust-hir-visitor.h: Adjust after removal of
HIR::IfLetExpr and HIR::IfLetExprConseqElse.
* hir/tree/rust-hir.cc (IfLetExpr::as_string): Remove.
(IfLetExprConseqElse::as_string): Remove.
(IfLetExpr::accept_vis): Remove.
(IfLetExprConseqElse::accept_vis): Remove.
* hir/tree/rust-hir.h: Adjust after removal of HIR::IfLetExpr and
HIR::IfLetExprConseqElse.
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit):
Likewise.
* typecheck/rust-hir-type-check-expr.h: Likewise.
* checks/errors/rust-hir-pattern-analysis.cc
(PatternChecker::visit (IfLetExpr &)): Remove.
(PatternChecker::visit (IfLetExprConseqElse &)): Remove.
* checks/errors/rust-hir-pattern-analysis.h (visit(IfLetExpr &)): Remove.
(visit(IfLetExprConseqElse &)): Remove.

gcc/testsuite/ChangeLog:

* rust/compile/if_let_expr.rs: Adjust.
* rust/compile/if_let_expr_simple.rs: New test.
* rust/compile/iflet.rs: New test.
* rust/execute/torture/iflet.rs: New test.
* rust/compile/nr2/exclude: Add iflet.rs and if_let_expr_simple.rs

Signed-off-by: Marc Poulhiès <dkm@kataplop.net>
32 files changed:
gcc/rust/backend/rust-compile-block.h
gcc/rust/backend/rust-compile-expr.h
gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc
gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.h
gcc/rust/checks/errors/borrowck/rust-bir-builder-lazyboolexpr.h
gcc/rust/checks/errors/borrowck/rust-bir-builder-struct.h
gcc/rust/checks/errors/borrowck/rust-function-collector.h
gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
gcc/rust/checks/errors/privacy/rust-privacy-reporter.h
gcc/rust/checks/errors/rust-const-checker.cc
gcc/rust/checks/errors/rust-const-checker.h
gcc/rust/checks/errors/rust-hir-pattern-analysis.cc
gcc/rust/checks/errors/rust-hir-pattern-analysis.h
gcc/rust/checks/errors/rust-unsafe-checker.cc
gcc/rust/checks/errors/rust-unsafe-checker.h
gcc/rust/hir/rust-ast-lower-block.h
gcc/rust/hir/rust-ast-lower.cc
gcc/rust/hir/rust-ast-lower.h
gcc/rust/hir/rust-hir-dump.cc
gcc/rust/hir/rust-hir-dump.h
gcc/rust/hir/tree/rust-hir-expr.h
gcc/rust/hir/tree/rust-hir-full-decls.h
gcc/rust/hir/tree/rust-hir-visitor.h
gcc/rust/hir/tree/rust-hir.cc
gcc/rust/hir/tree/rust-hir.h
gcc/rust/typecheck/rust-hir-type-check-expr.cc
gcc/rust/typecheck/rust-hir-type-check-expr.h
gcc/testsuite/rust/compile/if_let_expr.rs
gcc/testsuite/rust/compile/if_let_expr_simple.rs [new file with mode: 0644]
gcc/testsuite/rust/compile/iflet.rs [new file with mode: 0644]
gcc/testsuite/rust/compile/nr2/exclude
gcc/testsuite/rust/execute/torture/iflet.rs [new file with mode: 0644]