]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
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)
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]

index d226b4be9f4e5f981cb1f7de3d74ce8f3187caa4..6616882894f089967fe5547c82e137ab4616b5c2 100644 (file)
@@ -96,8 +96,6 @@ public:
   void visit (HIR::LoopExpr &) override {}
   void visit (HIR::WhileLoopExpr &) override {}
   void visit (HIR::WhileLetLoopExpr &) override {}
-  void visit (HIR::IfLetExpr &) override {}
-  void visit (HIR::IfLetExprConseqElse &) override {}
   void visit (HIR::MatchExpr &) override {}
   void visit (HIR::AwaitExpr &) override {}
   void visit (HIR::AsyncBlockExpr &) override {}
@@ -180,8 +178,6 @@ public:
   void visit (HIR::LoopExpr &) override {}
   void visit (HIR::WhileLoopExpr &) override {}
   void visit (HIR::WhileLetLoopExpr &) override {}
-  void visit (HIR::IfLetExpr &) override {}
-  void visit (HIR::IfLetExprConseqElse &) override {}
   void visit (HIR::MatchExpr &) override {}
   void visit (HIR::AwaitExpr &) override {}
   void visit (HIR::AsyncBlockExpr &) override {}
index ef907d1ca6255cc97c741074448598cc871253b8..2312375ff49a9b4e25bce75dc2ac9c5735ad4f9d 100644 (file)
@@ -77,8 +77,6 @@ public:
   // TODO
   // these need to be sugared in the HIR to if statements and a match
   void visit (HIR::WhileLetLoopExpr &) override {}
-  void visit (HIR::IfLetExpr &) override {}
-  void visit (HIR::IfLetExprConseqElse &) override {}
 
   // lets not worry about async yet....
   void visit (HIR::AwaitExpr &) override {}
index 1713bf6fcf8d47e611c98128abeec06312374ae8..df02c4b5e61bf19b254fd59c42d36df488fdd1f6 100644 (file)
@@ -611,18 +611,6 @@ ExprStmtBuilder::visit (HIR::IfExprConseqElse &expr)
     add_jump (else_end_bb, final_start_bb);
 }
 
