diff options
author | Benjamin Thos <benjamin.thos@epita.fr> | 2024-12-16 14:11:38 +0100 |
---|---|---|
committer | CohenArthur <arthur.cohen@embecosm.com> | 2025-02-17 09:40:03 +0000 |
commit | 7afc39068eb803ae2937ab6d22f93ab68d627173 (patch) | |
tree | f85195a04945e7fa0ad5f9caac37238be26f864c /gcc/rust | |
parent | 681805f7eeaf0ff15045892d289f7e4fe0ea589c (diff) | |
download | gcc-7afc39068eb803ae2937ab6d22f93ab68d627173.zip gcc-7afc39068eb803ae2937ab6d22f93ab68d627173.tar.gz gcc-7afc39068eb803ae2937ab6d22f93ab68d627173.tar.bz2 |
Add type check on if-expr
Check if an if-expr returns void type or a coercible type like an early return.
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit):
Add check on if-expr.
gcc/testsuite/ChangeLog:
* rust/compile/implicit_returns_err3.rs: Change test to be valid.
* rust/compile/torture/if.rs: Likewise.
* rust/compile/if-without-else.rs: New test.
Signed-off-by: Benjamin Thos <benjamin.thos@epita.fr>
Diffstat (limited to 'gcc/rust')
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-expr.cc | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc index 99edcc3..8d00011 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc @@ -526,9 +526,18 @@ TypeCheckExpr::visit (HIR::IfExpr &expr) expr.get_if_condition ().get_locus ()), expr.get_locus ()); - TypeCheckExpr::Resolve (expr.get_if_block ()); + TyTy::BaseType *block_type = TypeCheckExpr::Resolve (expr.get_if_block ()); - infered = TyTy::TupleType::get_unit_type (); + TyTy::BaseType *unit_ty = nullptr; + ok = context->lookup_builtin ("()", &unit_ty); + rust_assert (ok); + + infered + = coercion_site (expr.get_mappings ().get_hirid (), + TyTy::TyWithLocation (unit_ty), + TyTy::TyWithLocation (block_type, + expr.get_if_block ().get_locus ()), + expr.get_locus ()); } void |