public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Compile matches on boolean expressions
@ 2022-06-08 12:42 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:42 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:aa372462f481aab3593ab76782f35c816365d648

commit aa372462f481aab3593ab76782f35c816365d648
Author: David Faust <david.faust@oracle.com>
Date:   Tue May 3 12:55:39 2022 -0700

    Compile matches on boolean expressions

Diff:
---
 gcc/rust/backend/rust-compile-expr.cc             | 51 ++++++++++++++++-------
 gcc/testsuite/rust/execute/torture/match_bool1.rs | 44 +++++++++++++++++++
 2 files changed, 79 insertions(+), 16 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc
index 422526321b4..eb7b0dfc1b2 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -211,13 +211,18 @@ CompileExpr::visit (HIR::MatchExpr &expr)
       return;
     }
 
-  rust_assert (scrutinee_expr_tyty->get_kind () == TyTy::TypeKind::ADT);
+  TyTy::TypeKind scrutinee_kind = scrutinee_expr_tyty->get_kind ();
+  rust_assert (scrutinee_kind == TyTy::TypeKind::BOOL
+	       || scrutinee_kind == TyTy::TypeKind::ADT);
 
-  // this will need to change but for now the first pass implementation, lets
-  // assert this is the case
-  TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (scrutinee_expr_tyty);
-  rust_assert (adt->is_enum ());
-  rust_assert (adt->number_of_variants () > 0);
+  if (scrutinee_kind == TyTy::TypeKind::ADT)
+    {
+      // this will need to change but for now the first pass implementation,
+      // lets assert this is the case
+      TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (scrutinee_expr_tyty);
+      rust_assert (adt->is_enum ());
+      rust_assert (adt->number_of_variants () > 0);
+    }
 
   TyTy::BaseType *expr_tyty = nullptr;
   if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
@@ -247,16 +252,30 @@ CompileExpr::visit (HIR::MatchExpr &expr)
   tree match_scrutinee_expr
     = CompileExpr::Compile (expr.get_scrutinee_expr ().get (), ctx);
 
-  // need to access the qualifier field, if we use QUAL_UNION_TYPE this would be
-  // DECL_QUALIFIER i think. For now this will just access the first record
-  // field and its respective qualifier because it will always be set because
-  // this is all a big special union
-  tree scrutinee_first_record_expr
-    = ctx->get_backend ()->struct_field_expression (
-      match_scrutinee_expr, 0, expr.get_scrutinee_expr ()->get_locus ());
-  tree match_scrutinee_expr_qualifier_expr
-    = ctx->get_backend ()->struct_field_expression (
-      scrutinee_first_record_expr, 0, expr.get_scrutinee_expr ()->get_locus ());
+  tree match_scrutinee_expr_qualifier_expr;
+  if (scrutinee_kind == TyTy::TypeKind::BOOL)
+    {
+      match_scrutinee_expr_qualifier_expr = match_scrutinee_expr;
+    }
+  else if (scrutinee_kind == TyTy::TypeKind::ADT)
+    {
+      // need to access qualifier the field, if we use QUAL_UNION_TYPE this
+      // would be DECL_QUALIFIER i think. For now this will just access the
+      // first record field and its respective qualifier because it will always
+      // be set because this is all a big special union
+      tree scrutinee_first_record_expr
+	= ctx->get_backend ()->struct_field_expression (
+	  match_scrutinee_expr, 0, expr.get_scrutinee_expr ()->get_locus ());
+      match_scrutinee_expr_qualifier_expr
+	= ctx->get_backend ()->struct_field_expression (
+	  scrutinee_first_record_expr, 0,
+	  expr.get_scrutinee_expr ()->get_locus ());
+    }
+  else
+    {
+      // FIXME: match on other types of expressions not yet implemented.
+      gcc_assert (0);
+    }
 
   // setup the end label so the cases can exit properly
   tree fndecl = fnctx.fndecl;
diff --git a/gcc/testsuite/rust/execute/torture/match_bool1.rs b/gcc/testsuite/rust/execute/torture/match_bool1.rs
new file mode 100644
index 00000000000..45900b82410
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/match_bool1.rs
@@ -0,0 +1,44 @@
+// { dg-output "182 is more than 100\n55 is less than 100\n" }
+
+extern "C" {
+    fn printf(s: *const i8, ...);
+}
+
+fn foo (x: bool) -> i32 {
+    match x {
+        true => { return 182; },
+        false => { return 55; },
+    }
+}
+
+fn bar (y: i32) {
+
+    match y < 100 {
+        true => {
+            let a = "%i is less than 100\n\0";
+            let b = a as *const str;
+            let c = b as *const i8;
+
+            printf (c, y);
+        }
+        _ => {
+            let a = "%i is more than 100\n\0";
+            let b = a as *const str;
+            let c = b as *const i8;
+
+            printf (c, y);
+        }
+    }
+}
+
+
+fn main () -> i32 {
+
+    let a = foo (true);
+    let b = foo (false);
+
+    bar (a);
+    bar (b);
+
+    0
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-08 12:42 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:42 [gcc/devel/rust/master] Compile matches on boolean expressions Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).