Another Rust operator precedence bug

My earlier patch to fix PR rust/29859 introduced a new operator
precedence bug in the Rust parser.  Assignment operators are
right-associative in Rust.  And, while this doesn't often matter, as
Rust assignments always have the value (), still as a matter of
principle we should get this correct.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29859
This commit is contained in:
Tom Tromey
2022-12-12 06:36:55 -07:00
parent 167f3beb65
commit 67a8c89601
2 changed files with 11 additions and 2 deletions

View File

@ -1344,6 +1344,8 @@ rust_parser::parse_binop (bool required)
OPERATION (ANDAND, 2, logical_and_operation) \ OPERATION (ANDAND, 2, logical_and_operation) \
OPERATION (OROR, 1, logical_or_operation) OPERATION (OROR, 1, logical_or_operation)
#define ASSIGN_PREC 0
operation_up start = parse_atom (required); operation_up start = parse_atom (required);
if (start == nullptr) if (start == nullptr)
{ {
@ -1376,7 +1378,7 @@ rust_parser::parse_binop (bool required)
compound_assign_op = current_opcode; compound_assign_op = current_opcode;
/* FALLTHROUGH */ /* FALLTHROUGH */
case '=': case '=':
precedence = 0; precedence = ASSIGN_PREC;
lex (); lex ();
break; break;
@ -1398,7 +1400,11 @@ rust_parser::parse_binop (bool required)
break; break;
} }
while (precedence <= operator_stack.back ().precedence /* Make sure that assignments are right-associative while other
operations are left-associative. */
while ((precedence == ASSIGN_PREC
? precedence < operator_stack.back ().precedence
: precedence <= operator_stack.back ().precedence)
&& operator_stack.size () > 1) && operator_stack.size () > 1)
{ {
rustop_item rhs = std::move (operator_stack.back ()); rustop_item rhs = std::move (operator_stack.back ());

View File

@ -416,3 +416,6 @@ if {[lindex $v 0] >= 8} {
# The new parser introduced an operator precedence bug. # The new parser introduced an operator precedence bug.
gdb_test "print 5 * 7 / 5" " = 7" gdb_test "print 5 * 7 / 5" " = 7"
gdb_test "print 4 - 3 - 1" " = 0" gdb_test "print 4 - 3 - 1" " = 0"
# Another operator precedence bug.
gdb_test "print \$one = \$two = 75" " = \\\(\\\)"