From: Arthur Cohen <[email protected]>
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.
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.
Commit on github:
https://github.com/Rust-GCC/gccrs/commit/b17643dbfba9f1fec55010a7745d61698d2cb8af
The commit has NOT been mentioned in any issue.
The commit has been mentioned in the following pull-request(s):
- https://github.com/Rust-GCC/gccrs/pull/4551
.../resolve/rust-early-name-resolver-2.0.cc | 11 ++++++++++
.../resolve/rust-name-resolution-context.cc | 2 +-
.../resolve/rust-name-resolution-context.hxx | 10 ++++++---
.../rust/compile/import_in_type_ns6.rs | 17 ++++++++++++++
.../rust/compile/import_in_type_ns7.rs | 22 +++++++++++++++++++
5 files changed, 58 insertions(+), 4 deletions(-)
create mode 100644 gcc/testsuite/rust/compile/import_in_type_ns6.rs
create mode 100644 gcc/testsuite/rust/compile/import_in_type_ns7.rs
diff --git a/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
b/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
index 4616323e6..2109ab263 100644
--- a/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
@@ -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 ());
}
}
diff --git a/gcc/rust/resolve/rust-name-resolution-context.cc
b/gcc/rust/resolve/rust-name-resolution-context.cc
index efe746fe9..70c74323e 100644
--- a/gcc/rust/resolve/rust-name-resolution-context.cc
+++ b/gcc/rust/resolve/rust-name-resolution-context.cc
@@ -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>
diff --git a/gcc/rust/resolve/rust-name-resolution-context.hxx
b/gcc/rust/resolve/rust-name-resolution-context.hxx
index 28c7dcccf..1492125e5 100644
--- a/gcc/rust/resolve/rust-name-resolution-context.hxx
+++ b/gcc/rust/resolve/rust-name-resolution-context.hxx
@@ -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
index 000000000..6f5c312c8
--- /dev/null
+++ b/gcc/testsuite/rust/compile/import_in_type_ns6.rs
@@ -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
index 000000000..3c5aabb0e
--- /dev/null
+++ b/gcc/testsuite/rust/compile/import_in_type_ns7.rs
@@ -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;
+}
--
2.54.0