From 4ab9d9ce3366e51ccfe8a6a1a94e0eafedec5abb Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Wed, 1 Mar 2023 22:09:47 +0000 Subject: 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 --- 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(-) (limited to 'gcc') diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc index 3d1b573..bb3562a 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 b5c9c3a..5f7c5e3 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 740de93..f8a7dab 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 0ddb841..3a4362f 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 dc05c06..fbe79f0 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 9c518a03..ba5d9a3 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(()); } -- cgit v1.1