]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: nr: Properly resolve imports and modules in segments
authorArthur Cohen <arthur.cohen@embecosm.com>
Tue, 24 Mar 2026 13:34:15 +0000 (14:34 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 25 Jun 2026 17:21:26 +0000 (19:21 +0200)
gcc/rust/ChangeLog:

* resolve/rust-early-name-resolver-2.0.cc (Early::finalize_rebind_import): Insert imports
as possible glob containers.
* resolve/rust-name-resolution-context.cc (NameResolutionContext::map_usage): Allow
multiple mappings of the same usage.
* resolve/rust-name-resolution-context.hxx: Properly handle imports and modules in segments.

gcc/testsuite/ChangeLog:

* rust/compile/import_in_type_ns6.rs: New test.
* rust/compile/import_in_type_ns7.rs: New test.

gcc/rust/resolve/rust-early-name-resolver-2.0.cc
gcc/rust/resolve/rust-name-resolution-context.cc
gcc/rust/resolve/rust-name-resolution-context.hxx
gcc/testsuite/rust/compile/import_in_type_ns6.rs [new file with mode: 0644]
gcc/testsuite/rust/compile/import_in_type_ns7.rs [new file with mode: 0644]

index 4616323e6a8c133b26b711518f8bff584c813aba..2109ab263e79834096e9a1c0e97cf0896d58e4f1 100644 (file)
@@ -534,6 +534,17 @@ Early::finalize_rebind_import (const Early::ImportPair &mapping)
       toplevel
        .insert_or_error_out (declared_name,
                              path.get_locus (), import_id, definition.second /* TODO: This isn't clear - it would be better if it was called .ns or something */);
+
+      // Map the import to the glob container if it exists - this is important
+      // for 2-stepped glob imports which refer to glob containers, e.g.
+      //
+      // enum Foo { ... }
+      // pub use Foo;
+      // use self::Foo::*;
+      auto &mappings = Analysis::Mappings::get ();
+      if (auto container
+         = mappings.lookup_glob_container (definition.first.get_node_id ()))
+       mappings.insert_glob_container (import_id, container.value ());
     }
 }
 
index efe746fe9d340a1e4273634b4de65bbd3b53844b..70c74323eaeec6b109b70138dcf35c0455194bc5 100644 (file)
@@ -259,7 +259,7 @@ NameResolutionContext::map_usage (Usage usage, Definition definition)
   auto inserted = resolved_nodes.emplace (usage, definition).second;
 
   // is that valid?
-  rust_assert (inserted);
+  // rust_assert (inserted);
 }
 
 tl::optional<NodeId>
index 28c7dcccfb7cf044f70eae6fa072dc7038c6191b..1492125e5c585999608f430590a3a8d06d718aa0 100644 (file)
@@ -349,9 +349,12 @@ NameResolutionContext::resolve_segments (
                    .lookup_glob_container (rib_lookup->get_node_id ())
                    .has_value ())
                {
-                 child
-                   = stack.dfs_node (stack.root, rib_lookup->get_node_id ())
-                       .value ();
+                 auto leaf_module
+                   = find_leaf_definition (rib_lookup->get_node_id ())
+                       .value ()
+                       .id;
+
+                 child = stack.dfs_node (stack.root, leaf_module).value ();
                  break;
                }
              else
@@ -359,6 +362,7 @@ NameResolutionContext::resolve_segments (
                  insert_segment_resolution (Usage (seg.node_id),
                                             Definition (
                                               rib_lookup->get_node_id ()));
+
                  return tl::nullopt;
                }
            }
diff --git a/gcc/testsuite/rust/compile/import_in_type_ns6.rs b/gcc/testsuite/rust/compile/import_in_type_ns6.rs
new file mode 100644 (file)
index 0000000..6f5c312
--- /dev/null
@@ -0,0 +1,17 @@
+#![feature(no_core)]
+#![no_core]
+
+mod inner {
+    mod intrinsics {
+        pub fn unchecked_shl() {}
+        pub fn unchecked_snl() {}
+        pub fn unchecked_adult_swim() {}
+    }
+}
+
+use inner::intrinsics::{self, unchecked_snl};
+
+fn foo() /* { dg-warning "never used" } */
+{
+    use intrinsics::{unchecked_adult_swim, unchecked_shl};
+}
diff --git a/gcc/testsuite/rust/compile/import_in_type_ns7.rs b/gcc/testsuite/rust/compile/import_in_type_ns7.rs
new file mode 100644 (file)
index 0000000..3c5aabb
--- /dev/null
@@ -0,0 +1,22 @@
+#![feature(no_core)]
+#![no_core]
+
+mod inner {
+    pub mod error {
+        pub enum IntErrorKind {
+            A,
+            B,
+            C,
+        }
+    }
+}
+
+pub use inner::error::IntErrorKind;
+
+fn foo() /* { dg-warning "never used" } */
+{
+    use self::IntErrorKind::*;
+
+    let _ = A;
+    let _ = IntErrorKind::B;
+}