public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] gccrs: Fix missing move and copy constructors missing the associated-path
@ 2023-03-05 11:42 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-03-05 11:42 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:4ab9d9ce3366e51ccfe8a6a1a94e0eafedec5abb

commit 4ab9d9ce3366e51ccfe8a6a1a94e0eafedec5abb
Author: Philip Herron <herron.philip@googlemail.com>
Date:   Wed Mar 1 22:09:47 2023 +0000

    gccrs: Fix missing move and copy constructors missing the associated-path
    
    Addresses #1524
    
    Signed-off-by: Philip Herron <herron.philip@googlemail.com>
    
    gcc/rust/ChangeLog:
    
            * ast/rust-ast.cc (QualifiedPathInType::as_string): add missing to string
            * ast/rust-path.h: add missing copy+move constructors and assignment overloads
            * hir/tree/rust-hir-path.h: likewise
            * hir/tree/rust-hir.cc (QualifiedPathInType::as_string): add missing to string
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/parse_associated_type_as_generic_arg.rs: it now works without -fsyntax-only
            * rust/compile/parse_associated_type_as_generic_arg2.rs: likewise

Diff:
---
 gcc/rust/ast/rust-ast.cc                           |  1 +
 gcc/rust/ast/rust-path.h                           | 72 +++++++++++++++++++---
 gcc/rust/hir/tree/rust-hir-path.h                  | 15 +++--
 gcc/rust/hir/tree/rust-hir.cc                      |  1 +
 .../parse_associated_type_as_generic_arg.rs        |  6 +-
 .../parse_associated_type_as_generic_arg2.rs       |  8 +--
 6 files changed, 77 insertions(+), 26 deletions(-)

diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc
index 3d1b573968f..bb3562a73a3 100644
--- a/gcc/rust/ast/rust-ast.cc
+++ b/gcc/rust/ast/rust-ast.cc
@@ -2397,6 +2397,7 @@ QualifiedPathInType::as_string () const
    * literalised */
   std::string str = path_type.as_string ();
 
+  str += "::" + associated_segment->as_string ();
   for (const auto &segment : segments)
     str += "::" + segment->as_string ();
 
diff --git a/gcc/rust/ast/rust-path.h b/gcc/rust/ast/rust-path.h
index b5c9c3aab3a..5f7c5e35f25 100644
--- a/gcc/rust/ast/rust-path.h
+++ b/gcc/rust/ast/rust-path.h
@@ -202,6 +202,9 @@ public:
     return *this;
   }
 
+  GenericArg (GenericArg &&other) = default;
+  GenericArg &operator= (GenericArg &&other) = default;
+
   bool is_error () const { return kind == Kind::Error; }
 
   Kind get_kind () const { return kind; }
@@ -411,9 +414,16 @@ public:
 
   // copy constructor with vector clone
   GenericArgs (GenericArgs const &other)
-    : lifetime_args (other.lifetime_args), generic_args (other.generic_args),
-      binding_args (other.binding_args), locus (other.locus)
-  {}
+    : lifetime_args (other.lifetime_args), binding_args (other.binding_args),
+      locus (other.locus)
+  {
+    generic_args.clear ();
+    generic_args.reserve (other.generic_args.size ());
+    for (const auto &arg : other.generic_args)
+      {
+	generic_args.push_back (GenericArg (arg));
+      }
+  }
 
   ~GenericArgs () = default;
 
@@ -421,10 +431,16 @@ public:
   GenericArgs &operator= (GenericArgs const &other)
   {
     lifetime_args = other.lifetime_args;
-    generic_args = other.generic_args;
     binding_args = other.binding_args;
     locus = other.locus;
 
+    generic_args.clear ();
+    generic_args.reserve (other.generic_args.size ());
+    for (const auto &arg : other.generic_args)
+      {
+	generic_args.push_back (GenericArg (arg));
+      }
+
     return *this;
   }
 
@@ -672,6 +688,7 @@ protected:
   bool has_separating_scope_resolution;
   NodeId node_id;
 
+public:
   // Clone function implementation - not pure virtual as overrided by
   // subclasses
   virtual TypePathSegment *clone_type_path_segment_impl () const
