[PATCH] parse if expression with unary minus or not expression

2021-08-14 Thread Mark Wielaard
An if conditional expression doesn't need brackets, but that means
that it doesn't accept struct expressions. Those are not easy to
distinquish from the block that follows. What isn't immediately clear
from the grammar is that unary conditions like minus '-' or not '!'
also shouldn't accept struct expressions (when part of an if
conditional expression) because those also cannot be easily
distinquished from the block that follows.

Add a testcase "ifunaryexpr.rs" that shows a couple of contructs that
should be accepted as if conditional expressions and fix the parser to
pass the restriction of not accepting struct expressions after a unary
expression.
---
 gcc/rust/parse/rust-parse-impl.h  |  4 
 .../rust/compile/torture/ifunaryexpr.rs   | 22 +++
 2 files changed, 26 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/torture/ifunaryexpr.rs

diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index 731e0b3682f..fa6d409c6dc 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -12547,6 +12547,8 @@ Parser::null_denotation 
(const_TokenPtr tok,
   case MINUS: { // unary minus
ParseRestrictions entered_from_unary;
entered_from_unary.entered_from_unary = true;
+   if (!restrictions.can_be_struct_expr)
+ entered_from_unary.can_be_struct_expr = false;
std::unique_ptr expr
  = parse_expr (LBP_UNARY_MINUS, {}, entered_from_unary);
 
@@ -12571,6 +12573,8 @@ Parser::null_denotation 
(const_TokenPtr tok,
   case EXCLAM: { // logical or bitwise not
ParseRestrictions entered_from_unary;
entered_from_unary.entered_from_unary = true;
+   if (!restrictions.can_be_struct_expr)
+ entered_from_unary.can_be_struct_expr = false;
std::unique_ptr expr
  = parse_expr (LBP_UNARY_EXCLAM, {}, entered_from_unary);
 
diff --git a/gcc/testsuite/rust/compile/torture/ifunaryexpr.rs 
b/gcc/testsuite/rust/compile/torture/ifunaryexpr.rs
new file mode 100644
index 000..8f0bb87f558
--- /dev/null
+++ b/gcc/testsuite/rust/compile/torture/ifunaryexpr.rs
@@ -0,0 +1,22 @@
+extern "C"
+{
+  pub fn abort ();
+}
+
+struct B { b: bool }
+
+pub fn main ()
+{
+  let n = 1;
+  if 0 > -n { } else { unsafe { abort (); } }
+
+  let b = true;
+  if !b { unsafe { abort (); } }
+  if !!b { } else { unsafe { abort (); } }
+
+  let bb = B { b: false };
+
+  if !bb.b && !b { unsafe { abort (); } }
+
+  if (B { b: true }).b { } else { unsafe { abort (); } }
+}
-- 
2.32.0

-- 
Gcc-rust mailing list
Gcc-rust@gcc.gnu.org
https://gcc.gnu.org/mailman/listinfo/gcc-rust


[PATCH] Use builtin bool instead of creating new bool types for ComparisonExpr

2021-08-14 Thread Mark Wielaard
The TypeCheckExpr creates a new TyTy::BoolType for a
ComparisonExpr. This new BoolType is unknown to TyTyResolveCompile
which causes a crash when trying to compile the inferred new
BoolType. The new "bools_eq.rs" testcase uses several bools which show
this issue. Resolve this by looking up the builtin bool type.
---
 gcc/rust/typecheck/rust-hir-type-check-expr.h  |  2 +-
 gcc/testsuite/rust/compile/torture/bools_eq.rs | 18 ++
 2 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/rust/compile/torture/bools_eq.rs

diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h 
b/gcc/rust/typecheck/rust-hir-type-check-expr.h
index d88cb0b7f1d..9cf64685e00 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h
@@ -631,7 +631,7 @@ public:
   return;
 
 // we expect this to be
-infered = new TyTy::BoolType (expr.get_mappings ().get_hirid ());
+context->lookup_builtin ("bool", &infered);
 infered->append_reference (lhs->get_ref ());
 infered->append_reference (rhs->get_ref ());
   }
diff --git a/gcc/testsuite/rust/compile/torture/bools_eq.rs 
b/gcc/testsuite/rust/compile/torture/bools_eq.rs
new file mode 100644
index 000..965127b5d54
--- /dev/null
+++ b/gcc/testsuite/rust/compile/torture/bools_eq.rs
@@ -0,0 +1,18 @@
+extern "C"
+{
+  fn abort ();
+}
+
+fn beq (a: bool, b: bool) -> bool
+{
+  let bools_eq = a == b;
+  bools_eq
+}
+
+pub fn main ()
+{
+  let a = true;
+  let b = false;
+  let r = beq (a, b);
+  if r { unsafe { abort (); } }
+}
-- 
2.32.0

-- 
Gcc-rust mailing list
Gcc-rust@gcc.gnu.org
https://gcc.gnu.org/mailman/listinfo/gcc-rust