public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc/devel/rust/master] parser: Fix while let expr parsing
Date: Sun,  5 Mar 2023 11:42:15 +0000 (GMT)	[thread overview]
Message-ID: <20230305114215.209E03858436@sourceware.org> (raw)

https://gcc.gnu.org/g:26b19976c898dd08f7234dc179ad57085f974b1a

commit 26b19976c898dd08f7234dc179ad57085f974b1a
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Wed Mar 1 17:45:51 2023 +0100

    parser: Fix while let expr parsing
    
    While let expr return unit but are valid construct in rust, they should
    therefore be included in the parsing code. Also add a new test to check
    parsing of while let expressions.
    
    gcc/rust/ChangeLog:
    
            * parse/rust-parse-impl.h (Parser::parse_while_let_loop_expr):
            Prevent hard error on token skip.
            (Parser::null_denotation): Fix parser for while let expressions.
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/while_let_expr.rs: New test.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Diff:
---
 gcc/rust/parse/rust-parse-impl.h             | 15 +++++++++++----
 gcc/testsuite/rust/compile/while_let_expr.rs | 13 +++++++++++++
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index b17744fedaa..46704111f37 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -8372,7 +8372,7 @@ Parser<ManagedTokenSource>::parse_while_let_loop_expr (AST::AttrVec outer_attrs,
     locus = lexer.peek_token ()->get_locus ();
   else
     locus = label.get_locus ();
-  skip_token (WHILE);
+  maybe_skip_token (WHILE);
 
   /* check for possible accidental recognition of a while loop as a while let
    * loop */
@@ -13114,9 +13114,16 @@ Parser<ManagedTokenSource>::null_denotation (const_TokenPtr tok,
       return parse_loop_expr (std::move (outer_attrs), AST::LoopLabel::error (),
 			      tok->get_locus ());
     case WHILE:
-      return parse_while_loop_expr (std::move (outer_attrs),
-				    AST::LoopLabel::error (),
-				    tok->get_locus ());
+      if (lexer.peek_token ()->get_id () == LET)
+	{
+	  return parse_while_let_loop_expr (std::move (outer_attrs));
+	}
+      else
+	{
+	  return parse_while_loop_expr (std::move (outer_attrs),
+					AST::LoopLabel::error (),
+					tok->get_locus ());
+	}
     case MATCH_TOK:
       // also an expression with block
       return parse_match_expr (std::move (outer_attrs), tok->get_locus ());
diff --git a/gcc/testsuite/rust/compile/while_let_expr.rs b/gcc/testsuite/rust/compile/while_let_expr.rs
new file mode 100644
index 00000000000..113ad743cc2
--- /dev/null
+++ b/gcc/testsuite/rust/compile/while_let_expr.rs
@@ -0,0 +1,13 @@
+// { dg-options "-fsyntax-only" }
+
+pub enum Option<T> {
+    None,
+    Some(T),
+}
+
+fn main() {
+    let mut x = Option::Some(3);
+    let a = while let Option::Some(1) = x {
+        x = Option::None;
+    };
+}

                 reply	other threads:[~2023-03-05 11: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=20230305114215.209E03858436@sourceware.org \
    --to=tschwinge@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.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).