[gcc r14-7883] gccrs: rib2.0: Add shadowing

Arthur Cohen cohenarthur@gcc.gnu.org
Tue Jan 16 18:04:14 GMT 2024


https://gcc.gnu.org/g:5a0e099e892d575af167d6a0a7e9ae5f26f439d8

commit r14-7883-g5a0e099e892d575af167d6a0a7e9ae5f26f439d8
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Fri Jul 21 18:22:43 2023 +0200

    gccrs: rib2.0: Add shadowing
    
    gcc/rust/ChangeLog:
    
            * resolve/rust-rib.h: Add shadowing parameter. Make kind field public.
            * resolve/rust-rib.cc (Rib::insert): Likewise.

Diff:
---
 gcc/rust/resolve/rust-rib.cc | 8 +++++---
 gcc/rust/resolve/rust-rib.h  | 7 ++++---
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/gcc/rust/resolve/rust-rib.cc b/gcc/rust/resolve/rust-rib.cc
index 2cc9f3e1862..21fbe2ca530 100644
--- a/gcc/rust/resolve/rust-rib.cc
+++ b/gcc/rust/resolve/rust-rib.cc
@@ -36,13 +36,15 @@ Rib::Rib (Kind kind, std::unordered_map<std::string, NodeId> values)
 {}
 
 tl::expected<NodeId, DuplicateNameError>
-Rib::insert (std::string name, NodeId id)
+Rib::insert (std::string name, NodeId id, bool can_shadow)
 {
   auto res = values.insert ({name, id});
   auto inserted_id = res.first->second;
+  auto existed = !res.second;
 
-  // if we couldn't insert, the element already exists - exit with an error
-  if (!res.second)
+  // if we couldn't insert, the element already exists - exit with an error,
+  // unless shadowing is allowed
+  if (existed && !can_shadow)
     return tl::make_unexpected (DuplicateNameError (name, inserted_id));
 
   // return the NodeId
diff --git a/gcc/rust/resolve/rust-rib.h b/gcc/rust/resolve/rust-rib.h
index ea69cc7bd6e..37bd90f1f75 100644
--- a/gcc/rust/resolve/rust-rib.h
+++ b/gcc/rust/resolve/rust-rib.h
@@ -93,7 +93,7 @@ public:
     ForwardTypeParamBan,
     /* Const generic, as in the following example: fn foo<T, const X: T>() {} */
     ConstParamType,
-  };
+  } kind;
 
   Rib (Kind kind);
   Rib (Kind kind, std::string identifier, NodeId id);
@@ -107,12 +107,14 @@ public:
    *
    * @param name The name associated with the AST node
    * @param id Its NodeId
+   * @param can_shadow If the newly inserted value can shadow an existing one
    *
    * @return `DuplicateNameError` if the node is already present in the rib. The
    *         `DuplicateNameError` class contains the NodeId of the existing
    * node. Returns the new NodeId on success.
    */
-  tl::expected<NodeId, DuplicateNameError> insert (std::string name, NodeId id);
+  tl::expected<NodeId, DuplicateNameError> insert (std::string name, NodeId id,
+						   bool can_shadow = false);
 
   /**
    * Access an inserted NodeId.
@@ -125,7 +127,6 @@ public:
   const std::unordered_map<std::string, NodeId> &get_values () const;
 
 private:
-  Kind kind;
   std::unordered_map<std::string, NodeId> values;
 };


More information about the Gcc-cvs mailing list