-void
-ExprStmtBuilder::visit (HIR::IfLetExpr &expr)
-{
-  rust_sorry_at (expr.get_locus (), "if let expressions are not supported");
-}
-
-void
-ExprStmtBuilder::visit (HIR::IfLetExprConseqElse &expr)
-{
-  rust_sorry_at (expr.get_locus (), "if let expressions are not supported");
-}
-
 void
 ExprStmtBuilder::visit (HIR::MatchExpr &expr)
 {
index 574f0f36fdd727eb24390f8343a9d0051e0041c6..daedb68074494c904e937cd6111b7ac30f04208b 100644 (file)
@@ -101,8 +101,6 @@ protected: // Expr
   void visit (HIR::IfExprConseqElse &expr) override;
   void visit (HIR::InlineAsm &expr) override;
 
-  void visit (HIR::IfLetExpr &expr) override;
-  void visit (HIR::IfLetExprConseqElse &expr) override;
   void visit (HIR::MatchExpr &expr) override;
   void visit (HIR::AwaitExpr &expr) override;
   void visit (HIR::AsyncBlockExpr &expr) override;
index f4995327d87c16b941c0a9334dd0a24369a48b3d..8c01c501c36c2609f3a6d97debd4d5b1b4155984 100644 (file)
@@ -193,14 +193,6 @@ public:
   {
     return_place (ExprStmtBuilder (ctx).build (expr), expr.get_locus ());
   }
-  void visit (HIR::IfLetExpr &expr) override
-  {
-    return_place (ExprStmtBuilder (ctx).build (expr), expr.get_locus ());
-  }
-  void visit (HIR::IfLetExprConseqElse &expr) override
-  {
-    return_place (ExprStmtBuilder (ctx).build (expr), expr.get_locus ());
-  }
   void visit (HIR::MatchExpr &expr) override
   {
     return_place (ExprStmtBuilder (ctx).build (expr), expr.get_locus ());
index 53346bfe7370c042f1731f3704f5c51e965b3bf5..fefdd534cb16a1a4f3983f1b349dad144a0e5925 100644 (file)
@@ -149,8 +149,6 @@ protected:
   void visit (HIR::WhileLetLoopExpr &expr) override { rust_unreachable (); }
   void visit (HIR::IfExpr &expr) override { rust_unreachable (); }
   void visit (HIR::IfExprConseqElse &expr) override { rust_unreachable (); }
-  void visit (HIR::IfLetExpr &expr) override { rust_unreachable (); }
-  void visit (HIR::IfLetExprConseqElse &expr) override { rust_unreachable (); }
   void visit (HIR::MatchExpr &expr) override { rust_unreachable (); }
   void visit (HIR::AwaitExpr &expr) override { rust_unreachable (); }
   void visit (HIR::AsyncBlockExpr &expr) override { rust_unreachable (); }
index 51109d7dbdc7a28b65d22b817662f0441e296dcc..bea438c54451321e6c488e62be55134f4583a649 100644 (file)
@@ -119,8 +119,6 @@ public:
   void visit (HIR::WhileLetLoopExpr &expr) override {}
   void visit (HIR::IfExpr &expr) override {}
   void visit (HIR::IfExprConseqElse &expr) override {}
-  void visit (HIR::IfLetExpr &expr) override {}
-  void visit (HIR::IfLetExprConseqElse &expr) override {}
   void visit (HIR::MatchExpr &expr) override {}
   void visit (HIR::AwaitExpr &expr) override {}
   void visit (HIR::AsyncBlockExpr &expr) override {}
index 01d8ea5e87bb90df1d5492d56fd70b916f7ce611..1ee2097b3835f87ba0d53a76da7832a12735a350 100644 (file)
@@ -594,21 +594,6 @@ PrivacyReporter::visit (HIR::IfExprConseqElse &expr)
   expr.get_else_block ()->accept_vis (*this);
 }
 
-void
-PrivacyReporter::visit (HIR::IfLetExpr &)
-{
-  // TODO: We need to visit the if_let_expr
-  // TODO: We need to visit the block as well
-}
-
-void
-PrivacyReporter::visit (HIR::IfLetExprConseqElse &)
-{
-  // TODO: We need to visit the if_let_expr
-  // TODO: We need to visit the if_block as well
-  // TODO: We need to visit the else_block as well
-}
-
 void
 PrivacyReporter::visit (HIR::MatchExpr &expr)
 {
index c4f94ab3ed70ce0f750b11dcfd14036032e19b71..b9cc230d5db16b7ef161dcd207dba04f9499e72b 100644 (file)
@@ -121,8 +121,6 @@ types
   virtual void visit (HIR::WhileLetLoopExpr &expr);
   virtual void visit (HIR::IfExpr &expr);
   virtual void visit (HIR::IfExprConseqElse &expr);
-  virtual void visit (HIR::IfLetExpr &expr);
-  virtual void visit (HIR::IfLetExprConseqElse &expr);
   virtual void visit (HIR::MatchExpr &expr);
   virtual void visit (HIR::AwaitExpr &expr);
   virtual void visit (HIR::AsyncBlockExpr &expr);
index 84c09dd307e28fa87551a5ba2e38ddde01e94855..1e0ebd2979895d1a2cf612eb11e2ed6ee1a3fe1a 100644 (file)
@@ -511,22 +511,6 @@ ConstChecker::visit (IfExprConseqElse &expr)
   expr.get_else_block ()->accept_vis (*this);
 }
 
-void
-ConstChecker::visit (IfLetExpr &expr)
-{
-  expr.get_scrutinee_expr ()->accept_vis (*this);
-  expr.get_if_block ()->accept_vis (*this);
-}
-
-void
-ConstChecker::visit (IfLetExprConseqElse &expr)
-{
-  expr.get_scrutinee_expr ()->accept_vis (*this);
-  expr.get_if_block ()->accept_vis (*this);
-
-  // TODO: Visit else expression
-}
-
 void
 ConstChecker::visit (MatchExpr &expr)
 {
index 8890761e61d1a40949e726cc87edac076cf41c76..9a618b8f96d03f0e93ad672a3d8b1ca0634c75d9 100644 (file)
@@ -128,8 +128,6 @@ private:
   virtual void visit (WhileLetLoopExpr &expr) override;
   virtual void visit (IfExpr &expr) override;
   virtual void visit (IfExprConseqElse &expr) override;
-  virtual void visit (IfLetExpr &expr) override;
-  virtual void visit (IfLetExprConseqElse &expr) override;
   virtual void visit (MatchExpr &expr) override;
   virtual void visit (AwaitExpr &expr) override;
   virtual void visit (AsyncBlockExpr &expr) override;
index f46f429e93064a0676d319dd052ec372edf52459..8fb795b7b837665ba413d1bca6fe973ecb11f149 100644 (file)
@@ -389,22 +389,6 @@ PatternChecker::visit (IfExprConseqElse &expr)
   expr.get_else_block ()->accept_vis (*this);
 }
 
-void
-PatternChecker::visit (IfLetExpr &expr)
-{
-  expr.get_scrutinee_expr ()->accept_vis (*this);
-  expr.get_if_block ()->accept_vis (*this);
-}
-
-void
-PatternChecker::visit (IfLetExprConseqElse &expr)
-{
-  expr.get_scrutinee_expr ()->accept_vis (*this);
-  expr.get_if_block ()->accept_vis (*this);
-
-  expr.get_else_block ()->accept_vis (*this);
-}
-
 void
 PatternChecker::visit (MatchExpr &expr)
 {
index 1af02baa24b142b21791c642f5988a9b28671d19..9c43d4143d2ae7efbd0bd7cbc97a74249079a879 100644 (file)
@@ -102,8 +102,6 @@ private:
   virtual void visit (WhileLetLoopExpr &expr) override;
   virtual void visit (IfExpr &expr) override;
   virtual void visit (IfExprConseqElse &expr) override;
-  virtual void visit (IfLetExpr &expr) override;
-  virtual void visit (IfLetExprConseqElse &expr) override;
   virtual void visit (HIR::MatchExpr &expr) override;
   virtual void visit (AwaitExpr &expr) override;
   virtual void visit (AsyncBlockExpr &expr) override;
index 9ab18e2f65648c8d0ededd2d358f7b39e81e14c7..ee6654b4dd25345f359277c9ac7e8e395334a502 100644 (file)
@@ -633,22 +633,6 @@ UnsafeChecker::visit (IfExprConseqElse &expr)
   expr.get_else_block ()->accept_vis (*this);
 }
 
-void
-UnsafeChecker::visit (IfLetExpr &expr)
-{
-  expr.get_scrutinee_expr ()->accept_vis (*this);
-  expr.get_if_block ()->accept_vis (*this);
-}
-
-void
-UnsafeChecker::visit (IfLetExprConseqElse &expr)
-{
-  expr.get_scrutinee_expr ()->accept_vis (*this);
-  expr.get_if_block ()->accept_vis (*this);
-
-  // TODO: Visit else expression
-}
-
 void
 UnsafeChecker::visit (MatchExpr &expr)
 {
index 1fa1fe072da8aa7ce20503f379decdebe3dedd10..96ccdc03c53c6dc6ea641fda5c7fc7c2a9b60a91 100644 (file)
@@ -110,8 +110,6 @@ private:
   virtual void visit (WhileLetLoopExpr &expr) override;
   virtual void visit (IfExpr &expr) override;
   virtual void visit (IfExprConseqElse &expr) override;
-  virtual void visit (IfLetExpr &expr) override;
-  virtual void visit (IfLetExprConseqElse &expr) override;
   virtual void visit (MatchExpr &expr) override;
   virtual void visit (AwaitExpr &expr) override;
   virtual void visit (AsyncBlockExpr &expr) override;
index be6ca64b68e9c8b4e7a930975bdbed0b7ccef0fb..a39c010740eaa3e00637de090a37c20245fe5c73 100644 (file)
@@ -115,7 +115,7 @@ class ASTLoweringIfLetBlock : public ASTLoweringBase
   using Rust::HIR::ASTLoweringBase::visit;
 
 public:
-  static HIR::IfLetExpr *translate (AST::IfLetExpr &expr)
+  static HIR::MatchExpr *translate (AST::IfLetExpr &expr)
   {
     ASTLoweringIfLetBlock resolver;
     expr.accept_vis (resolver);
@@ -135,7 +135,10 @@ public:
 private:
   ASTLoweringIfLetBlock () : ASTLoweringBase (), translated (nullptr) {}
 
-  HIR::IfLetExpr *translated;
+  void desugar_iflet (AST::IfLetExpr &, HIR::Expr **, HIR::Expr *,
+                     std::vector<HIR::MatchCase> &);
+
+  HIR::MatchExpr *translated;
 };
 
 class ASTLoweringExprWithBlock : public ASTLoweringBase
@@ -149,9 +152,7 @@ public:
     ASTLoweringExprWithBlock resolver;
     expr.accept_vis (resolver);
     if (resolver.translated != nullptr)
-      {
-       resolver.mappings.insert_hir_expr (resolver.translated);
-      }
+      resolver.mappings.insert_hir_expr (resolver.translated);
 
     *terminated = resolver.terminated;
     return resolver.translated;
index 5a5c93fc3709a87b08b04477608c606e525136d3..c5e2250f29e03c7b9db273f48588066a7faddb6f 100644 (file)
@@ -24,6 +24,8 @@
 #include "rust-ast-lower-type.h"
 #include "rust-ast-lower-pattern.h"
 #include "rust-ast-lower-struct-field-expr.h"
+#include "rust-expr.h"
+#include "rust-hir-expr.h"
 
 namespace Rust {
 namespace HIR {
@@ -198,62 +200,144 @@ ASTLoweringIfBlock::visit (AST::IfExprConseqElse &expr)
     std::unique_ptr<HIR::ExprWithBlock> (else_block), expr.get_locus ());
 }
 
+/**
+ *  Lowers the common part "if let 'pattern' = 'expr' { 'if_block' }" of
+ *  IfLetExpr[ConseqElse]:
+ *  - 'expr' is lowered into *BRANCH_VALUE
+ *  - 'pattern' + 'if_block' are lowered and resulting ARM pushed in MATCH_ARMS
+ *  - 'KASE_ELSE_EXPR' is the lowered HIR to be used in the else part.
+ *
+ *  Looks like:
+ *
+ *  match (expr) {
+ *    pattern => {if_block}
+ *    _ => kase_else_expr
+ *  }
+ *
+ */
 void
-ASTLoweringIfLetBlock::visit (AST::IfLetExpr &expr)
+ASTLoweringIfLetBlock::desugar_iflet (AST::IfLetExpr &expr,
+                                     HIR::Expr **branch_value,
+                                     HIR::Expr *kase_else_expr,
+                                     std::vector<HIR::MatchCase> &match_arms)
 {
-  std::vector<std::unique_ptr<HIR::Pattern>> patterns;
+  HIR::Expr *kase_expr;
+  std::vector<std::unique_ptr<HIR::Pattern>> match_arm_patterns;
+
+  *branch_value = ASTLoweringExpr::translate (expr.get_value_expr ());
+  kase_expr = ASTLoweringExpr::translate (expr.get_if_block ());
+
+  // (stable) if let only accepts a single pattern, but (unstable) if let chains
+  // need more than one pattern.
+  // We don't support if let chains, so only support a single pattern.
+  rust_assert (expr.get_patterns ().size () == 1);
+
   for (auto &pattern : expr.get_patterns ())
     {
       HIR::Pattern *ptrn = ASTLoweringPattern::translate (*pattern);
-      patterns.push_back (std::unique_ptr<HIR::Pattern> (ptrn));
+      match_arm_patterns.push_back (std::unique_ptr<HIR::Pattern> (ptrn));
     }
-  HIR::Expr *value_ptr = ASTLoweringExpr::translate (expr.get_value_expr ());
 
-  bool ignored_terminated = false;
-  HIR::BlockExpr *block
-    = ASTLoweringBlock::translate (expr.get_if_block (), &ignored_terminated);
+  // The match arm corresponding to the if let pattern when it matches.
+  HIR::MatchArm arm (std::move (match_arm_patterns), expr.get_locus (), nullptr,
+                    {});
 
   auto crate_num = mappings.get_current_crate ();
   Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
                                 mappings.get_next_hir_id (crate_num),
                                 UNKNOWN_LOCAL_DEFID);
 
-  translated = new HIR::IfLetExpr (mapping, std::move (patterns),
-                                  std::unique_ptr<HIR::Expr> (value_ptr),
-                                  std::unique_ptr<HIR::BlockExpr> (block),
-                                  expr.get_locus ());
+  HIR::MatchCase kase (std::move (mapping), std::move (arm),
+                      std::unique_ptr<HIR::Expr> (kase_expr));
+  match_arms.push_back (std::move (kase));
+
+  // The default match arm when the if let pattern does not match
+  std::vector<std::unique_ptr<HIR::Pattern>> match_arm_patterns_wildcard;
+  Analysis::NodeMapping mapping_default (crate_num, expr.get_node_id (),
+                                        mappings.get_next_hir_id (crate_num),
+                                        UNKNOWN_LOCAL_DEFID);
+
+  std::unique_ptr<HIR::WildcardPattern> wc
+    = std::unique_ptr<HIR::WildcardPattern> (
+      new HIR::WildcardPattern (mapping_default, expr.get_locus ()));
+
+  match_arm_patterns_wildcard.push_back (std::move (wc));
+
+  HIR::MatchArm arm_default (std::move (match_arm_patterns_wildcard),
+                            expr.get_locus (), nullptr, {});
+
+  HIR::MatchCase kase_else (std::move (mapping_default),
+                           std::move (arm_default),
+                           std::unique_ptr<HIR::Expr> (kase_else_expr));
+  match_arms.push_back (std::move (kase_else));
 }
 
 void
-ASTLoweringIfLetBlock::visit (AST::IfLetExprConseqElse &expr)
+ASTLoweringIfLetBlock::visit (AST::IfLetExpr &expr)
 {
-  std::vector<std::unique_ptr<HIR::Pattern>> patterns;
-  for (auto &pattern : expr.get_patterns ())
-    {
-      HIR::Pattern *ptrn = ASTLoweringPattern::translate (*pattern);
-      patterns.push_back (std::unique_ptr<HIR::Pattern> (ptrn));
-    }
-  HIR::Expr *value_ptr = ASTLoweringExpr::translate (expr.get_value_expr ());
+  // Desugar:
+  //   if let Some(y) = some_value {
+  //     bar();
+  //   }
+  //
+  //   into:
+  //
+  //   match some_value {
+  //     Some(y) => {bar();},
+  //     _ => ()
+  //   }
+
+  HIR::Expr *branch_value;
 
-  bool ignored_terminated = false;
-  HIR::BlockExpr *block
-    = ASTLoweringBlock::translate (expr.get_if_block (), &ignored_terminated);
+  std::vector<HIR::MatchCase> match_arms;
+  auto crate_num = mappings.get_current_crate ();
+  Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
+                                mappings.get_next_hir_id (crate_num),
+                                UNKNOWN_LOCAL_DEFID);
 
-  HIR::ExprWithBlock *else_block
-    = ASTLoweringExprWithBlock::translate (expr.get_else_block (),
-                                          &ignored_terminated);
+  HIR::TupleExpr *unit
+    = new HIR::TupleExpr (mapping, {}, {}, {}, expr.get_locus ());
+
+  desugar_iflet (expr, &branch_value, unit, match_arms);
 
-  rust_assert (else_block);
+  translated
+    = new HIR::MatchExpr (mapping, std::unique_ptr<HIR::Expr> (branch_value),
+                         std::move (match_arms), {}, {}, expr.get_locus ());
+}
+
+void
+ASTLoweringIfLetBlock::visit (AST::IfLetExprConseqElse &expr)
+{
+  // desugar:
+  //   if let Some(y) = some_value {
+  //     bar();
+  //   } else {
+  //     baz();
+  //   }
+  //
+  //   into
+  //   match some_value {
+  //     Some(y) => {bar();},
+  //     _ => {baz();}
+  //   }
+  //
+
+  HIR::Expr *branch_value;
+  std::vector<HIR::MatchCase> match_arms;
+
+  HIR::Expr *kase_else_expr
+    = ASTLoweringExpr::translate (expr.get_else_block ());
+
+  desugar_iflet (expr, &branch_value, kase_else_expr, match_arms);
 
   auto crate_num = mappings.get_current_crate ();
   Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
                                 mappings.get_next_hir_id (crate_num),
                                 UNKNOWN_LOCAL_DEFID);
 
