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

commit ffeb37619e17e54efdbe4032546a270ee4f04ee5
Author: Kushal Pal <kushalpal...@gmail.com>
Date:   Wed Aug 7 10:16:24 2024 +0000

    Strong type ScopeId
    
    gcc/rust/ChangeLog:
    
            * checks/errors/borrowck/rust-bir-builder-expr-stmt.cc
            (ExprStmtBuilder::setup_loop): Use value of ScopeId.
            (ExprStmtBuilder::visit): Use continue scope id instead of
            continue basic block id.
            * checks/errors/borrowck/rust-bir-builder-internal.h: Use value
            of ScopeId.
            * checks/errors/borrowck/rust-bir-dump.cc (Dump::go): Use
            ROOT_VALUE instead of hardcoded 0.
            (Dump::visit_scope): Use value of ScopeId.
            * checks/errors/borrowck/rust-bir-place.h (struct ScopeId):
            ScopeId is now a struct.
            (std::numeric_limits::max): Set invalid ScopeId.
    
    Signed-off-by: Kushal Pal <kushalpal...@gmail.com>

Diff:
---
 .../errors/borrowck/rust-bir-builder-expr-stmt.cc  |  5 ++-
 .../errors/borrowck/rust-bir-builder-internal.h    |  4 +-
 gcc/rust/checks/errors/borrowck/rust-bir-dump.cc   | 10 ++---
 gcc/rust/checks/errors/borrowck/rust-bir-place.h   | 43 +++++++++++++++-------
 4 files changed, 39 insertions(+), 23 deletions(-)

diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc 
b/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc
index e9f1d971d245..c11cff0ae4df 100644
--- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc
+++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc
@@ -45,7 +45,8 @@ ExprStmtBuilder::setup_loop (HIR::BaseLoopExpr &expr)
 
   BasicBlockId break_bb = new_bb ();
   // We are still outside the loop block;
-  ScopeId continue_scope = ctx.place_db.get_current_scope_id () + 1;
+  ScopeId continue_scope
+    = ctx.place_db.get_current_scope_id ().next_scope_id ();
   ctx.loop_and_label_stack.emplace_back (true, label, label_var, break_bb,
                                         continue_bb, continue_scope);
 
@@ -414,7 +415,7 @@ ExprStmtBuilder::visit (HIR::ContinueExpr &cont)
   LoopAndLabelCtx info = cont.has_label () ? get_label_ctx (cont.get_label ())
                                           : get_unnamed_loop_ctx ();
   start_new_consecutive_bb ();
-  unwind_until (info.continue_bb);
+  unwind_until (info.continue_scope);
   push_goto (info.continue_bb);
   // No code allowed after continue. Handled in BlockExpr.
 }
diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h 
b/gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h
index d03bdeb10f6a..445373089b3c 100644
--- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h
+++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h
@@ -156,7 +156,7 @@ protected:
 
     auto place_id = ctx.place_db.add_variable (nodeid, ty);
 
-    if (ctx.place_db.get_current_scope_id () != 0)
+    if (ctx.place_db.get_current_scope_id () != INVALID_SCOPE)
       push_storage_live (place_id);
 
     if (user_type_annotation)
@@ -170,7 +170,7 @@ protected:
   void pop_scope ()
   {
     auto &scope = ctx.place_db.get_current_scope ();
-    if (ctx.place_db.get_current_scope_id () != 0)
+    if (ctx.place_db.get_current_scope_id () != INVALID_SCOPE)
       {
        std::for_each (scope.locals.rbegin (), scope.locals.rend (),
                       [&] (PlaceId place) { push_storage_dead (place); });
diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc 
b/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc
index c4a71ff61591..8312682e7eda 100644
--- a/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc
+++ b/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc
@@ -133,7 +133,7 @@ Dump::go (bool enable_simplify_cfg)
   stream << " {\n";
 
   // Print locals declaration.
-  visit_scope (0);
+  visit_scope (ROOT_SCOPE);
 
   // Print BBs.
   for (statement_bb = 0; statement_bb < func.basic_blocks.size ();
@@ -366,8 +366,8 @@ Dump::visit_scope (ScopeId id, size_t depth)
   if (scope.locals.empty () && scope.children.empty ())
     return;
 
-  if (id > 1)
-    indent (depth) << "scope " << id - 1 << " {\n";
+  if (id.value > 1)
+    indent (depth) << "scope " << id.value - 1 << " {\n";
 
   for (auto &local : scope.locals)
     {
@@ -385,9 +385,9 @@ Dump::visit_scope (ScopeId id, size_t depth)
       stream << "]\n";
     }
   for (auto &child : scope.children)
-    visit_scope (child, (id >= 1) ? depth + 1 : depth);
+    visit_scope (child, (id.value >= 1) ? depth + 1 : depth);
 
-  if (id > 1)
+  if (id.value > 1)
     indent (depth) << "}\n";
 }
 
diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-place.h 
b/gcc/rust/checks/errors/borrowck/rust-bir-place.h
index f68a341ad9b0..728728c08aa2 100644
--- a/gcc/rust/checks/errors/borrowck/rust-bir-place.h
+++ b/gcc/rust/checks/errors/borrowck/rust-bir-place.h
@@ -165,13 +165,25 @@ public:
   }
 };
 
