From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 6A5943858414; Sun, 5 Mar 2023 11:42:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6A5943858414 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678016555; bh=90093/t5r7+mH6DbR8Pc+Pe12wxfwvscvwwZAf1+8Mc=; h=From:To:Subject:Date:From; b=a4BxZaCKbjyrLiwz+UUUba5PRDXL4QCYfaneZ3EYxCocth4KXrpL0DfEIEHW6VdTu K8paHilX3VBAM0V7oYjtb/ucoQEoH+BSVcIKk+86/TQGi/ihoYF1lgNq7N50TOfZSG EzpSXtjrfj+ruHbmzSM/kO2cpCEHDjDN2cI6Sg9E= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] gccrs: Fix missing move and copy constructors missing the associated-path X-Act-Checkin: gcc X-Git-Author: Philip Herron X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 22465fbc8cca239aadcb35cd51d820d6d7213238 X-Git-Newrev: 4ab9d9ce3366e51ccfe8a6a1a94e0eafedec5abb Message-Id: <20230305114235.6A5943858414@sourceware.org> Date: Sun, 5 Mar 2023 11:42:35 +0000 (GMT) List-Id: https://gcc.gnu.org/g:4ab9d9ce3366e51ccfe8a6a1a94e0eafedec5abb commit 4ab9d9ce3366e51ccfe8a6a1a94e0eafedec5abb Author: Philip Herron 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 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 (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 (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 (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 (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 { Nothing, } -fn foo() -> Maybe<::A> { +pub fn foo() -> Maybe<::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 { Nothing, } -fn main() { - let a: Maybe<::A> = Maybe::Something(()); +pub fn test() { + let _a: Maybe<::A> = Maybe::Something(()); }