From: Yap Zhi Heng <[email protected]>

gcc/rust/ChangeLog:

        * resolve/rust-name-resolution-context.h (NameResolutionContext): Add 
new function
        definition for `should_search_prelude`.
        * resolve/rust-name-resolution-context.hxx 
(NameResolutionContext::should_search_prelude):
        Add implementation.
        (NameResolutionContext::resolve_segments): Update the check to trigger 
lang_prelude search
        during name resolution in modules.

Signed-off-by: Yap Zhi Heng <[email protected]>
---
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/035002f8a79e872ddcb80c1d372637b561c9c854

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/4580

 .../resolve/rust-name-resolution-context.h    |  6 +++++
 .../resolve/rust-name-resolution-context.hxx  | 23 ++++++++++++++++++-
 .../rust/compile/name_resolution28.rs         | 11 +++++++++
 3 files changed, 39 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/rust/compile/name_resolution28.rs

diff --git a/gcc/rust/resolve/rust-name-resolution-context.h 
b/gcc/rust/resolve/rust-name-resolution-context.h
index 6cbcfbb76..3bda8178a 100644
--- a/gcc/rust/resolve/rust-name-resolution-context.h
+++ b/gcc/rust/resolve/rust-name-resolution-context.h
@@ -872,6 +872,12 @@ public:
   tl::optional<NodeId> prelude;
 
 private:
+  template <Namespace N>
+  bool
+  should_search_prelude (const typename ForeverStack<N>::Node *current_node,
+                        const typename ForeverStack<N>::SegIterator &iterator,
+                        const std::vector<ResolutionPath::Segment> &segments);
+
   /**
    * Resolve a path to its definition
    *
diff --git a/gcc/rust/resolve/rust-name-resolution-context.hxx 
b/gcc/rust/resolve/rust-name-resolution-context.hxx
index 359dd9c65..7a8f24ac5 100644
--- a/gcc/rust/resolve/rust-name-resolution-context.hxx
+++ b/gcc/rust/resolve/rust-name-resolution-context.hxx
@@ -28,6 +28,26 @@
 namespace Rust {
 namespace Resolver2_0 {
 
+template <Namespace N>
+bool
+NameResolutionContext::should_search_prelude (
+  const typename ForeverStack<N>::Node *current_node,
+  const typename ForeverStack<N>::SegIterator &iterator,
+  const std::vector<ResolutionPath::Segment> &segments)
+{
+  // Check whether the current_node is a root node
+  if (current_node->is_root ())
+    return true;
+
+  // Check whether we're at the start of a module (we can't travel elsewhere
+  // from the start of a module)
+  if (is_start (iterator, segments)
+      && current_node->rib.kind == Rib::Kind::Module)
+    return true;
+
+  return false;
+}
+
 template <Namespace N>
 tl::optional<Rib::Definition>
 NameResolutionContext::resolve_path (
@@ -383,7 +403,8 @@ NameResolutionContext::resolve_segments (
                }
            }
 
-         if (current_node->is_root () && !searched_prelude)
+         if (!searched_prelude
+             && should_search_prelude<N> (current_node, iterator, segments))
            {
              searched_prelude = true;
              current_node = &stack.lang_prelude;
diff --git a/gcc/testsuite/rust/compile/name_resolution28.rs 
b/gcc/testsuite/rust/compile/name_resolution28.rs
new file mode 100644
index 000000000..d2400aed4
--- /dev/null
+++ b/gcc/testsuite/rust/compile/name_resolution28.rs
@@ -0,0 +1,11 @@
+#![feature(no_core, lang_items)]
+#![no_core]
+
+pub mod lateresolve {
+    #![lang = "f32"]
+    impl f32 {
+        pub const RADIX: u32 = 2;
+    }
+
+    pub const _: u32 = f32::RADIX;
+}

base-commit: 9af2a5f747071f9c608e6f89557be875040a91a0
-- 
2.54.0

Reply via email to