public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Ensure TupleStructPattern and TuplePattern have items
@ 2024-05-07 16:16 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2024-05-07 16:16 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:dae69e86c0feafd4e67bec2309c8e14b82f935ac

commit dae69e86c0feafd4e67bec2309c8e14b82f935ac
Author: Owen Avery <powerboat9.gamer@gmail.com>
Date:   Tue Feb 27 16:34:23 2024 -0500

    Ensure TupleStructPattern and TuplePattern have items
    
    Note that instances of both classes which have been
    moved from will have (items == nullptr).
    
    gcc/rust/ChangeLog:
    
            * ast/rust-pattern.h
            (class TupleStructPattern): Assert that items != nullptr.
            (class TuplePattern): Likewise.
            (TupleStructPattern::has_items): Remove.
            (TuplePattern::has_tuple_pattern_items): Likewise.
            * parse/rust-parse-impl.h
            (Parser::parse_ident_leading_pattern):
            Prevent construction of TupleStructPattern with
            (items == nullptr).
            (Parser::parse_pattern_no_alt): Likewise.
            * ast/rust-ast-collector.cc
            (TokenCollector::visit): Remove usage of
            TupleStructPattern::has_items.
            * ast/rust-ast-visitor.cc
            (DefaultASTVisitor::visit): Likewise.
            * resolve/rust-early-name-resolver.cc
            (EarlyNameResolver::visit): Likewise.
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/pattern-struct.rs: Fix test.
    
    Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>

Diff:
---
 gcc/rust/ast/rust-ast-collector.cc           |  3 +-
 gcc/rust/ast/rust-ast-visitor.cc             |  3 +-
 gcc/rust/ast/rust-pattern.h                  | 46 ++++++++++++++--------------
 gcc/rust/parse/rust-parse-impl.h             | 16 ----------
 gcc/rust/resolve/rust-early-name-resolver.cc | 10 ------
 gcc/testsuite/rust/compile/pattern-struct.rs |  2 +-
 6 files changed, 26 insertions(+), 54 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-collector.cc b/gcc/rust/ast/rust-ast-collector.cc
index c0e8e774824a..744d0eb9d289 100644
--- a/gcc/rust/ast/rust-ast-collector.cc
+++ b/gcc/rust/ast/rust-ast-collector.cc
@@ -2503,8 +2503,7 @@ TokenCollector::visit (TupleStructPattern &pattern)
 {
   visit (pattern.get_path ());
   push (Rust::Token::make (LEFT_PAREN, pattern.get_locus ()));
-  if (pattern.has_items ())
-    visit (pattern.get_items ());
+  visit (pattern.get_items ());
   push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
 }
 
diff --git a/gcc/rust/ast/rust-ast-visitor.cc b/gcc/rust/ast/rust-ast-visitor.cc
index c72e2d72f6d2..697c27263098 100644
--- a/gcc/rust/ast/rust-ast-visitor.cc
+++ b/gcc/rust/ast/rust-ast-visitor.cc
@@ -1225,8 +1225,7 @@ void
 DefaultASTVisitor::visit (AST::TupleStructPattern &pattern)
 {
   visit (pattern.get_path ());
-  if (pattern.has_items ())
-    visit (pattern.get_items ());
+  visit (pattern.get_items ());
 }
 
 void
diff --git a/gcc/rust/ast/rust-pattern.h b/gcc/rust/ast/rust-pattern.h
index 6a90b5361758..96f09355fae0 100644
--- a/gcc/rust/ast/rust-pattern.h
+++ b/gcc/rust/ast/rust-pattern.h
@@ -1123,22 +1123,22 @@ class TupleStructPattern : public Pattern
 public:
   std::string as_string () const override;
 
-  // Returns whether the pattern has tuple struct items.
-  bool has_items () const { return items != nullptr; }
-
   TupleStructPattern (PathInExpression tuple_struct_path,
 		      std::unique_ptr<TupleStructItems> items)
     : path (std::move (tuple_struct_path)), items (std::move (items)),
       node_id (Analysis::Mappings::get ()->get_next_node_id ())
-  {}
+  {
+    rust_assert (this->items != nullptr);
+  }
 
   // Copy constructor required to clone
   TupleStructPattern (TupleStructPattern const &other) : path (other.path)
   {
     // guard to protect from null dereference
+    rust_assert (other.items != nullptr);
+
     node_id = other.node_id;
-    if (other.items != nullptr)
-      items = other.items->clone_tuple_struct_items ();
+    items = other.items->clone_tuple_struct_items ();
   }
 
   // Operator overload assignment operator to clone
@@ -1148,10 +1148,9 @@ public:
     node_id = other.node_id;
 
     // guard to protect from null dereference
-    if (other.items != nullptr)
-      items = other.items->clone_tuple_struct_items ();
-    else
-      items = nullptr;
+    rust_assert (other.items != nullptr);
+
+    items = other.items->clone_tuple_struct_items ();
 
     return *this;
   }
@@ -1164,7 +1163,11 @@ public:
 
   void accept_vis (ASTVisitor &vis) override;
 