-using ScopeId = uint32_t;
+struct ScopeId
+{
+  uint32_t value;
+  ScopeId next_scope_id () const { return {value + 1}; }
+  // some overloads for comparision
+  bool operator== (const ScopeId &rhs) const { return value == rhs.value; }
+  bool operator!= (const ScopeId &rhs) const { return !(operator== (rhs)); }
+  bool operator< (const ScopeId &rhs) const { return value < rhs.value; }
+  bool operator> (const ScopeId &rhs) const { return value > rhs.value; }
+  bool operator<= (const ScopeId &rhs) const { return !(operator> (rhs)); }
+  bool operator>= (const ScopeId &rhs) const { return !(operator< (rhs)); }
+};
 
-static constexpr ScopeId INVALID_SCOPE = std::numeric_limits<ScopeId>::max ();
+static constexpr ScopeId INVALID_SCOPE
+  = {std::numeric_limits<uint32_t>::max ()};
 /** Arguments and return value are in the root scope. */
-static constexpr ScopeId ROOT_SCOPE = 0;
+static constexpr ScopeId ROOT_SCOPE = {0};
 /** Top-level local variables are in the top-level scope. */
-static constexpr ScopeId TOP_LEVEL_SCOPE = 1;
+static constexpr ScopeId TOP_LEVEL_SCOPE = {1};
 
 struct Scope
 {
@@ -195,7 +207,7 @@ private:
   std::vector<Place> places;
   std::unordered_map<TyTy::BaseType *, PlaceId> constants_lookup;
   std::vector<Scope> scopes;
-  ScopeId current_scope = 0;
+  ScopeId current_scope = ROOT_SCOPE;
 
   std::vector<Loan> loans;
 
@@ -228,9 +240,12 @@ public:
 
   const std::vector<Scope> &get_scopes () const { return scopes; }
 
-  const Scope &get_current_scope () const { return scopes[current_scope]; }
+  const Scope &get_current_scope () const
+  {
+    return scopes[current_scope.value];
+  }
 
-  const Scope &get_scope (ScopeId id) const { return scopes[id]; }
+  const Scope &get_scope (ScopeId id) const { return scopes[id.value]; }
 
   FreeRegion get_next_free_region ()
   {
@@ -244,17 +259,17 @@ public:
 
   ScopeId push_new_scope ()
   {
-    ScopeId new_scope = scopes.size ();
+    ScopeId new_scope = {scopes.size ()};
     scopes.emplace_back ();
-    scopes[new_scope].parent = current_scope;
-    scopes[current_scope].children.push_back (new_scope);
+    scopes[new_scope.value].parent = current_scope;
+    scopes[current_scope.value].children.push_back (new_scope);
     current_scope = new_scope;
     return new_scope;
   }
 
   ScopeId pop_scope ()
   {
-    current_scope = scopes[current_scope].parent;
+    current_scope = scopes[current_scope.value].parent;
     return current_scope;
   }
 
@@ -270,7 +285,7 @@ public:
 
     if (new_place_ref.kind == Place::VARIABLE
        || new_place_ref.kind == Place::TEMPORARY)
-      scopes[current_scope].locals.push_back (new_place);
+      scopes[current_scope.value].locals.push_back (new_place);
 
     auto variances = Resolver::TypeCheckContext::get ()
                       ->get_variance_analysis_ctx ()
@@ -460,9 +475,9 @@ private:
   WARN_UNUSED_RESULT bool is_in_scope (PlaceId place) const
   {
     for (ScopeId scope = current_scope; scope != INVALID_SCOPE;
-        scope = scopes[scope].parent)
+        scope = scopes[scope.value].parent)
       {
-       auto &scope_ref = scopes[scope];
+       auto &scope_ref = scopes[scope.value];
        if (std::find (scope_ref.locals.begin (), scope_ref.locals.end (),
                       place)
            != scope_ref.locals.end ())

Reply via email to