]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix undefined behaviour issues on macos
authorPhilip Herron <philip.herron@embecosm.com>
Mon, 7 Nov 2022 13:43:04 +0000 (13:43 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 21 Feb 2023 11:36:52 +0000 (12:36 +0100)
This adds missing copy constructors to HIR::PathExprSegment which were
wrongly defaulting to empty vectors when apply specified generic arguments
to method calls.

gcc/rust/ChangeLog:

* hir/tree/rust-hir-expr.h: Add const `get_method_name`.
* hir/tree/rust-hir-full-decls.h (struct GenericArgs): Move from `struct`...
(class GenericArgs): ...to `class`.
* hir/tree/rust-hir-path.h (struct GenericArgs): Likewise.
(class GenericArgs): Clear `type_args` in copy constructor.
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Reorder
debug print.
* typecheck/rust-tyty.h: Add default constructors for `SubstitutionArgumentMappings`.

gcc/rust/hir/tree/rust-hir-expr.h
gcc/rust/hir/tree/rust-hir-full-decls.h
gcc/rust/hir/tree/rust-hir-path.h
gcc/rust/typecheck/rust-hir-type-check-expr.cc
gcc/rust/typecheck/rust-tyty.h

index 227bacbe64186f2f5d8ea5742f2874c78b92e955..cc884420bef266ea27a254f1efae6bec9c675b78 100644 (file)
@@ -1872,7 +1872,8 @@ public:
 
   std::unique_ptr<Expr> &get_receiver () { return receiver; }
 
-  PathExprSegment get_method_name () const { return method_name; };
+  PathExprSegment &get_method_name () { return method_name; };
+  const PathExprSegment &get_method_name () const { return method_name; };
 
   size_t num_params () const { return params.size (); }
 
index 70ee7538298ef363a199d62a88562b0dc6784bf5..7870a5497edf7aabfcf52cc3f12f5d926c002155 100644 (file)
@@ -43,7 +43,7 @@ class PathExpr;
 // rust-path.h
 class PathIdentSegment;
 struct GenericArgsBinding;
-struct GenericArgs;
+class GenericArgs;
 class PathExprSegment;
 class PathPattern;
 class PathInExpression;
index fa8347b2cf875fa59739a068bdcc52a51f40affb..17eedb8d741bfc400e10f8c24a51e8d23f65108a 100644 (file)
@@ -140,8 +140,7 @@ private:
   Location locus;
 };
 
-// Generic arguments allowed in each path expression segment - inline?
-struct GenericArgs
+class GenericArgs
 {
   std::vector<Lifetime> lifetime_args;
   std::vector<std::unique_ptr<Type> > type_args;
@@ -172,6 +171,7 @@ public:
     : lifetime_args (other.lifetime_args), binding_args (other.binding_args),
       const_args (other.const_args), locus (other.locus)
   {
+    type_args.clear ();
     type_args.reserve (other.type_args.size ());
 
     for (const auto &e : other.type_args)
@@ -188,6 +188,7 @@ public:
     const_args = other.const_args;
     locus = other.locus;
 
+    type_args.clear ();
     type_args.reserve (other.type_args.size ());
     for (const auto &e : other.type_args)
       type_args.push_back (e->clone_type ());
@@ -235,26 +236,44 @@ private:
   Location locus;
 
 public:
-  // Returns true if there are any generic arguments
-  bool has_generic_args () const { return generic_args.has_generic_args (); }
-
-  // Constructor for segment (from IdentSegment and GenericArgs)
   PathExprSegment (Analysis::NodeMapping mappings,
-                  PathIdentSegment segment_name, Location locus = Location (),
-                  GenericArgs generic_args = GenericArgs::create_empty ())
+                  PathIdentSegment segment_name, Location locus,
+                  GenericArgs generic_args)
     : mappings (std::move (mappings)), segment_name (std::move (segment_name)),
       generic_args (std::move (generic_args)), locus (locus)
   {}
 
+  PathExprSegment (PathExprSegment const &other)
+    : mappings (other.mappings), segment_name (other.segment_name),
+      generic_args (other.generic_args), locus (other.locus)
+  {}
+
+  PathExprSegment &operator= (PathExprSegment const &other)
+  {
+    mappings = other.mappings;
+    segment_name = other.segment_name;
+    generic_args = other.generic_args;
+    locus = other.locus;
+
+    return *this;
+  }
+
+  // move constructors
+  PathExprSegment (PathExprSegment &&other) = default;
+  PathExprSegment &operator= (PathExprSegment &&other) = default;
+
   std::string as_string () const;
 
   Location get_locus () const { return locus; }
 
-  PathIdentSegment get_segment () const { return segment_name; }
+  PathIdentSegment &get_segment () { return segment_name; }
+  const PathIdentSegment &get_segment () const { return segment_name; }
 
   GenericArgs &get_generic_args () { return generic_args; }
 
   const Analysis::NodeMapping &get_mappings () const { return mappings; }
+
+  bool has_generic_args () const { return generic_args.has_generic_args (); }
 };
 
 // HIR node representing a pattern that involves a "path" - abstract base class
index 0b0db3296a0e28d6535379975da7cac0784cbbe7..5c43cc8c7b44d89c2ece874cef1269b3ede5fd61 100644 (file)
@@ -1149,10 +1149,11 @@ TypeCheckExpr::visit (HIR::MethodCallExpr &expr)
   // apply any remaining generic arguments
   if (expr.get_method_name ().has_generic_args ())
     {
-      rust_debug_loc (expr.get_method_name ().get_generic_args ().get_locus (),
+      HIR::GenericArgs &args = expr.get_method_name ().get_generic_args ();
+      rust_debug_loc (args.get_locus (),
                      "applying generic arguments to method_call: {%s}",
                      lookup->debug_str ().c_str ());
-      HIR::GenericArgs &args = expr.get_method_name ().get_generic_args ();
+
       lookup
        = SubstMapper::Resolve (lookup, expr.get_method_name ().get_locus (),
                                &args);
index b9a1fdfa5c73b8017ab47cb9c1823b6d857af12c..0fd664c8d46b2b246397e20c7a2b311063843de4 100644 (file)
@@ -699,6 +699,10 @@ public:
     return *this;
   }
 
+  SubstitutionArgumentMappings (SubstitutionArgumentMappings &&other) = default;
+  SubstitutionArgumentMappings &operator= (SubstitutionArgumentMappings &&other)
+    = default;
+
   static SubstitutionArgumentMappings error ()
   {
     return SubstitutionArgumentMappings ({}, Location (), nullptr, false);