[gcc/devel/rust/master] Make globbing definition shadowable by default

Thomas Schwinge tschwinge@gcc.gnu.org
Tue May 7 16:26:37 GMT 2024


https://gcc.gnu.org/g:ac867c000d46f7b13130b0cb8acc1f2ac8a4362d

commit ac867c000d46f7b13130b0cb8acc1f2ac8a4362d
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Mon Jan 15 16:34:36 2024 +0100

    Make globbing definition shadowable by default
    
    Elements from glob use declaration shall be shadowable by default.
    
    gcc/rust/ChangeLog:
    
            * resolve/rust-forever-stack.h: Add a new function prototype to insert
            a shadowable definition.
            * resolve/rust-forever-stack.hxx: Add the new insert_shadowable
            function to insert shadowable definition into the forever stack.
            * resolve/rust-name-resolution-context.cc (NameResolutionContext::insert_shadowable):
            Likewise with the name resolution context.
            * resolve/rust-name-resolution-context.h: Add name resolution context
            insert_shadowable member function prototype.
            * resolve/rust-toplevel-name-resolver-2.0.cc (GlobbingVisitor::visit):
            Insert shadowable definition into the forever stack for glob use
            declaration.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Diff:
---
 gcc/rust/resolve/rust-forever-stack.h              | 15 ++++++++++
 gcc/rust/resolve/rust-forever-stack.hxx            | 10 +++++++
 gcc/rust/resolve/rust-name-resolution-context.cc   | 19 ++++++++++++
 gcc/rust/resolve/rust-name-resolution-context.h    |  3 ++
 .../resolve/rust-toplevel-name-resolver-2.0.cc     | 35 +++++++++++-----------
 5 files changed, 65 insertions(+), 17 deletions(-)

diff --git a/gcc/rust/resolve/rust-forever-stack.h b/gcc/rust/resolve/rust-forever-stack.h
index 01371fc7bcda..bba5875d4352 100644
--- a/gcc/rust/resolve/rust-forever-stack.h
+++ b/gcc/rust/resolve/rust-forever-stack.h
@@ -437,6 +437,21 @@ public:
    */
   tl::expected<NodeId, DuplicateNameError> insert (Identifier name, NodeId id);
 
