Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gccrs: empty match expressions should resolve to ! #3256

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions gcc/rust/backend/rust-compile-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1014,17 +1014,7 @@ check_match_scrutinee (HIR::MatchExpr &expr, Context *ctx)
|| scrutinee_kind == TyTy::TypeKind::TUPLE
|| scrutinee_kind == TyTy::TypeKind::REF);

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);
if (adt->is_enum ())
rust_assert (adt->number_of_variants () > 0);
else
rust_assert (adt->number_of_variants () == 1);
}
else if (scrutinee_kind == TyTy::TypeKind::FLOAT)
if (scrutinee_kind == TyTy::TypeKind::FLOAT)
{
// FIXME: CASE_LABEL_EXPR does not support floating point types.
// Find another way to compile these.
Expand Down Expand Up @@ -1065,6 +1055,15 @@ CompileExpr::visit (HIR::MatchExpr &expr)
return;
}

// if the result of this expression is meant to be never type then we can
// optimise this away but there is the case where match arms resolve to !
// because of return statements we need to special case this
if (!expr.has_match_arms () && expr_tyty->is<TyTy::NeverType> ())
{
translated = unit_expression (expr.get_locus ());
return;
}

fncontext fnctx = ctx->peek_fn ();
Bvariable *tmp = NULL;
tree enclosing_scope = ctx->peek_enclosing_scope ();
Expand Down
3 changes: 3 additions & 0 deletions gcc/rust/checks/errors/rust-hir-pattern-analysis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,9 @@ void
check_match_usefulness (Resolver::TypeCheckContext *ctx,
TyTy::BaseType *scrutinee_ty, HIR::MatchExpr &expr)
{
if (!expr.has_match_arms ())
return;

// Lower the arms to a more convenient representation.
std::vector<MatrixRow> rows;
for (auto &arm : expr.get_match_cases ())
Expand Down
12 changes: 8 additions & 4 deletions gcc/rust/hir/rust-hir-dump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ Dump::do_matcharm (MatchArm &e)
// FIXME Can't remember how to handle that. Let's see later.
// do_outer_attrs(e);
visit_collection ("match_arm_patterns", e.get_patterns ());
visit_field ("guard_expr", e.get_guard_expr ());
if (e.has_match_arm_guard ())
visit_field ("guard_expr", e.get_guard_expr ());
end ("MatchArm");
}

Expand Down Expand Up @@ -1264,7 +1265,8 @@ Dump::visit (BlockExpr &e)

visit_collection ("statements", e.get_statements ());

visit_field ("expr", e.get_final_expr ());
if (e.has_final_expr ())
visit_field ("expr", e.get_final_expr ());

end ("BlockExpr");
}
Expand Down Expand Up @@ -1489,7 +1491,8 @@ Dump::visit (TypeParam &e)

visit_collection ("type_param_bounds", e.get_type_param_bounds ());

visit_field ("type", e.get_type ());
if (e.has_type ())
visit_field ("type", e.get_type ());

end ("TypeParam");
}
Expand Down Expand Up @@ -1655,7 +1658,8 @@ Dump::visit (Function &e)
put_field ("function_params", "empty");
}

visit_field ("return_type", e.get_return_type ());
if (e.has_function_return_type ())
visit_field ("return_type", e.get_return_type ());

if (!e.has_where_clause ())
put_field ("where_clause", "none");
Expand Down
12 changes: 12 additions & 0 deletions gcc/rust/typecheck/rust-hir-type-check-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,18 @@ TypeCheckExpr::visit (HIR::MatchExpr &expr)
TyTy::BaseType *scrutinee_tyty
= TypeCheckExpr::Resolve (expr.get_scrutinee_expr ());

// https://github.com/Rust-GCC/gccrs/issues/3231#issuecomment-2462660048
// https://github.com/rust-lang/rust/blob/3d1dba830a564d1118361345d7ada47a05241f45/compiler/rustc_hir_typeck/src/_match.rs#L32-L36
if (!expr.has_match_arms ())
{
// this is a special case where rustc returns !
TyTy::BaseType *lookup = nullptr;
bool ok = context->lookup_builtin ("!", &lookup);
rust_assert (ok);
infered = lookup->clone ();
return;
}

bool saw_error = false;
std::vector<TyTy::BaseType *> kase_block_tys;
for (auto &kase : expr.get_match_cases ())
Expand Down
4 changes: 1 addition & 3 deletions gcc/testsuite/rust/compile/exhaustiveness1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ fn s2(s: S) {
}

fn s3(s: S) {
match s {
// { dg-error "non-exhaustive patterns: '_' not covered" "" { target *-*-* } .-1 }
}
match s {}
}

enum E {
Expand Down
8 changes: 8 additions & 0 deletions gcc/testsuite/rust/compile/issue-2567-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// { dg-options "-w" }
enum Empty {}

fn foo(x: Empty) {
let x: Empty = match x {
// empty
};
}
8 changes: 8 additions & 0 deletions gcc/testsuite/rust/compile/issue-2567-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// { dg-options "-w" }
enum Empty {}

fn foo(x: Empty) {
let x: i32 = match x {
// empty
};
}
8 changes: 8 additions & 0 deletions gcc/testsuite/rust/compile/issue-2567-3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// { dg-options "-w" }
enum Empty {}

fn foo(x: Empty) {
match x {
// empty
}
}
8 changes: 8 additions & 0 deletions gcc/testsuite/rust/compile/issue-3231.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// { dg-options "-w" }
pub enum X {}

pub fn foo(x: X) {
let _a: i32 = match x {};
}

pub fn main() {}
Loading