diff --git a/gcc/rust/resolve/rust-name-resolution-context.h b/gcc/rust/resolve/rust-name-resolution-context.h index 6cbcfbb7655..3bda8178a83 100644 --- a/gcc/rust/resolve/rust-name-resolution-context.h +++ b/gcc/rust/resolve/rust-name-resolution-context.h @@ -872,6 +872,12 @@ class NameResolutionContext tl::optional prelude; private: + template + bool + should_search_prelude (const typename ForeverStack::Node *current_node, + const typename ForeverStack::SegIterator &iterator, + const std::vector &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 359dd9c65e5..7a8f24ac515 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 +bool +NameResolutionContext::should_search_prelude ( + const typename ForeverStack::Node *current_node, + const typename ForeverStack::SegIterator &iterator, + const std::vector &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 tl::optional NameResolutionContext::resolve_path ( @@ -383,7 +403,8 @@ NameResolutionContext::resolve_segments ( } } - if (current_node->is_root () && !searched_prelude) + if (!searched_prelude + && should_search_prelude (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 00000000000..d2400aed4bd --- /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; +}