Skip to content

Commit

Permalink
Improve error messages for operator expressions
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
antoniospg authored and P-E-P committed Jul 17, 2024
1 parent c1ba898 commit 5f001ba
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
4 changes: 4 additions & 0 deletions gcc/rust/hir/tree/rust-hir-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ class ArithmeticOrLogicalExpr : public OperatorExpr
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 */
Expand Down Expand Up @@ -766,6 +768,8 @@ class CompoundAssignmentExpr : public OperatorExpr
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 */
Expand Down
19 changes: 16 additions & 3 deletions gcc/rust/hir/tree/rust-hir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.";
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 () + "))";
Expand Down
6 changes: 4 additions & 2 deletions gcc/rust/typecheck/rust-hir-type-check-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion gcc/testsuite/rust/compile/shadow1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>" }
}

0 comments on commit 5f001ba

Please sign in to comment.