@@ -705,6 +722,25 @@ public:
       node_id (Analysis::Mappings::get ()->get_next_node_id ())
   {}
 
+  TypePathSegment (TypePathSegment const &other)
+    : ident_segment (other.ident_segment), locus (other.locus),
+      has_separating_scope_resolution (other.has_separating_scope_resolution),
+      node_id (other.node_id)
+  {}
+
+  TypePathSegment &operator= (TypePathSegment const &other)
+  {
+    ident_segment = other.ident_segment;
+    locus = other.locus;
+    has_separating_scope_resolution = other.has_separating_scope_resolution;
+    node_id = other.node_id;
+
+    return *this;
+  }
+
+  TypePathSegment (TypePathSegment &&other) = default;
+  TypePathSegment &operator= (TypePathSegment &&other) = default;
+
   virtual std::string as_string () const { return ident_segment.as_string (); }
 
   /* Returns whether the type path segment is in an error state. May be
@@ -780,6 +816,23 @@ public:
 				 std::move (binding_args)))
   {}
 
+  // Copy constructor with vector clone
+  TypePathSegmentGeneric (TypePathSegmentGeneric const &other)
+    : TypePathSegment (other), generic_args (other.generic_args)
+  {}
+
+  // Overloaded assignment operator with vector clone
+  TypePathSegmentGeneric &operator= (TypePathSegmentGeneric const &other)
+  {
+    generic_args = other.generic_args;
+
+    return *this;
+  }
+
+  // move constructors
+  TypePathSegmentGeneric (TypePathSegmentGeneric &&other) = default;
+  TypePathSegmentGeneric &operator= (TypePathSegmentGeneric &&other) = default;
+
   std::string as_string () const override;
 
   void accept_vis (ASTVisitor &vis) override;
@@ -791,7 +844,6 @@ public:
     return generic_args;
   }
 
-protected:
   // Use covariance to override base class method
   TypePathSegmentGeneric *clone_type_path_segment_impl () const override
   {
@@ -941,7 +993,6 @@ public:
     return function_path;
   }
 
-protected:
   // Use covariance to override base class method
   TypePathSegmentFunction *clone_type_path_segment_impl () const override
   {
@@ -1242,13 +1293,13 @@ public:
       segments (std::move (path_segments)), locus (locus)
   {}
 
-  /* TODO: maybe make a shortcut constructor that has QualifiedPathType
-   * elements as params */
-
   // Copy constructor with vector clone
   QualifiedPathInType (QualifiedPathInType const &other)
     : path_type (other.path_type), locus (other.locus)
   {
+    auto seg = other.associated_segment->clone_type_path_segment_impl ();
+    associated_segment = std::unique_ptr<TypePathSegment> (seg);
+
     segments.reserve (other.segments.size ());
     for (const auto &e : other.segments)
       segments.push_back (e->clone_type_path_segment ());
@@ -1257,6 +1308,9 @@ public:
   // Overloaded assignment operator with vector clone
   QualifiedPathInType &operator= (QualifiedPathInType const &other)
   {
+    auto seg = other.associated_segment->clone_type_path_segment_impl ();
+    associated_segment = std::unique_ptr<TypePathSegment> (seg);
+
     path_type = other.path_type;
     locus = other.locus;
 
diff --git a/gcc/rust/hir/tree/rust-hir-path.h b/gcc/rust/hir/tree/rust-hir-path.h
index 740de9391a0..f8a7dab0001 100644
--- a/gcc/rust/hir/tree/rust-hir-path.h
+++ b/gcc/rust/hir/tree/rust-hir-path.h
@@ -431,6 +431,7 @@ protected:
   bool has_separating_scope_resolution;
   SegmentType type;
 
+public:
   // Clone function implementation - not pure virtual as overrided by subclasses
   virtual TypePathSegment *clone_type_path_segment_impl () const
   {
@@ -538,7 +539,6 @@ public:
     return SegmentType::GENERIC;
   }
 
-protected:
   // Use covariance to override base class method
   TypePathSegmentGeneric *clone_type_path_segment_impl () const override
   {
@@ -654,7 +654,6 @@ public:
 
   TypePathFunction &get_function_path () { return function_path; }
 
-protected:
   // Use covariance to override base class method
   TypePathSegmentFunction *clone_type_path_segment_impl () const override
   {
@@ -933,24 +932,24 @@ public:
       segments (std::move (path_segments))
   {}
 
-  /* TODO: maybe make a shortcut constructor that has QualifiedPathType elements
-   * as params */
-
   // Copy constructor with vector clone
   QualifiedPathInType (QualifiedPathInType const &other)
     : TypeNoBounds (other.mappings, other.locus), path_type (other.path_type)
   {
+    auto seg = other.associated_segment->clone_type_path_segment_impl ();
+    associated_segment = std::unique_ptr<TypePathSegment> (seg);
+
     segments.reserve (other.segments.size ());
     for (const auto &e : other.segments)
       segments.push_back (e->clone_type_path_segment ());
-
-    // Untested.
-    gcc_unreachable ();
   }
 
   // Overloaded assignment operator with vector clone
   QualifiedPathInType &operator= (QualifiedPathInType const &other)
   {
+    auto seg = other.associated_segment->clone_type_path_segment_impl ();
+    associated_segment = std::unique_ptr<TypePathSegment> (seg);
+
     path_type = other.path_type;
     locus = other.locus;
     mappings = other.mappings;
diff --git a/gcc/rust/hir/tree/rust-hir.cc b/gcc/rust/hir/tree/rust-hir.cc
index 0ddb8418e95..3a4362f92b4 100644
--- a/gcc/rust/hir/tree/rust-hir.cc
+++ b/gcc/rust/hir/tree/rust-hir.cc
@@ -2112,6 +2112,7 @@ QualifiedPathInType::as_string () const
 {
   std::string str = path_type.as_string ();
 
+  str += "::" + associated_segment->as_string ();
   for (const auto &segment : segments)
     {
       str += "::" + segment->as_string ();
diff --git a/gcc/testsuite/rust/compile/parse_associated_type_as_generic_arg.rs b/gcc/testsuite/rust/compile/parse_associated_type_as_generic_arg.rs
index dc05c068359..fbe79f00bde 100644
--- a/gcc/testsuite/rust/compile/parse_associated_type_as_generic_arg.rs
+++ b/gcc/testsuite/rust/compile/parse_associated_type_as_generic_arg.rs
@@ -1,12 +1,10 @@
-// { dg-additional-options "-fsyntax-only" }
-
 trait Foo {
     type A;
 
     fn foo();
 }
 
-struct S;
+struct S; // { dg-warning "struct is never constructed" }
 
 impl Foo for S {
     type A = i32;
@@ -19,6 +17,6 @@ enum Maybe<T> {
     Nothing,
 }
 
-fn foo() -> Maybe<<S as Foo>::A> {
+pub fn foo() -> Maybe<<S as Foo>::A> {
     Maybe::Something(15)
 }
diff --git a/gcc/testsuite/rust/compile/parse_associated_type_as_generic_arg2.rs b/gcc/testsuite/rust/compile/parse_associated_type_as_generic_arg2.rs
index 9c518a03ca7..ba5d9a3936c 100644
--- a/gcc/testsuite/rust/compile/parse_associated_type_as_generic_arg2.rs
+++ b/gcc/testsuite/rust/compile/parse_associated_type_as_generic_arg2.rs
@@ -1,12 +1,10 @@
-// { dg-additional-options "-fsyntax-only" }
-
 trait Foo {
     type A;
 
     fn foo();
 }
 
-struct S;
+struct S; // { dg-warning "struct is never constructed" }
 
 impl Foo for S {
     type A = ();
@@ -19,6 +17,6 @@ enum Maybe<T> {
     Nothing,
 }
 
-fn main() {
-    let a: Maybe<<S as Foo>::A> = Maybe::Something(());
+pub fn test() {
+    let _a: Maybe<<S as Foo>::A> = Maybe::Something(());
 }

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

only message in thread, other threads:[~2023-03-05 11:42 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-05 11:42 [gcc/devel/rust/master] gccrs: Fix missing move and copy constructors missing the associated-path 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).