public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [pushed] Another Rust operator precedence bug
Date: Mon, 12 Dec 2022 06:41:50 -0700	[thread overview]
Message-ID: <20221212134150.375769-1-tom@tromey.com> (raw)

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
---
 gdb/rust-parse.c                  | 10 ++++++++--
 gdb/testsuite/gdb.rust/simple.exp |  3 +++
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/gdb/rust-parse.c b/gdb/rust-parse.c
index 337927219d5..f28514ab2da 100644
--- a/gdb/rust-parse.c
+++ b/gdb/rust-parse.c
@@ -1344,6 +1344,8 @@ rust_parser::parse_binop (bool required)
   OPERATION (ANDAND, 2, logical_and_operation)	\
   OPERATION (OROR, 1, logical_or_operation)
 
+#define ASSIGN_PREC 0
+
   operation_up start = parse_atom (required);
   if (start == nullptr)
     {
@@ -1376,7 +1378,7 @@ rust_parser::parse_binop (bool required)
 	  compound_assign_op = current_opcode;
 	  /* FALLTHROUGH */
 	case '=':
-	  precedence = 0;
+	  precedence = ASSIGN_PREC;
 	  lex ();
 	  break;
 
@@ -1398,7 +1400,11 @@ rust_parser::parse_binop (bool required)
 	  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)
 	{
 	  rustop_item rhs = std::move (operator_stack.back ());
diff --git a/gdb/testsuite/gdb.rust/simple.exp b/gdb/testsuite/gdb.rust/simple.exp
index 3a010f30ea6..0fb06af9380 100644
--- a/gdb/testsuite/gdb.rust/simple.exp
+++ b/gdb/testsuite/gdb.rust/simple.exp
@@ -416,3 +416,6 @@ if {[lindex $v 0] >= 8} {
 # The new parser introduced an operator precedence bug.
 gdb_test "print 5 * 7 / 5" " = 7"
 gdb_test "print 4 - 3 - 1" " = 0"
+
+# Another operator precedence bug.
+gdb_test "print \$one = \$two = 75" " = \\\(\\\)"
-- 
2.38.1


                 reply	other threads:[~2022-12-12 13:42 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221212134150.375769-1-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).