aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-05-11 17:12:51 +0000
committerGitHub <noreply@github.com>2022-05-11 17:12:51 +0000
commitd4434b511a4e650e95c7a1de6810d8748c5d8a70 (patch)
tree3556b2c237a43afcde7a643cac7896e0b39e9858 /gcc/rust
parentf2aee09a0b72eec4732cc2e3324bf62729539f88 (diff)
parent5aa411c537289f5695b63de12b973415b2d830d6 (diff)
downloadgcc-d4434b511a4e650e95c7a1de6810d8748c5d8a70.zip
gcc-d4434b511a4e650e95c7a1de6810d8748c5d8a70.tar.gz
gcc-d4434b511a4e650e95c7a1de6810d8748c5d8a70.tar.bz2
Merge #1244
1244: Allow match on integer and char types r=dafaust a=dafaust Enable compiling match expressions on other primitive types like `i32`, `usize` and `char`. Co-authored-by: David Faust <david.faust@oracle.com>
Diffstat (limited to 'gcc/rust')
-rw-r--r--gcc/rust/backend/rust-compile-expr.cc14
-rw-r--r--gcc/rust/backend/rust-compile-pattern.cc11
-rw-r--r--gcc/rust/typecheck/rust-tyty.cc20
-rw-r--r--gcc/rust/typecheck/rust-tyty.h3
4 files changed, 45 insertions, 3 deletions
diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc
index 0307df1..824d6d3 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -212,7 +212,8 @@ CompileExpr::visit (HIR::MatchExpr &expr)
}
TyTy::TypeKind scrutinee_kind = scrutinee_expr_tyty->get_kind ();
- rust_assert (scrutinee_kind == TyTy::TypeKind::BOOL
+ rust_assert ((TyTy::is_primitive_type_kind (scrutinee_kind)
+ && scrutinee_kind != TyTy::TypeKind::NEVER)
|| scrutinee_kind == TyTy::TypeKind::ADT);
if (scrutinee_kind == TyTy::TypeKind::ADT)
@@ -223,6 +224,13 @@ CompileExpr::visit (HIR::MatchExpr &expr)
rust_assert (adt->is_enum ());
rust_assert (adt->number_of_variants () > 0);
}
+ else if (scrutinee_kind == TyTy::TypeKind::FLOAT)
+ {
+ // FIXME: CASE_LABEL_EXPR does not support floating point types.
+ // Find another way to compile these.
+ sorry_at (expr.get_locus ().gcc_location (),
+ "match on floating-point types is not yet supported");
+ }
TyTy::BaseType *expr_tyty = nullptr;
if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
@@ -253,7 +261,7 @@ CompileExpr::visit (HIR::MatchExpr &expr)
= CompileExpr::Compile (expr.get_scrutinee_expr ().get (), ctx);
tree match_scrutinee_expr_qualifier_expr;
- if (scrutinee_kind == TyTy::TypeKind::BOOL)
+ if (TyTy::is_primitive_type_kind (scrutinee_kind))
{
match_scrutinee_expr_qualifier_expr = match_scrutinee_expr;
}
@@ -274,7 +282,7 @@ CompileExpr::visit (HIR::MatchExpr &expr)
else
{
// FIXME: match on other types of expressions not yet implemented.
- gcc_assert (0);
+ gcc_unreachable ();
}
// setup the end label so the cases can exit properly
diff --git a/gcc/rust/backend/rust-compile-pattern.cc b/gcc/rust/backend/rust-compile-pattern.cc
index e8f1c51..9c1a35a 100644
--- a/gcc/rust/backend/rust-compile-pattern.cc
+++ b/gcc/rust/backend/rust-compile-pattern.cc
@@ -89,6 +89,17 @@ CompilePatternCaseLabelExpr::visit (HIR::LiteralPattern &pattern)
pattern.get_literal (), pattern.get_locus (),
std::vector<AST::Attribute> ());
+ // Note: Floating point literals are currently accepted but will likely be
+ // forbidden in LiteralPatterns in a future version of Rust.
+ // See: https://github.com/rust-lang/rust/issues/41620
+ // For now, we cannot compile them anyway as CASE_LABEL_EXPR does not support
+ // floating point types.
+ if (pattern.get_literal ().get_lit_type () == HIR::Literal::LitType::FLOAT)
+ {
+ sorry_at (pattern.get_locus ().gcc_location (),
+ "floating-point literal in pattern");
+ }
+
tree lit = CompileExpr::Compile (litexpr, ctx);
case_label_expr = build_case_label (lit, NULL_TREE, associated_case_label);
diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc
index 6b034ea..d0e8b76 100644
--- a/gcc/rust/typecheck/rust-tyty.cc
+++ b/gcc/rust/typecheck/rust-tyty.cc
@@ -114,6 +114,26 @@ TypeKindFormat::to_string (TypeKind kind)
}
bool
+is_primitive_type_kind (TypeKind kind)
+{
+ switch (kind)
+ {
+ case TypeKind::BOOL:
+ case TypeKind::CHAR:
+ case TypeKind::INT:
+ case TypeKind::UINT:
+ case TypeKind::ISIZE:
+ case TypeKind::USIZE:
+ case TypeKind::FLOAT:
+ case TypeKind::NEVER:
+ case TypeKind::STR:
+ return true;
+ default:
+ return false;
+ }
+}
+
+bool
BaseType::satisfies_bound (const TypeBoundPredicate &predicate) const
{
const Resolver::TraitReference *query = predicate.get ();
diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h
index 5f35c32..a2950e9 100644
--- a/gcc/rust/typecheck/rust-tyty.h
+++ b/gcc/rust/typecheck/rust-tyty.h
@@ -67,6 +67,9 @@ enum TypeKind
ERROR
};
+extern bool
+is_primitive_type_kind (TypeKind kind);
+
class TypeKindFormat
{
public: