diff options
author | Philip Herron <herron.philip@googlemail.com> | 2024-11-21 17:36:34 +0000 |
---|---|---|
committer | CohenArthur <arthur.cohen@embecosm.com> | 2024-11-22 13:53:47 +0000 |
commit | 433a09278263a13b8aff12fbbc61d2e8723c947a (patch) | |
tree | 37cebe5ae1cf85f5e2ee0548e1cbcf389d738f6b /gcc/rust/backend/rust-compile-expr.cc | |
parent | b3f0d72ba8d365fd0cdabbe216acbbebfabb5df4 (diff) | |
download | gcc-433a09278263a13b8aff12fbbc61d2e8723c947a.zip gcc-433a09278263a13b8aff12fbbc61d2e8723c947a.tar.gz gcc-433a09278263a13b8aff12fbbc61d2e8723c947a.tar.bz2 |
gccrs: empty match expressions should resolve to !
This is a special case in Rust and the ! type can unify with pretty much
anything its almost a inference variable and a unit-type for special cases.
Fixes Rust-GCC/gccrs#3231
Fixes Rust-GCC/gccrs#2567
gcc/rust/ChangeLog:
* backend/rust-compile-expr.cc (check_match_scrutinee): check for empty match
(CompileExpr::visit): fix assertion
* checks/errors/rust-hir-pattern-analysis.cc (check_match_usefulness): check for empty
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): resolve to !
gcc/testsuite/ChangeLog:
* rust/compile/exhaustiveness1.rs: remove bad check
* rust/compile/issue-2567-1.rs: New test.
* rust/compile/issue-2567-2.rs: New test.
* rust/compile/issue-2567-3.rs: New test.
* rust/compile/issue-3231.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Diffstat (limited to 'gcc/rust/backend/rust-compile-expr.cc')
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.cc | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 5b7d67a..7323413 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -1013,17 +1013,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. @@ -1064,6 +1054,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 (); |