+  /**
+   * Insert a new shadowable definition in the innermost `Rib` in this stack
+   *
+   * @param name The name of the definition
+   * @param id Its NodeId
+   *
+   * @return `DuplicateNameError` if that node was already present in the Rib,
+   * the node's `NodeId` otherwise.
+   *
+   * @aborts if there are no `Rib`s inserted in the current map, this function
+   *         aborts the program.
+   */
+  tl::expected<NodeId, DuplicateNameError> insert_shadowable (Identifier name,
+							      NodeId id);
+
   /**
    * Insert a new definition at the root of this stack
    *
diff --git a/gcc/rust/resolve/rust-forever-stack.hxx b/gcc/rust/resolve/rust-forever-stack.hxx
index a2fdce983629..008adff4676c 100644
--- a/gcc/rust/resolve/rust-forever-stack.hxx
+++ b/gcc/rust/resolve/rust-forever-stack.hxx
@@ -119,6 +119,16 @@ ForeverStack<N>::insert (Identifier name, NodeId node)
 		       Rib::Definition::NonShadowable (node));
 }
 
+template <Namespace N>
+tl::expected<NodeId, DuplicateNameError>
+ForeverStack<N>::insert_shadowable (Identifier name, NodeId node)
+{
+  auto &innermost_rib = peek ();
+
+  return insert_inner (innermost_rib, name.as_string (),
+		       Rib::Definition::Shadowable (node));
+}
+
 template <Namespace N>
 tl::expected<NodeId, DuplicateNameError>
 ForeverStack<N>::insert_at_root (Identifier name, NodeId node)
diff --git a/gcc/rust/resolve/rust-name-resolution-context.cc b/gcc/rust/resolve/rust-name-resolution-context.cc
index 0340d28f1278..e8529b70bcb7 100644
--- a/gcc/rust/resolve/rust-name-resolution-context.cc
+++ b/gcc/rust/resolve/rust-name-resolution-context.cc
@@ -45,6 +45,25 @@ NameResolutionContext::insert (Identifier name, NodeId id, Namespace ns)
     }
 }
 
+tl::expected<NodeId, DuplicateNameError>
+NameResolutionContext::insert_shadowable (Identifier name, NodeId id,
+					  Namespace ns)
+{
+  switch (ns)
+    {
+    case Namespace::Values:
+      return values.insert_shadowable (name, id);
+    case Namespace::Types:
+      return types.insert_shadowable (name, id);
+    case Namespace::Macros:
+      return macros.insert_shadowable (name, id);
+    case Namespace::Labels:
+    default:
+      // return labels.insert (name, id);
+      rust_unreachable ();
+    }
+}
+
 void
 NameResolutionContext::map_usage (Usage usage, Definition definition)
 {
diff --git a/gcc/rust/resolve/rust-name-resolution-context.h b/gcc/rust/resolve/rust-name-resolution-context.h
index 8702900d0f3a..74f110d54dee 100644
--- a/gcc/rust/resolve/rust-name-resolution-context.h
+++ b/gcc/rust/resolve/rust-name-resolution-context.h
@@ -171,6 +171,9 @@ public:
   tl::expected<NodeId, DuplicateNameError> insert (Identifier name, NodeId id,
 						   Namespace ns);
 
+  tl::expected<NodeId, DuplicateNameError>
+  insert_shadowable (Identifier name, NodeId id, Namespace ns);
+
   /**
    * Run a lambda in a "scoped" context, meaning that a new `Rib` will be pushed
    * before executing the lambda and then popped. This is useful for all kinds
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 501204174f22..7f4169a4d8e2 100644
--- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
@@ -36,71 +36,72 @@ void
 GlobbingVisitor::visit (AST::Module &module)
 {
   if (module.get_visibility ().is_public ())
-    ctx.insert (module.get_name (), module.get_node_id (), Namespace::Types);
+    ctx.insert_shadowable (module.get_name (), module.get_node_id (),
+			   Namespace::Types);
 }
 
 void
 GlobbingVisitor::visit (AST::MacroRulesDefinition &macro)
 {
   if (macro.get_visibility ().is_public ())
-    ctx.insert (macro.get_rule_name (), macro.get_node_id (),
-		Namespace::Macros);
+    ctx.insert_shadowable (macro.get_rule_name (), macro.get_node_id (),
+			   Namespace::Macros);
 }
 
 void
 GlobbingVisitor::visit (AST::Function &function)
 {
   if (function.get_visibility ().is_public ())
-    ctx.insert (function.get_function_name (), function.get_node_id (),
-		Namespace::Values);
+    ctx.insert_shadowable (function.get_function_name (),
+			   function.get_node_id (), Namespace::Values);
 }
 
 void
 GlobbingVisitor::visit (AST::StaticItem &static_item)
 {
   if (static_item.get_visibility ().is_public ())
-    ctx.insert (static_item.get_identifier (), static_item.get_node_id (),
-		Namespace::Values);
+    ctx.insert_shadowable (static_item.get_identifier (),
+			   static_item.get_node_id (), Namespace::Values);
 }
 
 void
 GlobbingVisitor::visit (AST::StructStruct &struct_item)
 {
   if (struct_item.get_visibility ().is_public ())
-    ctx.insert (struct_item.get_identifier (), struct_item.get_node_id (),
-		Namespace::Values);
+    ctx.insert_shadowable (struct_item.get_identifier (),
+			   struct_item.get_node_id (), Namespace::Values);
 }
 
 void
 GlobbingVisitor::visit (AST::TupleStruct &tuple_struct)
 {
   if (tuple_struct.get_visibility ().is_public ())
-    ctx.insert (tuple_struct.get_identifier (), tuple_struct.get_node_id (),
-		Namespace::Values);
+    ctx.insert_shadowable (tuple_struct.get_identifier (),
+			   tuple_struct.get_node_id (), Namespace::Values);
 }
 
 void
 GlobbingVisitor::visit (AST::Enum &enum_item)
 {
   if (enum_item.get_visibility ().is_public ())
-    ctx.insert (enum_item.get_identifier (), enum_item.get_node_id (),
-		Namespace::Values);
+    ctx.insert_shadowable (enum_item.get_identifier (),
+			   enum_item.get_node_id (), Namespace::Values);
 }
 
 void
 GlobbingVisitor::visit (AST::Union &union_item)
 {
   if (union_item.get_visibility ().is_public ())
-    ctx.insert (union_item.get_identifier (), union_item.get_node_id (),
-		Namespace::Values);
+    ctx.insert_shadowable (union_item.get_identifier (),
+			   union_item.get_node_id (), Namespace::Values);
 }
 
 void
 GlobbingVisitor::visit (AST::ConstantItem &const_item)
 {
   if (const_item.get_visibility ().is_public ())
-    ctx.insert (const_item.get_identifier (), const_item.get_node_id (),
-		Namespace::Values);
+    ctx.insert_shadowable (const_item.get_identifier (),
+			   const_item.get_node_id (), Namespace::Values);
 }
 
 void


More information about the Gcc-cvs mailing list