aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2021-01-19 14:47:01 +0000
committerPhilip Herron <herron.philip@googlemail.com>2021-01-20 10:02:14 +0000
commit76b308af7e69f5b50fee96ba4b3584935e621259 (patch)
tree2c98289d813b6fc133c989ae2e0de6b7a25348f4 /gcc
parent23edde6fef4321d30f9f2f8c75e6cbfd59b75ca4 (diff)
downloadgcc-76b308af7e69f5b50fee96ba4b3584935e621259.zip
gcc-76b308af7e69f5b50fee96ba4b3584935e621259.tar.gz
gcc-76b308af7e69f5b50fee96ba4b3584935e621259.tar.bz2
ComparisonExprs and LazyBoolExprs are bools
This fixes the expression type resolution to coerce these into bools. For LazyBoolExprs && and || the lhs and rhs are bools.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-expr.h19
-rw-r--r--gcc/testsuite/rust.test/compilable/comparison_expr1.rs34
2 files changed, 51 insertions, 2 deletions
diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h
index dfa319b..c47e0ec 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h
@@ -376,7 +376,12 @@ public:
auto lhs = TypeCheckExpr::Resolve (expr.get_lhs ());
auto rhs = TypeCheckExpr::Resolve (expr.get_rhs ());
- infered = lhs->combine (rhs);
+ auto result = lhs->combine (rhs);
+ if (result == nullptr || result->get_kind () == TyTy::TypeKind::ERROR)
+ return;
+
+ // we expect this to be
+ infered = new TyTy::BoolType (expr.get_mappings ().get_hirid ());
}
void visit (HIR::LazyBooleanExpr &expr)
@@ -384,8 +389,18 @@ public:
auto lhs = TypeCheckExpr::Resolve (expr.get_lhs ());
auto rhs = TypeCheckExpr::Resolve (expr.get_rhs ());
+ // we expect the lhs and rhs must be bools at this point
+ TyTy::BoolType elhs (expr.get_mappings ().get_hirid ());
+ lhs = elhs.combine (lhs);
+ if (lhs == nullptr || lhs->get_kind () == TyTy::TypeKind::ERROR)
+ return;
+
+ TyTy::BoolType rlhs (expr.get_mappings ().get_hirid ());
+ rhs = elhs.combine (rhs);
+ if (lhs == nullptr || lhs->get_kind () == TyTy::TypeKind::ERROR)
+ return;
+
infered = lhs->combine (rhs);
- // FIXME this will need to turn into bool
}
void visit (HIR::IfExpr &expr)
diff --git a/gcc/testsuite/rust.test/compilable/comparison_expr1.rs b/gcc/testsuite/rust.test/compilable/comparison_expr1.rs
new file mode 100644
index 0000000..d3c5263
--- /dev/null
+++ b/gcc/testsuite/rust.test/compilable/comparison_expr1.rs
@@ -0,0 +1,34 @@
+fn is_zero(x: i32) -> bool {
+ x == 0
+}
+
+fn is_not_zero(x: i32) -> bool {
+ x != 0
+}
+
+fn is_positive(x: i32) -> bool {
+ x > 0
+}
+
+fn is_negative(x: i32) -> bool {
+ x < 0
+}
+
+fn is_positive_or_zero(x: i32) -> bool {
+ x >= 0
+}
+
+fn is_negative_or_zero(x: i32) -> bool {
+ x <= 0
+}
+
+fn main() {
+ let a: bool = is_zero(1);
+ let b: bool = is_not_zero(2);
+ let c: bool = is_positive(3);
+ let d: bool = is_negative(4);
+ let e: bool = is_positive_or_zero(5);
+ let f: bool = is_negative_or_zero(6);
+ let g: bool = a || b;
+ let h: bool = c && d;
+}