From: Owen Avery <[email protected]>

gcc/rust/ChangeLog:

        * resolve/rust-finalize-imports-2.0.cc
        (GlobbingVisitor::visit_enum_container): Conditionally insert
        into value namespace as well.
        * resolve/rust-forever-stack.hxx (ForeverStack::insert_variant):
        Add template specialization for value namespace.
        * resolve/rust-name-resolution-context.cc
        (NameResolutionContext::insert_variant): Allow insertion into
        value namespace.
        * resolve/rust-name-resolution-context.h
        (NameResolutionContext::insert_variant): Likewise.
        * resolve/rust-toplevel-name-resolver-2.0.cc
        (TopLevel::insert_enum_variant_or_error_out): Likewise.
        (TopLevel::visit): Use tweaked insert_enum_variant_or_error_out
        properly for all enum item kinds.
        * resolve/rust-toplevel-name-resolver-2.0.h
        (TopLevel::insert_enum_variant_or_error_out): Tweak function
        signature.

Signed-off-by: Owen Avery <[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/4af91dba01b7c2894bcef9d7a82378b952415dd1

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4480

 gcc/rust/resolve/rust-finalize-imports-2.0.cc   |  9 +++++++--
 gcc/rust/resolve/rust-forever-stack.hxx         |  8 ++++++++
 .../resolve/rust-name-resolution-context.cc     |  8 ++++++--
 gcc/rust/resolve/rust-name-resolution-context.h |  4 ++--
 .../resolve/rust-toplevel-name-resolver-2.0.cc  | 17 +++++++++--------
 .../resolve/rust-toplevel-name-resolver-2.0.h   |  5 +++--
 6 files changed, 35 insertions(+), 16 deletions(-)

diff --git a/gcc/rust/resolve/rust-finalize-imports-2.0.cc 
b/gcc/rust/resolve/rust-finalize-imports-2.0.cc
index 5feb44070..a5bbc5b6c 100644
--- a/gcc/rust/resolve/rust-finalize-imports-2.0.cc
+++ b/gcc/rust/resolve/rust-finalize-imports-2.0.cc
@@ -64,8 +64,13 @@ void
 GlobbingVisitor::visit_enum_container (AST::Enum &item)
 {
   for (auto &variant : item.get_variants ())
-    ctx.insert_globbed (variant->get_identifier (), variant->get_node_id (),
-                       Namespace::Types);
+    {
+      ctx.insert_globbed (variant->get_identifier (), variant->get_node_id (),
+                         Namespace::Types);
+      if (variant->get_enum_item_kind () != AST::EnumItem::Kind::Struct)
+       ctx.insert_globbed (variant->get_identifier (), variant->get_node_id (),
+                           Namespace::Values);
+    }
 }
 
 void
diff --git a/gcc/rust/resolve/rust-forever-stack.hxx 
b/gcc/rust/resolve/rust-forever-stack.hxx
index c7c5c9439..e8f4e8449 100644
--- a/gcc/rust/resolve/rust-forever-stack.hxx
+++ b/gcc/rust/resolve/rust-forever-stack.hxx
@@ -199,6 +199,14 @@ ForeverStack<Namespace::Types>::insert_variant (Identifier 
name, NodeId node)
                       Rib::Definition::NonShadowable (node, true));
 }
 
+template <>
+inline tl::expected<NodeId, DuplicateNameError>
+ForeverStack<Namespace::Values>::insert_variant (Identifier name, NodeId node)
+{
+  return insert_inner (peek (), name.as_string (),
+                      Rib::Definition::NonShadowable (node, true));
+}
+
 template <Namespace N>
 inline void
 ForeverStack<N>::insert_lang_prelude (Identifier name, NodeId id)
diff --git a/gcc/rust/resolve/rust-name-resolution-context.cc 
b/gcc/rust/resolve/rust-name-resolution-context.cc
index 45b78cb89..bbd8f07dd 100644
--- a/gcc/rust/resolve/rust-name-resolution-context.cc
+++ b/gcc/rust/resolve/rust-name-resolution-context.cc
@@ -207,9 +207,13 @@ NameResolutionContext::insert (Identifier name, NodeId id, 
Namespace ns)
 }
 
 tl::expected<NodeId, DuplicateNameError>
-NameResolutionContext::insert_variant (Identifier name, NodeId id)
+NameResolutionContext::insert_variant (Identifier name, NodeId id,
+                                      bool is_also_value)
 {
-  return types.insert_variant (name, id);
+  auto res = types.insert_variant (name, id);
+  if (res.has_value () && is_also_value)
+    res = values.insert_variant (name, id);
+  return res;
 }
 
 tl::expected<NodeId, DuplicateNameError>