-  translated = new HIR::IfLetExprConseqElse (
-    mapping, std::move (patterns), std::unique_ptr<HIR::Expr> (value_ptr),
-    std::unique_ptr<HIR::BlockExpr> (block),
-    std::unique_ptr<HIR::ExprWithBlock> (else_block), expr.get_locus ());
+  translated
+    = new HIR::MatchExpr (mapping, std::unique_ptr<HIR::Expr> (branch_value),
+                         std::move (match_arms), {}, {}, expr.get_locus ());
 }
 
 // rust-ast-lower-struct-field-expr.h
index 079ffa9af92d4bdec98a49d4762889f8e3f148bb..cc7408277a317e321052730a4bd8d93f9b237bec 100644 (file)
@@ -39,6 +39,11 @@ struct_field_name_exists (std::vector<HIR::StructField> &fields,
 Visibility
 translate_visibility (const AST::Visibility &vis);
 
+/**
+ * Main base class used for lowering AST to HIR.
+ *
+ * Every subclass should provide a translate() method that takes an AST node and
+ * lowers it to some HIR stored in the TRANSLATED member. */
 class ASTLowering
 {
 public:
index b44137732348d7cbedd61e02ef03d76d58773fe7..4fa028c07b8d792103602b81f7f7bd1ca3ab40f6 100644 (file)
@@ -463,17 +463,6 @@ Dump::do_baseloopexpr (BaseLoopExpr &e)
   visit_field ("loop_block", e.get_loop_block ());
 }
 
-void
-Dump::do_ifletexpr (IfLetExpr &e)
-{
-  do_expr (e);
-
-  visit_collection ("match_arm_patterns", e.get_patterns ());
-
-  visit_field ("value", e.get_scrutinee_expr ());
-  visit_field ("if_block", e.get_if_block ());
-}
-
 void
 Dump::do_struct (Struct &e)
 {
@@ -1441,23 +1430,6 @@ Dump::visit (IfExprConseqElse &e)
   end ("IfExprConseqElse");
 }
 
-void
-Dump::visit (IfLetExpr &e)
-{
-  begin ("IfLetExpr");
-  do_ifletexpr (e);
-  end ("IfLetExpr");
-}
-
-void
-Dump::visit (IfLetExprConseqElse &e)
-{
-  begin ("IfLetExprConseqElse");
-  do_ifletexpr (e);
-  visit_field ("else_block", e.get_else_block ());
-  end ("IfLetExprConseqElse");
-}
-
 void
 Dump::visit (MatchExpr &e)
 {
index b3a2020742fdb2a1f641e7786f91a418abc1e06a..8683dee9d6c03de56aadf6aa7a74ee7f1b0e5746 100644 (file)
@@ -80,7 +80,6 @@ private:
   void do_type (Type &);
   void do_expr (Expr &);
   void do_ifexpr (IfExpr &);
-  void do_ifletexpr (IfLetExpr &);
   void do_pathexpr (PathExpr &);
   void do_pathpattern (PathPattern &);
   void do_genericargs (GenericArgs &);
@@ -162,8 +161,6 @@ private:
   virtual void visit (WhileLetLoopExpr &) override;
   virtual void visit (IfExpr &) override;
   virtual void visit (IfExprConseqElse &) override;
-  virtual void visit (IfLetExpr &) override;
-  virtual void visit (IfLetExprConseqElse &) override;
 
   virtual void visit (MatchExpr &) override;
   virtual void visit (AwaitExpr &) override;
index 1ee106637084837e06ab7df4add0ff6db1c7d03d..a79aea5838617b79006da7017060f02a41ccd5a9 100644 (file)
@@ -32,7 +32,7 @@ namespace HIR {
 // TODO: inline?
 class LoopLabel /*: public Node*/
 {
-  Lifetime label; // or type LIFETIME_OR_LABEL
+  Lifetime label; // of type LIFETIME_OR_LABEL
 
   location_t locus;
 
@@ -3108,9 +3108,6 @@ protected:
   }
 };
 
-// forward decl for IfExpr
-class IfLetExpr;
-
 // Base if expression with no "else" or "if let" HIR node
 class IfExpr : public ExprWithBlock
 {
@@ -3258,177 +3255,6 @@ protected:
   }
 };
 
-// Basic "if let" expression HIR node with no else
-class IfLetExpr : public ExprWithBlock
-{
-  // MatchArmPatterns patterns;
-  std::vector<std::unique_ptr<Pattern> > match_arm_patterns; // inlined
-  std::unique_ptr<Expr> value;
-  std::unique_ptr<BlockExpr> if_block;
-
-  location_t locus;
-
-public:
-  std::string as_string () const override;
-
-  IfLetExpr (Analysis::NodeMapping mappings,
-            std::vector<std::unique_ptr<Pattern> > match_arm_patterns,
-            std::unique_ptr<Expr> value, std::unique_ptr<BlockExpr> if_block,
-            location_t locus)
-    : ExprWithBlock (std::move (mappings), AST::AttrVec ()),
-      match_arm_patterns (std::move (match_arm_patterns)),
-      value (std::move (value)), if_block (std::move (if_block)), locus (locus)
-  {}
-  // outer attributes not allowed on if let exprs either
-
-  // copy constructor with clone
-  IfLetExpr (IfLetExpr const &other)
-    : ExprWithBlock (other),
-      /*match_arm_patterns(other.match_arm_patterns),*/ value (
-       other.value->clone_expr ()),
-      if_block (other.if_block->clone_block_expr ()), locus (other.locus)
-  {
-    match_arm_patterns.reserve (other.match_arm_patterns.size ());
-    for (const auto &e : other.match_arm_patterns)
-      match_arm_patterns.push_back (e->clone_pattern ());
-  }
-
-  // overload assignment operator to clone
-  IfLetExpr &operator= (IfLetExpr const &other)
-  {
-    ExprWithBlock::operator= (other);
-    // match_arm_patterns = other.match_arm_patterns;
-    value = other.value->clone_expr ();
-    if_block = other.if_block->clone_block_expr ();
-    locus = other.locus;
-
-    match_arm_patterns.reserve (other.match_arm_patterns.size ());
-    for (const auto &e : other.match_arm_patterns)
-      match_arm_patterns.push_back (e->clone_pattern ());
-
-    return *this;
-  }
-
-  // move constructors
-  IfLetExpr (IfLetExpr &&other) = default;
-  IfLetExpr &operator= (IfLetExpr &&other) = default;
-
-  // Unique pointer custom clone function
-  std::unique_ptr<IfLetExpr> clone_if_let_expr () const
-  {
-    return std::unique_ptr<IfLetExpr> (clone_if_let_expr_impl ());
-  }
-
-  location_t get_locus () const override final { return locus; }
-
-  void accept_vis (HIRFullVisitor &vis) override;
-  void accept_vis (HIRExpressionVisitor &vis) override;
-
-  std::unique_ptr<Expr> &get_scrutinee_expr () { return value; }
-
-  std::vector<std::unique_ptr<Pattern> > &get_patterns ()
-  {
-    return match_arm_patterns;
-  }
-
-  std::unique_ptr<BlockExpr> &get_if_block () { return if_block; }
-
-  ExprType get_expression_type () const final override
-  {
-    return ExprType::IfLet;
-  }
-
-protected:
-  /* Use covariance to implement clone function as returning this object rather
-   * than base */
-  IfLetExpr *clone_expr_impl () const override { return new IfLetExpr (*this); }
-
-  /* Use covariance to implement clone function as returning this object rather
-   * than base */
-  IfLetExpr *clone_expr_with_block_impl () const override
-  {
-    return new IfLetExpr (*this);
-  }
-
-  // Base clone function but still concrete as concrete base class
-  virtual IfLetExpr *clone_if_let_expr_impl () const
-  {
-    return new IfLetExpr (*this);
-  }
-};
-
-/* HIR node representing "if let" expression with an "else" expression at the
- * end */
-class IfLetExprConseqElse : public IfLetExpr
-{
-  std::unique_ptr<ExprWithBlock> else_block;
-
-public:
-  std::string as_string () const override;
-
-  IfLetExprConseqElse (
-    Analysis::NodeMapping mappings,
-    std::vector<std::unique_ptr<Pattern> > match_arm_patterns,
-    std::unique_ptr<Expr> value, std::unique_ptr<BlockExpr> if_block,
-    std::unique_ptr<ExprWithBlock> else_block, location_t locus)
-    : IfLetExpr (std::move (mappings), std::move (match_arm_patterns),
-                std::move (value), std::move (if_block), locus),
-      else_block (std::move (else_block))
-  {}
-  // outer attributes not allowed
-
-  // copy constructor with clone
-  IfLetExprConseqElse (IfLetExprConseqElse const &other)
-    : IfLetExpr (other), else_block (other.else_block->clone_expr_with_block ())
-  {}
-
-  // overload assignment operator to clone
-  IfLetExprConseqElse &operator= (IfLetExprConseqElse const &other)
-  {
-    IfLetExpr::operator= (other);
-    // match_arm_patterns = other.match_arm_patterns;
-    // value = other.value->clone_expr();
-    // if_block = other.if_block->clone_block_expr();
-    else_block = other.else_block->clone_expr_with_block ();
-    // outer_attrs = other.outer_attrs;
-
-    return *this;
-  }
-
-  // move constructors
-  IfLetExprConseqElse (IfLetExprConseqElse &&other) = default;
-  IfLetExprConseqElse &operator= (IfLetExprConseqElse &&other) = default;
-
-  void accept_vis (HIRFullVisitor &vis) override;
-  void accept_vis (HIRExpressionVisitor &vis) override;
-
-  void vis_else_block (HIRFullVisitor &vis) { else_block->accept_vis (vis); }
-
-  std::unique_ptr<ExprWithBlock> &get_else_block () { return else_block; }
-
-protected:
-  /* Use covariance to implement clone function as returning this object rather
-   * than base */
-  IfLetExprConseqElse *clone_expr_impl () const override
-  {
-    return new IfLetExprConseqElse (*this);
-  }
-
-  /* Use covariance to implement clone function as returning this object rather
-   * than base */
-  IfLetExprConseqElse *clone_expr_with_block_impl () const override
-  {
-    return new IfLetExprConseqElse (*this);
-  }
-
-  /* Use covariance to implement clone function as returning this object rather
-   * than base */
-  IfLetExprConseqElse *clone_if_let_expr_impl () const override
-  {
-    return new IfLetExprConseqElse (*this);
-  }
-};
-
 // Match arm expression
 struct MatchArm
 {
index 64be7bf93d1ff89fc09917fa57a1c59dce382ccf..c64c8bf65136e33aea8bbf1211aff430c9bca99b 100644 (file)
@@ -113,8 +113,6 @@ class WhileLoopExpr;
 class WhileLetLoopExpr;
 class IfExpr;
 class IfExprConseqElse;
-class IfLetExpr;
-class IfLetExprConseqElse;
 struct MatchArm;
 // class MatchCase;
 // class MatchCaseBlockExpr;
index e69e951d656d382339084cf3536f6883cc7dc572..9948a1555b9695e19afecbab7f903b0778ff0787 100644 (file)
@@ -81,8 +81,6 @@ public:
   virtual void visit (WhileLetLoopExpr &expr) = 0;
   virtual void visit (IfExpr &expr) = 0;
   virtual void visit (IfExprConseqElse &expr) = 0;
-  virtual void visit (IfLetExpr &expr) = 0;
-  virtual void visit (IfLetExprConseqElse &expr) = 0;
   virtual void visit (MatchExpr &expr) = 0;
   virtual void visit (AwaitExpr &expr) = 0;
   virtual void visit (AsyncBlockExpr &expr) = 0;
@@ -219,8 +217,6 @@ public:
   virtual void visit (WhileLetLoopExpr &) override {}
   virtual void visit (IfExpr &) override {}
   virtual void visit (IfExprConseqElse &) override {}
-  virtual void visit (IfLetExpr &) override {}
-  virtual void visit (IfLetExprConseqElse &) override {}
 
   virtual void visit (MatchExpr &) override {}
   virtual void visit (AwaitExpr &) override {}
@@ -448,8 +444,6 @@ public:
   virtual void visit (WhileLetLoopExpr &expr) = 0;
   virtual void visit (IfExpr &expr) = 0;
   virtual void visit (IfExprConseqElse &expr) = 0;
-  virtual void visit (IfLetExpr &expr) = 0;
-  virtual void visit (IfLetExprConseqElse &expr) = 0;
   virtual void visit (InlineAsm &expr) = 0;
   virtual void visit (MatchExpr &expr) = 0;
   virtual void visit (AwaitExpr &expr) = 0;
index f05e5065e62584596f59ffa35e14f168a54f6937..9de881f6c9c0b698c0a860244e2d624df4fee22b 100644 (file)
@@ -1571,41 +1571,6 @@ IfExprConseqElse::as_string () const
   return str;
 }
 
-std::string
-IfLetExpr::as_string () const
-{
-  std::string str ("IfLetExpr: ");
-
-  str += "\n Condition match arm patterns: ";
-  if (match_arm_patterns.empty ())
-    {
-      str += "none";
-    }
-  else
-    {
-      for (const auto &pattern : match_arm_patterns)
-       {
-         str += "\n  " + pattern->as_string ();
-       }
-    }
-
-  str += "\n Scrutinee expr: " + value->as_string ();
-
-  str += "\n If let block expr: " + if_block->as_string ();
-
-  return str;
-}
-
-std::string
-IfLetExprConseqElse::as_string () const
-{
-  std::string str = IfLetExpr::as_string ();
-
-  str += "\n Else expr: " + else_block->as_string ();
-
-  return str;
-}
-
 std::string
 RangeFromToInclExpr::as_string () const
 {
@@ -4149,18 +4114,6 @@ IfExprConseqElse::accept_vis (HIRFullVisitor &vis)
   vis.visit (*this);
 }
 
-void
-IfLetExpr::accept_vis (HIRFullVisitor &vis)
-{
-  vis.visit (*this);
-}
-
-void
-IfLetExprConseqElse::accept_vis (HIRFullVisitor &vis)
-{
-  vis.visit (*this);
-}
-
 void
 MatchExpr::accept_vis (HIRFullVisitor &vis)
 {
@@ -4911,18 +4864,6 @@ RangeFromToInclExpr::accept_vis (HIRExpressionVisitor &vis)
   vis.visit (*this);
 }
 
-void
-IfLetExprConseqElse::accept_vis (HIRExpressionVisitor &vis)
-{
-  vis.visit (*this);
-}
-
-void
-IfLetExpr::accept_vis (HIRExpressionVisitor &vis)
-{
-  vis.visit (*this);
-}
-
 void
 IfExprConseqElse::accept_vis (HIRExpressionVisitor &vis)
 {
index f8eb22db087a96b4daf6f6fa65b3f5ca958146f0..8ce5cf4d10259d4b0f464a4b465e8525887e2407 100644 (file)
@@ -300,7 +300,6 @@ public:
     UnsafeBlock,
     BaseLoop,
     If,
-    IfLet,
     Match,
     Await,
     AsyncBlock,
index dfdf85a141dbdc10645bf75991226839da9c4662..5b64daf33b753de4bf18d9d83494073d6b6db3e3 100644 (file)
@@ -503,67 +503,6 @@ TypeCheckExpr::visit (HIR::IfExprConseqElse &expr)
     }
 }
 
