Skip to content

Commit

Permalink
backend: Allow anything as a match scrutinee
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* backend/rust-compile-expr.cc (check_match_scrutinee): Allow anything to be used as a
	match scrutinee, not just ADTs.

gcc/testsuite/ChangeLog:

	* rust/compile/match_scrutinee.rs: New test.
  • Loading branch information
CohenArthur committed Jan 22, 2025
1 parent b87fd67 commit 5fa1a1d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
13 changes: 8 additions & 5 deletions gcc/rust/backend/rust-compile-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "convert.h"
#include "print-tree.h"
#include "rust-system.h"
#include "rust-tyty.h"

namespace Rust {
namespace Compile {
Expand Down Expand Up @@ -1035,11 +1036,13 @@ check_match_scrutinee (HIR::MatchExpr &expr, Context *ctx)
}

TyTy::TypeKind scrutinee_kind = scrutinee_expr_tyty->get_kind ();
rust_assert ((TyTy::is_primitive_type_kind (scrutinee_kind)
&& scrutinee_kind != TyTy::TypeKind::NEVER)
|| scrutinee_kind == TyTy::TypeKind::ADT
|| scrutinee_kind == TyTy::TypeKind::TUPLE
|| scrutinee_kind == TyTy::TypeKind::REF);
rust_debug_loc(expr.get_locus(), "[ARTHUR] check scrutinee: type: %s, %s", TyTy::TypeKindFormat::to_string(scrutinee_kind).c_str(), scrutinee_expr_tyty->as_string().c_str());

// rust_assert ((TyTy::is_primitive_type_kind (scrutinee_kind)
// && scrutinee_kind != TyTy::TypeKind::NEVER)
// || scrutinee_kind == TyTy::TypeKind::ADT
// || scrutinee_kind == TyTy::TypeKind::TUPLE
// || scrutinee_kind == TyTy::TypeKind::REF);

if (scrutinee_kind == TyTy::TypeKind::FLOAT)
{
Expand Down
29 changes: 29 additions & 0 deletions gcc/testsuite/rust/compile/match_scrutinee.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
struct Foo {
a: i32,
b: bool,
}

fn foo() -> Foo {
Foo { a: 145, b: false }
}

fn main() {
let x = 15;

match x {
15 => {},
_ => {},
};

match 14 {
14 => {},
_ => {},
};

match foo() {
mut foo => {
let _ = foo.a;
let _ = foo.b;
},
};
}

0 comments on commit 5fa1a1d

Please sign in to comment.