diff --git a/gcc/rust/resolve/rust-name-resolution-context.h 
b/gcc/rust/resolve/rust-name-resolution-context.h
index 91eb0dcde..eac38eb62 100644
--- a/gcc/rust/resolve/rust-name-resolution-context.h
+++ b/gcc/rust/resolve/rust-name-resolution-context.h
@@ -473,8 +473,8 @@ public:
   tl::expected<NodeId, DuplicateNameError> insert (Identifier name, NodeId id,
                                                   Namespace ns);
 
-  tl::expected<NodeId, DuplicateNameError> insert_variant (Identifier name,
-                                                          NodeId id);
+  tl::expected<NodeId, DuplicateNameError>
+  insert_variant (Identifier name, NodeId id, bool is_also_value);
 
   tl::expected<NodeId, DuplicateNameError>
   insert_shadowable (Identifier name, NodeId id, Namespace ns);
diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc 
b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
index 67a8c52d1..2002df545 100644
--- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
@@ -33,10 +33,10 @@ TopLevel::TopLevel (NameResolutionContext &resolver)
 template <typename T>
 void
 TopLevel::insert_enum_variant_or_error_out (const Identifier &identifier,
-                                           const T &node)
+                                           const T &node, bool is_also_value)
 {
   insert_enum_variant_or_error_out (identifier, node.get_locus (),
-                                   node.get_node_id ());
+                                   node.get_node_id (), is_also_value);
 }
 
 void
@@ -58,12 +58,13 @@ TopLevel::check_multiple_insertion_error (
 void
 TopLevel::insert_enum_variant_or_error_out (const Identifier &identifier,
                                            const location_t &locus,
-                                           const NodeId node_id)
+                                           const NodeId node_id,
+                                           bool is_also_value)
 {
   // keep track of each node's location to provide useful errors
   node_locations.emplace (node_id, locus);
 
-  auto result = ctx.insert_variant (identifier, node_id);
+  auto result = ctx.insert_variant (identifier, node_id, is_also_value);
   check_multiple_insertion_error (result, identifier, locus, node_id);
 }
 
@@ -309,7 +310,7 @@ TopLevel::visit (AST::TupleStruct &tuple_struct)
 void
 TopLevel::visit (AST::EnumItem &variant)
 {
-  insert_enum_variant_or_error_out (variant.get_identifier (), variant);
+  insert_enum_variant_or_error_out (variant.get_identifier (), variant, true);
 
   DefaultResolver::visit (variant);
 }
@@ -317,7 +318,7 @@ TopLevel::visit (AST::EnumItem &variant)
 void
 TopLevel::visit (AST::EnumItemTuple &variant)
 {
-  insert_enum_variant_or_error_out (variant.get_identifier (), variant);
+  insert_enum_variant_or_error_out (variant.get_identifier (), variant, true);
 
   DefaultResolver::visit (variant);
 }
@@ -325,7 +326,7 @@ TopLevel::visit (AST::EnumItemTuple &variant)
 void
 TopLevel::visit (AST::EnumItemStruct &variant)
 {
-  insert_enum_variant_or_error_out (variant.get_identifier (), variant);
+  insert_enum_variant_or_error_out (variant.get_identifier (), variant, false);
 
   DefaultResolver::visit (variant);
 }
@@ -333,7 +334,7 @@ TopLevel::visit (AST::EnumItemStruct &variant)
 void
 TopLevel::visit (AST::EnumItemDiscriminant &variant)
 {
-  insert_or_error_out (variant.get_identifier (), variant, Namespace::Types);
+  insert_enum_variant_or_error_out (variant.get_identifier (), variant, true);
 
   DefaultResolver::visit (variant);
 }
diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h 
b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
index b5b0c8b19..bcee6c2c1 100644
--- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
+++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
@@ -142,11 +142,12 @@ public:
 
   template <typename T>
   void insert_enum_variant_or_error_out (const Identifier &identifier,
-                                        const T &node);
+                                        const T &node, bool is_also_value);
 
   void insert_enum_variant_or_error_out (const Identifier &identifier,
                                         const location_t &locus,
-                                        const NodeId node_id);
+                                        const NodeId node_id,
+                                        bool is_also_value);
 
 private:
   // If a new export has been defined whilst visiting the visitor is considered
-- 
2.53.0

Reply via email to