-void
-TypeCheckExpr::visit (HIR::IfLetExpr &expr)
-{
-  // this needs to perform a least upper bound coercion on the blocks and then
-  // unify the scruintee and arms
-  TyTy::BaseType *scrutinee_tyty
-    = TypeCheckExpr::Resolve (expr.get_scrutinee_expr ().get ());
-
-  for (auto &pattern : expr.get_patterns ())
-    {
-      TyTy::BaseType *kase_arm_ty
-       = TypeCheckPattern::Resolve (pattern.get (), scrutinee_tyty);
-
-      unify_site (expr.get_mappings ().get_hirid (),
-                 TyTy::TyWithLocation (scrutinee_tyty),
-                 TyTy::TyWithLocation (kase_arm_ty, pattern->get_locus ()),
-                 expr.get_locus ());
-    }
-
-  TypeCheckExpr::Resolve (expr.get_if_block ().get ());
-
-  infered = TyTy::TupleType::get_unit_type (expr.get_mappings ().get_hirid ());
-}
-
-void
-TypeCheckExpr::visit (HIR::IfLetExprConseqElse &expr)
-{
-  TyTy::BaseType *scrutinee_tyty
-    = TypeCheckExpr::Resolve (expr.get_scrutinee_expr ().get ());
-
-  for (auto &pattern : expr.get_patterns ())
-    {
-      TyTy::BaseType *kase_arm_ty
-       = TypeCheckPattern::Resolve (pattern.get (), scrutinee_tyty);
-
-      unify_site (expr.get_mappings ().get_hirid (),
-                 TyTy::TyWithLocation (scrutinee_tyty),
-                 TyTy::TyWithLocation (kase_arm_ty, pattern->get_locus ()),
-                 expr.get_locus ());
-    }
-
-  auto if_blk_resolved = TypeCheckExpr::Resolve (expr.get_if_block ().get ());
-  auto else_blk_resolved
-    = TypeCheckExpr::Resolve (expr.get_else_block ().get ());
-
-  if (if_blk_resolved->get_kind () == TyTy::NEVER)
-    infered = else_blk_resolved;
-  else if (else_blk_resolved->get_kind () == TyTy::NEVER)
-    infered = if_blk_resolved;
-  else
-    {
-      infered = unify_site (
-       expr.get_mappings ().get_hirid (),
-       TyTy::TyWithLocation (if_blk_resolved,
-                             expr.get_if_block ()->get_locus ()),
-       TyTy::TyWithLocation (else_blk_resolved,
-                             expr.get_else_block ()->get_locus ()),
-       expr.get_locus ());
-    }
-}
-
 void
 TypeCheckExpr::visit (HIR::UnsafeBlockExpr &expr)
 {
index 7192cf4dbda1ad681c759f3c4d033be12a32f324..997761144dba2ed098ca326055b2409e07e34f88 100644 (file)
@@ -45,8 +45,6 @@ public:
   void visit (HIR::NegationExpr &expr) override;
   void visit (HIR::IfExpr &expr) override;
   void visit (HIR::IfExprConseqElse &expr) override;
-  void visit (HIR::IfLetExpr &expr) override;
-  void visit (HIR::IfLetExprConseqElse &) override;
   void visit (HIR::BlockExpr &expr) override;
   void visit (HIR::UnsafeBlockExpr &expr) override;
   void visit (HIR::ArrayIndexExpr &expr) override;
index 7bab19a1ef03c3358c0056d51586ff7ddb3974c0..b0879e5fadbb495e8a1a15efd239a60044fc25be 100644 (file)
@@ -7,8 +7,9 @@ pub enum Option<T> {
 }
 
 fn main() {
-    let x = Option::Some(3); // { dg-warning "unused name" }
-    let a = if let Option::Some(1) = x {
+    let x = Option::Some(3);
+
+    let a = if let Option::Some(1) = x {// { dg-warning "unused name" }
         1
     } else if x == Option::Some(2) {
         2
diff --git a/gcc/testsuite/rust/compile/if_let_expr_simple.rs b/gcc/testsuite/rust/compile/if_let_expr_simple.rs
new file mode 100644 (file)
index 0000000..d7fb0af
--- /dev/null
@@ -0,0 +1,12 @@
+enum MyOption {
+    Some(i32),
+    None,
+}
+
+pub fn toto(i : MyOption) -> i32 {
+    if let MyOption::Some(v) = i {
+        v
+    } else {
+        23i32
+    }
+}
diff --git a/gcc/testsuite/rust/compile/iflet.rs b/gcc/testsuite/rust/compile/iflet.rs
new file mode 100644 (file)
index 0000000..6d46339
--- /dev/null
@@ -0,0 +1,32 @@
+pub fn simple_iflet() -> i32 {
+    let mut res = 0;
+
+    enum E {
+        X(i32),
+    }
+    let v = E::X(4);
+    
+    if let E::X(n) = v {
+        res = 1;
+    }
+
+    res
+}
+
+pub fn simple_iflet_else() -> i32 {
+    let mut res = 0;
+
+    enum E {
+        X(i32),
+        Y,
+    }
+    let v = E::X(4);
+
+    if let E::Y = v {
+        res = 1;
+    } else {
+        res = 2;
+    }
+
+    res
+}
index 47d651b22bdfd6beedae7b5526be7e1a7b72f29c..424ad686094546c7b651eac337cb2be4ec4ff06b 100644 (file)
@@ -223,4 +223,7 @@ issue-2203.rs
 issue-2499.rs
 issue-3032-1.rs
 issue-3032-2.rs
+# https://github.com/Rust-GCC/gccrs/issues/3189
+if_let_expr_simple.rs
+iflet.rs
 # please don't delete the trailing newline
diff --git a/gcc/testsuite/rust/execute/torture/iflet.rs b/gcc/testsuite/rust/execute/torture/iflet.rs
new file mode 100644 (file)
index 0000000..da4e93a
--- /dev/null
@@ -0,0 +1,84 @@
+enum Res {
+    OK,
+    BAD,
+}
+
+enum LOption {
+    Some(i32),
+    None,
+}
+
+// Expect a Some(_)
+//
+// Check we can match a Some.
+fn test_can_destructure_Some(v: LOption) -> Res {
+    if let LOption::Some(v) = v {
+        return Res::OK;
+    }
+    return Res::BAD;
+}
+
+// Expect Some(100).
+//
+// Check we can destructure and the inner value is correct.
+fn test_inner_value_is_100(v: LOption) -> Res {
+    if let LOption::Some(v) = v {
+        return match v {
+            100 => Res::OK,
+            _   => Res::BAD,
+        }
+    }
+    return Res::BAD;
+}
+
+// Expect a None as actual parameter.
+//
+// Only when we FAIL to match a Some do we take the else and return OK.
+fn test_if_else(v: LOption) -> Res {
+    if let LOption::Some(v) = v {
+        return Res::BAD;
+    } else {
+        return Res::OK;
+    }
+}
+
+fn main() -> i32 {
+
+    // Passing a None, so the function should return BAD
+    match test_can_destructure_Some(LOption::None) {
+        Res::OK => return 1,
+        Res::BAD => (),
+    }
+
+    // Same, but with a Some, should return OK
+    match test_can_destructure_Some(LOption::Some(1)) {
+        Res::OK => (),
+        Res::BAD => return 1,
+    }
+
+    // Check the destructuring is correct by looking for Some(100)
+    match test_inner_value_is_100(LOption::Some(100)) {
+        Res::OK => (),
+        Res::BAD => return 1,
+    }
+
+    // ... passing Some(1) should return BAD
+    match test_inner_value_is_100(LOption::Some(1)) {
+        Res::OK => return 1,
+        Res::BAD => (),
+    }
+
+    // ... and so does passing None
+    match test_inner_value_is_100(LOption::None) {
+        Res::OK => return 1,
+        Res::BAD => (),
+    }
+
+    // Check if let... else ...
+    match test_if_else(LOption::None) {
+        Res::OK => (),
+        Res::BAD => return 1,
+    }
+
+    0
+}