]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Improve error messages for operator expressions
authorAntonio Gomes <antoniospg100@gmail.com>
Sun, 14 Jul 2024 04:45:24 +0000 (01:45 -0300)
committerP-E-P <32375388+P-E-P@users.noreply.github.com>
Wed, 17 Jul 2024 11:41:19 +0000 (11:41 +0000)
gcc/rust/ChangeLog:
* hir/tree/rust-hir-expr.h: Add new get_operator_str method in
ArithmeticOrLogicalExpr and CompoundAssignmentExpr
* hir/tree/rust-hir.cc: Likewise
* typecheck/rust-hir-type-check-expr.cc: Improve error message for
operator expressions to display the correct operator symbol

gcc/testsuite/ChangeLog:
* rust/compile/shadow1.rs: Fix test for new error message

Signed-off-by: Antonio Gomes <antoniospg100@gmail.com>
gcc/rust/hir/tree/rust-hir-expr.h
gcc/rust/hir/tree/rust-hir.cc
gcc/rust/typecheck/rust-hir-type-check-expr.cc
gcc/testsuite/rust/compile/shadow1.rs

index 9851cb1bb1dc0821e276953d99f9de298690f41f..7f4255d11944c41348482ebc0349acff62fe670a 100644 (file)
@@ -417,6 +417,8 @@ public:
   std::unique_ptr<Expr> &get_lhs () { return main_or_left_expr; }
   std::unique_ptr<Expr> &get_rhs () { return right_expr; }
 
+  std::string get_operator_str () const;
+
 protected:
   /* Use covariance to implement clone function as returning this object rather
    * than base */
@@ -766,6 +768,8 @@ public:
   void visit_lhs (HIRFullVisitor &vis) { main_or_left_expr->accept_vis (vis); }
   void visit_rhs (HIRFullVisitor &vis) { right_expr->accept_vis (vis); }
 
+  std::string get_operator_str () const;
+
 protected:
   /* Use covariance to implement clone function as returning this object rather
    * than base */
index b3de930c307595bb33170c8e9816ed898edb1144..80878fe18a1beabf3819dba189e6ac7dac1348fd 100644 (file)
@@ -1299,7 +1299,7 @@ AssignmentExpr::as_string () const
 }
 
 std::string
-CompoundAssignmentExpr::as_string () const
+CompoundAssignmentExpr::get_operator_str () const
 {
   std::string operator_str;
   operator_str.reserve (1);
@@ -1344,7 +1344,14 @@ CompoundAssignmentExpr::as_string () const
 
   operator_str += "=";
 
+  return operator_str;
+}
+
+std::string
+CompoundAssignmentExpr::as_string () const
+{
   std::string str ("CompoundAssignmentExpr: ");
+  std::string operator_str = get_operator_str ();
   if (main_or_left_expr == nullptr || right_expr == nullptr)
     {
       str += "error. this is probably a parsing failure.";
@@ -1574,7 +1581,7 @@ ErrorPropagationExpr::as_string () const
 }
 
 std::string
-ArithmeticOrLogicalExpr::as_string () const
+ArithmeticOrLogicalExpr::get_operator_str () const
 {
   std::string operator_str;
   operator_str.reserve (1);
@@ -1617,8 +1624,14 @@ ArithmeticOrLogicalExpr::as_string () const
       break;
     }
 
+  return operator_str;
+}
+
+std::string
+ArithmeticOrLogicalExpr::as_string () const
+{
   std::string str = main_or_left_expr->as_string () + " ";
-  str += operator_str + " ";
+  str += get_operator_str () + " ";
   str += right_expr->as_string ();
 
   return "( " + str + " (" + get_mappings ().as_string () + "))";
index 8485f9c3acfa950641a313cbec92cd8dbf17eff0..d4517f3a6d18b5d44cf098807e0d8a8e3af6ad7f 100644 (file)
@@ -272,7 +272,8 @@ TypeCheckExpr::visit (HIR::CompoundAssignmentExpr &expr)
   if (!valid)
     {
       rust_error_at (expr.get_locus (),
-                    "cannot apply this operator to types %s and %s",
+                    "cannot apply operator %qs to types %s and %s",
+                    expr.get_operator_str ().c_str (),
                     lhs->as_string ().c_str (), rhs->as_string ().c_str ());
       return;
     }
@@ -303,7 +304,8 @@ TypeCheckExpr::visit (HIR::ArithmeticOrLogicalExpr &expr)
   if (!valid)
     {
       rust_error_at (expr.get_locus (),
-                    "cannot apply this operator to types %s and %s",
+                    "cannot apply operator %qs to types %s and %s",
+                    expr.get_operator_str ().c_str (),
                     lhs->as_string ().c_str (), rhs->as_string ().c_str ());
       return;
     }
index cef972adf3391e9eaf0f2ba100968a446ae5920f..43d2764a98db3795e095122d884c9c71281e22c6 100644 (file)
@@ -2,5 +2,5 @@ fn main() {
     let mut x = 5;
     let mut x;
     x = true;
-    x = x + 2; // { dg-error "cannot apply this operator to types bool and <integer>"  }
+    x = x + 2; // { dg-error "cannot apply operator .+. to types bool and <integer>"  }
 }