-  std::unique_ptr<TupleStructItems> &get_items () { return items; }
+  std::unique_ptr<TupleStructItems> &get_items ()
+  {
+    rust_assert (items != nullptr);
+    return items;
+  }
 
   PathInExpression &get_path () { return path; }
   const PathInExpression &get_path () const { return path; }
@@ -1358,7 +1361,6 @@ protected:
 // AST node representing a tuple pattern
 class TuplePattern : public Pattern
 {
-  // bool has_tuple_pattern_items;
   std::unique_ptr<TuplePatternItems> items;
   location_t locus;
   NodeId node_id;
@@ -1366,21 +1368,21 @@ class TuplePattern : public Pattern
 public:
   std::string as_string () const override;
 
-  // Returns true if the tuple pattern has items
-  bool has_tuple_pattern_items () const { return items != nullptr; }
-
   TuplePattern (std::unique_ptr<TuplePatternItems> items, location_t locus)
     : items (std::move (items)), locus (locus),
       node_id (Analysis::Mappings::get ()->get_next_node_id ())
-  {}
+  {
+    rust_assert (this->items != nullptr);
+  }
 
   // Copy constructor requires clone
   TuplePattern (TuplePattern const &other) : locus (other.locus)
   {
     // guard to prevent null dereference
+    rust_assert (other.items != nullptr);
+
     node_id = other.node_id;
-    if (other.items != nullptr)
-      items = other.items->clone_tuple_pattern_items ();
+    items = other.items->clone_tuple_pattern_items ();
   }
 
   // Overload assignment operator to clone
@@ -1390,11 +1392,9 @@ public:
     node_id = other.node_id;
 
     // guard to prevent null dereference
-    if (other.items != nullptr)
-      items = other.items->clone_tuple_pattern_items ();
-    else
-      items = nullptr;
+    rust_assert (other.items != nullptr);
 
+    items = other.items->clone_tuple_pattern_items ();
     return *this;
   }
 
@@ -1405,7 +1405,7 @@ public:
   // TODO: seems kinda dodgy. Think of better way.
   std::unique_ptr<TuplePatternItems> &get_items ()
   {
-    rust_assert (has_tuple_pattern_items ());
+    rust_assert (items != nullptr);
     return items;
   }
 
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index ac1754542d43..9d9722e97142 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -10631,14 +10631,6 @@ Parser<ManagedTokenSource>::parse_pattern_no_alt ()
 	      // tuple struct
 	      lexer.skip_token ();
 
-	      // check if empty tuple
-	      if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
-		{
-		  lexer.skip_token ();
-		  return std::unique_ptr<AST::TupleStructPattern> (
-		    new AST::TupleStructPattern (std::move (path), nullptr));
-		}
-
 	      // parse items
 	      std::unique_ptr<AST::TupleStructItems> items
 		= parse_tuple_struct_items ();
@@ -11094,14 +11086,6 @@ Parser<ManagedTokenSource>::parse_ident_leading_pattern ()
 	// DEBUG
 	rust_debug ("parsing tuple struct pattern");
 
-	// check if empty tuple
-	if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
-	  {
-	    lexer.skip_token ();
-	    return std::unique_ptr<AST::TupleStructPattern> (
-	      new AST::TupleStructPattern (std::move (path), nullptr));
-	  }
-
 	// parse items
 	std::unique_ptr<AST::TupleStructItems> items
 	  = parse_tuple_struct_items ();
diff --git a/gcc/rust/resolve/rust-early-name-resolver.cc b/gcc/rust/resolve/rust-early-name-resolver.cc
index d70f9ca9806e..5447084cfddf 100644
--- a/gcc/rust/resolve/rust-early-name-resolver.cc
+++ b/gcc/rust/resolve/rust-early-name-resolver.cc
@@ -558,16 +558,6 @@ EarlyNameResolver::visit (AST::StructPattern &)
 void
 EarlyNameResolver::visit (AST::TupleStructPattern &pattern)
 {
-  if (!pattern.has_items ())
-    {
-      rich_location rich_locus (line_table, pattern.get_locus ());
-      rich_locus.add_fixit_replace (
-	"function calls are not allowed in patterns");
-      rust_error_at (
-	rich_locus, ErrorCode::E0164,
-	"expected tuple struct or tuple variant, found associated function");
-      return;
-    }
   pattern.get_items ()->accept_vis (*this);
 }
 
diff --git a/gcc/testsuite/rust/compile/pattern-struct.rs b/gcc/testsuite/rust/compile/pattern-struct.rs
index 17275098cd2c..db242418c1d3 100644
--- a/gcc/testsuite/rust/compile/pattern-struct.rs
+++ b/gcc/testsuite/rust/compile/pattern-struct.rs
@@ -11,7 +11,7 @@ fn main() {
     fn bar(foo: A) {
         match foo {
             A::new() => (), 
-            // { dg-error "expected tuple struct or tuple variant, found associated function" "" { target *-*-* } .-1 }
+            // { dg-error "expected tuple struct or tuple variant, found function" "" { target *-*-* } .-1 }
             _ => {}
         }
     }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-05-07 16:16 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-07 16:16 [gcc/devel/rust/master] Ensure TupleStructPattern and TuplePattern have items Thomas Schwinge

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).