Skip to content

Commit 9c4e2a7

Browse files
committed
Resolve module final self segment in use decls
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>
1 parent 29cace3 commit 9c4e2a7

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

gcc/rust/resolve/rust-early-name-resolver-2.0.cc

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,19 @@ Early::finalize_rebind_import (const Early::ImportPair &mapping)
417417
declared_name = rebind.get_identifier ().as_string ();
418418
locus = rebind.get_identifier ().get_locus ();
419419
break;
420-
case AST::UseTreeRebind::NewBindType::NONE:
421-
declared_name = path.get_final_segment ().as_string ();
422-
locus = path.get_final_segment ().get_locus ();
423-
break;
420+
case AST::UseTreeRebind::NewBindType::NONE: {
421+
const auto &segments = path.get_segments ();
422+
// We don't want to insert `self` with `use module::self`
423+
if (path.get_final_segment ().is_lower_self_seg ())
424+
{
425+
rust_assert (segments.size () > 1);
426+
declared_name = segments[segments.size () - 2].as_string ();
427+
}
428+
else
429+
declared_name = path.get_final_segment ().as_string ();
430+
locus = path.get_final_segment ().get_locus ();
431+
break;
432+
}
424433
case AST::UseTreeRebind::NewBindType::WILDCARD:
425434
rust_unreachable ();
426435
break;

gcc/rust/resolve/rust-forever-stack.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,10 @@ template <Namespace N> class ForeverStack
795795
SegIterator<S> iterator,
796796
std::function<void (const S &, NodeId)> insert_segment_resolution);
797797

798+
tl::optional<Rib::Definition> resolve_final_segment (Node &final_node,
799+
std::string &seg_name,
800+
bool is_lower_self);
801+
798802
/* Helper functions for forward resolution (to_canonical_path, to_rib...) */
799803
struct DfsResult
800804
{

gcc/rust/resolve/rust-forever-stack.hxx

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,26 @@ ForeverStack<N>::resolve_segments (
594594
return *current_node;
595595
}
596596

597+
template <>
598+
inline tl::optional<Rib::Definition>
599+
ForeverStack<Namespace::Types>::resolve_final_segment (Node &final_node,
600+
std::string &seg_name,
601+
bool is_lower_self)
602+
{
603+
if (is_lower_self)
604+
return Rib::Definition::NonShadowable (final_node.id);
605+
else
606+
return final_node.rib.get (seg_name);
607+
}
608+
609+
template <Namespace N>
610+
tl::optional<Rib::Definition>
611+
ForeverStack<N>::resolve_final_segment (Node &final_node, std::string &seg_name,
612+
bool is_lower_self)
613+
{
614+
return final_node.rib.get (seg_name);
615+
}
616+
597617
template <Namespace N>
598618
template <typename S>
599619
tl::optional<Rib::Definition>
@@ -643,12 +663,13 @@ ForeverStack<N>::resolve_path (
643663
if (final_node.rib.kind == Rib::Kind::TraitOrImpl)
644664
return tl::nullopt;
645665

646-
std::string seg_name
647-
= unwrap_type_segment (segments.back ()).as_string ();
666+
auto &seg = unwrap_type_segment (segments.back ());
667+
std::string seg_name = seg.as_string ();
648668

649669
// assuming this can't be a lang item segment
650-
tl::optional<Rib::Definition> res = final_node.rib.get (seg_name);
651-
670+
tl::optional<Rib::Definition> res
671+
= resolve_final_segment (final_node, seg_name,
672+
seg.is_lower_self_seg ());
652673
// Ok we didn't find it in the rib, Lets try the prelude...
653674
if (!res)
654675
res = get_prelude (seg_name);

0 commit comments

Comments
 (0)