public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-7962] gccrs: Improve type checking for if expressions
@ 2024-01-16 18:09 Arthur Cohen
  0 siblings, 0 replies; only message in thread
From: Arthur Cohen @ 2024-01-16 18:09 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:6e7ed4bad9656e8b5c5996396ed29ca5f06d3808

commit r14-7962-g6e7ed4bad9656e8b5c5996396ed29ca5f06d3808
Author: Owen Avery <powerboat9.gamer@gmail.com>
Date:   Fri Sep 1 22:07:30 2023 -0400

    gccrs: Improve type checking for if expressions
    
    gcc/rust/ChangeLog:
    
            * typecheck/rust-hir-type-check-expr.cc
            (TypeCheckExpr::visit): Expect if conditions to have type bool.
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/type-if.rs: New test.
    
    Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>

Diff:
---
 gcc/rust/typecheck/rust-hir-type-check-expr.cc | 26 ++++++++++++++++++++++++--
 gcc/testsuite/rust/compile/type-if.rs          |  5 +++++
 2 files changed, 29 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 f4ffc40fe09..898ea117333 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
@@ -444,7 +444,18 @@ TypeCheckExpr::visit (HIR::NegationExpr &expr)
 void
 TypeCheckExpr::visit (HIR::IfExpr &expr)
 {
-  TypeCheckExpr::Resolve (expr.get_if_condition ().get ());
+  TyTy::BaseType *bool_ty = nullptr;
+  bool ok = context->lookup_builtin ("bool", &bool_ty);
+  rust_assert (ok);
+
+  TyTy::BaseType *cond_type
+    = TypeCheckExpr::Resolve (expr.get_if_condition ().get ());
+
+  unify_site (expr.get_mappings ().get_hirid (), TyTy::TyWithLocation (bool_ty),
+	      TyTy::TyWithLocation (cond_type,
+				    expr.get_if_condition ()->get_locus ()),
+	      expr.get_locus ());
+
   TypeCheckExpr::Resolve (expr.get_if_block ().get ());
 
   infered = TyTy::TupleType::get_unit_type (expr.get_mappings ().get_hirid ());
@@ -453,7 +464,18 @@ TypeCheckExpr::visit (HIR::IfExpr &expr)
 void
 TypeCheckExpr::visit (HIR::IfExprConseqElse &expr)
 {
-  TypeCheckExpr::Resolve (expr.get_if_condition ().get ());
+  TyTy::BaseType *bool_ty = nullptr;
+  bool ok = context->lookup_builtin ("bool", &bool_ty);
+  rust_assert (ok);
+
+  TyTy::BaseType *cond_type
+    = TypeCheckExpr::Resolve (expr.get_if_condition ().get ());
+
+  unify_site (expr.get_mappings ().get_hirid (), TyTy::TyWithLocation (bool_ty),
+	      TyTy::TyWithLocation (cond_type,
+				    expr.get_if_condition ()->get_locus ()),
+	      expr.get_locus ());
+
   auto if_blk_resolved = TypeCheckExpr::Resolve (expr.get_if_block ().get ());
   auto else_blk_resolved
     = TypeCheckExpr::Resolve (expr.get_else_block ().get ());
diff --git a/gcc/testsuite/rust/compile/type-if.rs b/gcc/testsuite/rust/compile/type-if.rs
new file mode 100644
index 00000000000..f3d0eb6ca67
--- /dev/null
+++ b/gcc/testsuite/rust/compile/type-if.rs
@@ -0,0 +1,5 @@
+pub fn main() -> i32 {
+    if 12 {} // { dg-error "mismatched types" }
+    if 12 {} else {} // { dg-error "mismatched types" }
+    0
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-01-16 18:09 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-16 18:09 [gcc r14-7962] gccrs: Improve type checking for if expressions Arthur Cohen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).