diff options
author | David Faust <david.faust@oracle.com> | 2022-05-03 12:55:39 -0700 |
---|---|---|
committer | David Faust <david.faust@oracle.com> | 2022-05-03 12:55:39 -0700 |
commit | aa372462f481aab3593ab76782f35c816365d648 (patch) | |
tree | 97f7702d0b6bb78d984c2eb8ff4d1e0795821802 /gcc | |
parent | ca75e756e802ccfff445f7efb4f3b530c6f3891f (diff) | |
download | gcc-aa372462f481aab3593ab76782f35c816365d648.zip gcc-aa372462f481aab3593ab76782f35c816365d648.tar.gz gcc-aa372462f481aab3593ab76782f35c816365d648.tar.bz2 |
Compile matches on boolean expressions
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.cc | 51 | ||||
-rw-r--r-- | 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 4225263..eb7b0df 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 0000000..45900b8 --- /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 +} |