]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Resolve module final self segment in use decls
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Mon, 24 Mar 2025 17:31:12 +0000 (18:31 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Mon, 31 Mar 2025 19:07:17 +0000 (21:07 +0200)
Lowercase self suffix with path was not resolved properly, this should
point to the module right before.

gcc/rust/ChangeLog:

* resolve/rust-forever-stack.hxx: Add a new specialized function
to retrieve the last "real" segment depending on the namespace.
* resolve/rust-forever-stack.h: Add new function prototype.
* resolve/rust-early-name-resolver-2.0.cc (Early::finalize_rebind_import):
Set declared name according to the selected segment, if there is a self
suffix in the use declaration then select the previous segment.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/resolve/rust-early-name-resolver-2.0.cc
gcc/rust/resolve/rust-forever-stack.h
gcc/rust/resolve/rust-forever-stack.hxx

index 492a665f43e9253bbbc1bf056265e2e0e3ef292c..afaca1f71f03313a17ea704449d4994e08a82177 100644 (file)
@@ -417,10 +417,19 @@ Early::finalize_rebind_import (const Early::ImportPair &mapping)
       declared_name = rebind.get_identifier ().as_string ();
       locus = rebind.get_identifier ().get_locus ();
       break;
-    case AST::UseTreeRebind::NewBindType::NONE:
-      declared_name = path.get_final_segment ().as_string ();
-      locus = path.get_final_segment ().get_locus ();
-      break;
+      case AST::UseTreeRebind::NewBindType::NONE: {
+       const auto &segments = path.get_segments ();
+       // We don't want to insert `self` with `use module::self`
+       if (path.get_final_segment ().is_lower_self_seg ())
+         {
+           rust_assert (segments.size () > 1);
+           declared_name = segments[segments.size () - 2].as_string ();
+         }
+       else
+         declared_name = path.get_final_segment ().as_string ();
+       locus = path.get_final_segment ().get_locus ();
+       break;
+      }
     case AST::UseTreeRebind::NewBindType::WILDCARD:
       rust_unreachable ();
       break;
index 2a4c7348728b9793378212d3877aa360c0b9eaed..d86212073b8d23f0ec3fbc1cf1c3822a5d268471 100644 (file)
@@ -795,6 +795,10 @@ private:
     SegIterator<S> iterator,
     std::function<void (const S &, NodeId)> insert_segment_resolution);
 
+  tl::optional<Rib::Definition> resolve_final_segment (Node &final_node,
+                                                      std::string &seg_name,
+                                                      bool is_lower_self);
+
   /* Helper functions for forward resolution (to_canonical_path, to_rib...) */
   struct DfsResult
   {
index a6e0b30a57b11354a12071d0088e639abc282b1d..fbe537d3b41d78db04966ee904af157f8e8d44af 100644 (file)
@@ -594,6 +594,26 @@ ForeverStack<N>::resolve_segments (
   return *current_node;
 }
 
+template <>
+inline tl::optional<Rib::Definition>
+ForeverStack<Namespace::Types>::resolve_final_segment (Node &final_node,
+                                                      std::string &seg_name,
+                                                      bool is_lower_self)
+{
+  if (is_lower_self)
+    return Rib::Definition::NonShadowable (final_node.id);
+  else
+    return final_node.rib.get (seg_name);
+}
+
+template <Namespace N>
+tl::optional<Rib::Definition>
+ForeverStack<N>::resolve_final_segment (Node &final_node, std::string &seg_name,
+                                       bool is_lower_self)
+{
+  return final_node.rib.get (seg_name);
+}
+
 template <Namespace N>
 template <typename S>
 tl::optional<Rib::Definition>
@@ -643,12 +663,13 @@ ForeverStack<N>::resolve_path (
       if (final_node.rib.kind == Rib::Kind::TraitOrImpl)
        return tl::nullopt;
 
-      std::string seg_name
-       = unwrap_type_segment (segments.back ()).as_string ();
+      auto &seg = unwrap_type_segment (segments.back ());
+      std::string seg_name = seg.as_string ();
 
       // assuming this can't be a lang item segment
-      tl::optional<Rib::Definition> res = final_node.rib.get (seg_name);
-
+      tl::optional<Rib::Definition> res
+       = resolve_final_segment (final_node, seg_name,
+                                seg.is_lower_self_seg ());
       // Ok we didn't find it in the rib, Lets try the prelude...
       if (!res)
        res = get_prelude (seg_name);