]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Change lookup_node_to_hir return type to optional
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Wed, 24 Apr 2024 19:27:26 +0000 (21:27 +0200)
committerP-E-P <32375388+P-E-P@users.noreply.github.com>
Fri, 17 May 2024 15:28:30 +0000 (15:28 +0000)
Previous API was using a boolean and a pointer, this was not practical
and could be replaced with an optional.

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::visit): Change call to use
the returned optional.
(CompileExpr::generate_closure_function): Likewise.
* backend/rust-compile-resolve-path.cc (ResolvePathRef::resolve):
Likewise.
* backend/rust-compile-type.cc (TyTyResolveCompile::visit): Likewise.
* backend/rust-mangle-v0.cc (v0_path): Likewise.
* checks/errors/privacy/rust-visibility-resolver.cc:
Likewise.
* checks/errors/rust-const-checker.cc (ConstChecker::visit): Likewise.
* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit):
Likewise.
* checks/lints/rust-lint-marklive.cc (MarkLive::visit): Likewise.
(MarkLive::visit_path_segment): Likewise.
* typecheck/rust-hir-trait-resolve.cc (TraitResolver::resolve_path_to_trait):
Likewise.
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_root_path):
Likewise.
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::resolve_root_path):
Likewise.
(ResolveWhereClauseItem::visit): Likewise.
* util/rust-hir-map.cc (Mappings::lookup_node_to_hir): Return an
optional instead of a boolean.
* util/rust-hir-map.h: Change function prototype to match the new
return type.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
13 files changed:
gcc/rust/backend/rust-compile-expr.cc
gcc/rust/backend/rust-compile-resolve-path.cc
gcc/rust/backend/rust-compile-type.cc
gcc/rust/backend/rust-mangle-v0.cc
gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc
gcc/rust/checks/errors/rust-const-checker.cc
gcc/rust/checks/errors/rust-unsafe-checker.cc
gcc/rust/checks/lints/rust-lint-marklive.cc
gcc/rust/typecheck/rust-hir-trait-resolve.cc
gcc/rust/typecheck/rust-hir-type-check-path.cc
gcc/rust/typecheck/rust-hir-type-check-type.cc
gcc/rust/util/rust-hir-map.cc
gcc/rust/util/rust-hir-map.h

index e53da939a7734b1511e1ea33539b7fc1fd14b919..3cf37ded3db13046d316f44cdd2f04bfd0a7763a 100644 (file)
@@ -30,6 +30,7 @@
 #include "realmpfr.h"
 #include "convert.h"
 #include "print-tree.h"
+#include "rust-system.h"
 
 namespace Rust {
 namespace Compile {
@@ -734,12 +735,14 @@ CompileExpr::visit (HIR::BreakExpr &expr)
          return;
        }
 
-      HirId ref = UNKNOWN_HIRID;
-      if (!ctx->get_mappings ().lookup_node_to_hir (resolved_node_id, &ref))
+      tl::optional<HirId> hid
+       = ctx->get_mappings ().lookup_node_to_hir (resolved_node_id);
+      if (!hid.has_value ())
        {
          rust_fatal_error (expr.get_locus (), "reverse lookup label failure");
          return;
        }
+      auto ref = hid.value ();
 
       tree label = NULL_TREE;
       if (!ctx->lookup_label_decl (ref, &label))
@@ -778,12 +781,14 @@ CompileExpr::visit (HIR::ContinueExpr &expr)
          return;
        }
 
-      HirId ref = UNKNOWN_HIRID;
-      if (!ctx->get_mappings ().lookup_node_to_hir (resolved_node_id, &ref))
+      tl::optional<HirId> hid
+       = ctx->get_mappings ().lookup_node_to_hir (resolved_node_id);
+      if (!hid.has_value ())
        {
          rust_fatal_error (expr.get_locus (), "reverse lookup label failure");
          return;
        }
+      auto ref = hid.value ();
 
       if (!ctx->lookup_label_decl (ref, &label))
        {
@@ -2176,20 +2181,21 @@ CompileExpr::visit (HIR::ClosureExpr &expr)
   for (const auto &capture : closure_tyty->get_captures ())
     {
       // lookup the HirId
-      HirId ref = UNKNOWN_HIRID;
-      bool ok = ctx->get_mappings ().lookup_node_to_hir (capture, &ref);
-      rust_assert (ok);
-
-      // lookup the var decl
-      Bvariable *var = nullptr;
-      bool found = ctx->lookup_var_decl (ref, &var);
-      rust_assert (found);
-
-      // FIXME
-      // this should bes based on the closure move-ability
-      tree var_expr = var->get_tree (expr.get_locus ());
-      tree val = address_expression (var_expr, expr.get_locus ());
-      vals.push_back (val);
+      if (auto hid = ctx->get_mappings ().lookup_node_to_hir (capture))
+       {
+         // lookup the var decl
+         Bvariable *var = nullptr;
+         bool found = ctx->lookup_var_decl (*hid, &var);
+         rust_assert (found);
+
+         // FIXME
+         // this should bes based on the closure move-ability
+         tree var_expr = var->get_tree (expr.get_locus ());
+         tree val = address_expression (var_expr, expr.get_locus ());
+         vals.push_back (val);
+       }
+      else
+       rust_unreachable ();
     }
 
   translated = Backend::constructor_expression (compiled_closure_tyty, false,
@@ -2246,21 +2252,21 @@ CompileExpr::generate_closure_function (HIR::ClosureExpr &expr,
   for (const auto &capture : closure_tyty.get_captures ())
     {
       // lookup the HirId
-      HirId ref = UNKNOWN_HIRID;
-      bool ok = ctx->get_mappings ().lookup_node_to_hir (capture, &ref);
-      rust_assert (ok);
-
-      // get the assessor
-      tree binding = Backend::struct_field_expression (self_param->get_tree (
-                                                        expr.get_locus ()),
-                                                      idx, expr.get_locus ());
-      tree indirection = indirect_expression (binding, expr.get_locus ());
+      if (auto hid = ctx->get_mappings ().lookup_node_to_hir (capture))
+       {
+         // get the assessor
+         tree binding = Backend::struct_field_expression (
+           self_param->get_tree (expr.get_locus ()), idx, expr.get_locus ());
+         tree indirection = indirect_expression (binding, expr.get_locus ());
 
-      // insert bindings
-      ctx->insert_closure_binding (ref, indirection);
+         // insert bindings
+         ctx->insert_closure_binding (*hid, indirection);
 
-      // continue
-      idx++;
+         // continue
+         idx++;
+       }
+      else
+       rust_unreachable ();
     }
 
   // args tuple
index 182b5fe9fdf0b6e5dc08fa2dac6f1484a1007745..c9a27782bf89cc07727283e4e78fb32f58ae838a 100644 (file)
@@ -125,12 +125,14 @@ ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment,
                                                      expr_locus);
     }
 
-  HirId ref;
-  if (!ctx->get_mappings ().lookup_node_to_hir (ref_node_id, &ref))
+  tl::optional<HirId> hid
+    = ctx->get_mappings ().lookup_node_to_hir (ref_node_id);
+  if (!hid.has_value ())
     {
       rust_error_at (expr_locus, "reverse call path lookup failure");
       return error_mark_node;
     }
+  auto ref = hid.value ();
 
   // might be a constant
   tree constant_expr;
index 64af5aed0e16bf94f7cd6098a81ec27b4b2b3b7f..c8fe1cd9d62959dba73e1cdf979acef4b0f4ae1f 100644 (file)
@@ -155,9 +155,9 @@ TyTyResolveCompile::visit (const TyTy::ClosureType &type)
   for (const auto &capture : type.get_captures ())
     {
       // lookup the HirId
-      HirId ref = UNKNOWN_HIRID;
-      bool ok = mappings.lookup_node_to_hir (capture, &ref);
-      rust_assert (ok);
+      tl::optional<HirId> hid = mappings.lookup_node_to_hir (capture);
+      rust_assert (hid.has_value ());
+      auto ref = hid.value ();
 
       // lookup the var decl type
       TyTy::BaseType *lookup = nullptr;
index f62c51e93c86ebfe34f2356b53fdb2676b873662..bb2b0d40b1705fc2f7a45ae3b672c6389f8e62fb 100644 (file)
@@ -374,14 +374,15 @@ v0_path (Rust::Compile::Context *ctx, const TyTy::BaseType *ty,
   V0Path v0path = {};
 
   cpath.iterate_segs ([&] (const Resolver::CanonicalPath &seg) {
-    HirId hir_id;
-    bool ok = mappings.lookup_node_to_hir (seg.get_node_id (), &hir_id);
-    if (!ok)
+    tl::optional<HirId> hid = mappings.lookup_node_to_hir (seg.get_node_id ());
+    if (!hid.has_value ())
       {
        // FIXME: generic arg in canonical path? (e.g. <i32> in crate::S<i32>)
        rust_unreachable ();
       }
 
+    auto hir_id = hid.value ();
+
     HirId parent_impl_id = UNKNOWN_HIRID;
     HIR::ImplItem *impl_item
       = mappings.lookup_hir_implitem (hir_id, &parent_impl_id);
index 381f68c9df68d1f370cfa9a3edf129c48ca6e558..5854b46562d3d631298b8cb816ef6211db5e087d 100644 (file)
@@ -71,8 +71,9 @@ VisibilityResolver::resolve_module_path (const HIR::SimplePath &restriction,
   // TODO: For the hint, can we point to the original item's definition if
   // present?
 
-  HirId ref;
-  rust_assert (mappings.lookup_node_to_hir (ref_node_id, &ref));
+  tl::optional<HirId> hid = mappings.lookup_node_to_hir (ref_node_id);
+  rust_assert (hid.has_value ());
+  auto ref = hid.value ();
 
   auto crate = mappings.get_ast_crate (mappings.get_current_crate ());
 
index 9c690137273795b48b63e4c5108cf823a3faf7a4..18864fdeb5a5539d6593c3c205f29e037623fafc 100644 (file)
@@ -21,6 +21,7 @@
 #include "rust-hir-expr.h"
 #include "rust-hir-stmt.h"
 #include "rust-hir-item.h"
+#include "rust-system.h"
 
 namespace Rust {
 namespace HIR {
@@ -352,18 +353,22 @@ ConstChecker::visit (CallExpr &expr)
 
   NodeId ast_node_id = expr.get_fnexpr ()->get_mappings ().get_nodeid ();
   NodeId ref_node_id;
-  HirId definition_id;
 
   // We don't care about types here
   if (!resolver.lookup_resolved_name (ast_node_id, &ref_node_id))
     return;
 
-  rust_assert (mappings.lookup_node_to_hir (ref_node_id, &definition_id));
-
-  check_function_call (definition_id, expr.get_locus ());
+  if (auto definition_id = mappings.lookup_node_to_hir (ref_node_id))
+    {
+      check_function_call (*definition_id, expr.get_locus ());
 
-  for (auto &arg : expr.get_arguments ())
-    arg->accept_vis (*this);
+      for (auto &arg : expr.get_arguments ())
+       arg->accept_vis (*this);
+    }
+  else
+    {
+      rust_unreachable ();
+    }
 }
 
 void
index aa7ffd806f81ac3fd02c7d45acaad9b3317c5879..ee3e52224fb38348a99e1884562c0efd5992aaa9 100644 (file)
@@ -22,6 +22,7 @@
 #include "rust-hir-stmt.h"
 #include "rust-hir-item.h"
 #include "rust-attribute-values.h"
+#include "rust-system.h"
 
 namespace Rust {
 namespace HIR {
@@ -220,14 +221,18 @@ UnsafeChecker::visit (PathInExpression &path)
 {
   NodeId ast_node_id = path.get_mappings ().get_nodeid ();
   NodeId ref_node_id;
-  HirId definition_id;
 
   if (!resolver.lookup_resolved_name (ast_node_id, &ref_node_id))
     return;
 
-  rust_assert (mappings.lookup_node_to_hir (ref_node_id, &definition_id));
-
-  check_use_of_static (definition_id, path.get_locus ());
+  if (auto definition_id = mappings.lookup_node_to_hir (ref_node_id))
+    {
+      check_use_of_static (*definition_id, path.get_locus ());
+    }
+  else
+    {
+      rust_unreachable ();
+    }
 }
 
 void
@@ -415,7 +420,6 @@ UnsafeChecker::visit (CallExpr &expr)
 
   NodeId ast_node_id = expr.get_fnexpr ()->get_mappings ().get_nodeid ();
   NodeId ref_node_id;
-  HirId definition_id;
 
   // There are no unsafe types, and functions are defined in the name resolver.
   // If we can't find the name, then we're dealing with a type and should return
@@ -423,19 +427,24 @@ UnsafeChecker::visit (CallExpr &expr)
   if (!resolver.lookup_resolved_name (ast_node_id, &ref_node_id))
     return;
 
-  rust_assert (mappings.lookup_node_to_hir (ref_node_id, &definition_id));
-
-  // At this point we have the function's HIR Id. There are three checks we
-  // must perform:
-  //     1. The function is an unsafe one
-  //     2. The function is an extern one
-  //     3. The function is marked with a target_feature attribute
-  check_function_call (definition_id, expr.get_locus ());
-  check_function_attr (definition_id, expr.get_locus ());
-
-  if (expr.has_params ())
-    for (auto &arg : expr.get_arguments ())
-      arg->accept_vis (*this);
+  if (auto definition_id = mappings.lookup_node_to_hir (ref_node_id))
+    {
+      // At this point we have the function's HIR Id. There are three checks we
+      // must perform:
+      //     1. The function is an unsafe one
+      //     2. The function is an extern one
+      //     3. The function is marked with a target_feature attribute
+      check_function_call (*definition_id, expr.get_locus ());
+      check_function_attr (*definition_id, expr.get_locus ());
+
+      if (expr.has_params ())
+       for (auto &arg : expr.get_arguments ())
+         arg->accept_vis (*this);
+    }
+  else
+    {
+      rust_unreachable ();
+    }
 }
 
 void
index 4170181be0207b48e69b600ab4f8a5785fa8b8f5..c5b536425faa0d68e6962671930c4a953ebfc589 100644 (file)
@@ -24,6 +24,7 @@
 #include "rust-hir-full.h"
 #include "rust-name-resolver.h"
 #include "rust-immutable-name-resolution-context.h"
+#include "rust-system.h"
 
 namespace Rust {
 namespace Analysis {
@@ -118,9 +119,9 @@ MarkLive::visit (HIR::PathInExpression &expr)
   find_ref_node_id (ast_node_id, ref_node_id);
 
   // node back to HIR
-  HirId ref;
-  bool ok = mappings.lookup_node_to_hir (ref_node_id, &ref);
-  rust_assert (ok);
+  tl::optional<HirId> hid = mappings.lookup_node_to_hir (ref_node_id);
+  rust_assert (hid.has_value ());
+  auto ref = hid.value ();
 
   // it must resolve to some kind of HIR::Item or HIR::InheritImplItem
   HIR::Item *resolved_item = mappings.lookup_hir_item (ref);
@@ -154,10 +155,10 @@ MarkLive::visit (HIR::MethodCallExpr &expr)
   find_ref_node_id (ast_node_id, ref_node_id);
 
   // node back to HIR
-  HirId ref;
-  bool ok = mappings.lookup_node_to_hir (ref_node_id, &ref);
-  rust_assert (ok);
-  mark_hir_id (ref);
+  if (auto hid = mappings.lookup_node_to_hir (ref_node_id))
+    mark_hir_id (*hid);
+  else
+    rust_unreachable ();
 }
 
 bool
@@ -179,11 +180,12 @@ MarkLive::visit_path_segment (HIR::PathExprSegment seg)
       if (!resolver->lookup_resolved_type (ast_node_id, &ref_node_id))
        return false;
     }
-  HirId ref;
-  bool ok = mappings.lookup_node_to_hir (ref_node_id, &ref);
-  rust_assert (ok);
-  mark_hir_id (ref);
-  return true;
+  if (auto hid = mappings.lookup_node_to_hir (ref_node_id))
+    {
+      mark_hir_id (*hid);
+      return true;
+    }
+  rust_unreachable ();
 }
 
 void
@@ -253,10 +255,10 @@ MarkLive::visit (HIR::TypeAlias &alias)
   NodeId ast_node_id;
   resolver->lookup_resolved_type (
     alias.get_type_aliased ()->get_mappings ().get_nodeid (), &ast_node_id);
-  HirId hir_id;
-  bool ok = mappings.lookup_node_to_hir (ast_node_id, &hir_id);
-  rust_assert (ok);
-  mark_hir_id (hir_id);
+  if (auto hid = mappings.lookup_node_to_hir (ast_node_id))
+    mark_hir_id (*hid);
+  else
+    rust_unreachable ();
 }
 
 void
index 6eb045c0b4f68483b28a76dfd2396bed7ef15ef3..c09cc9606e36ab4dfbdda72615f38fd54d7aed44 100644 (file)
@@ -117,19 +117,18 @@ TraitResolver::resolve_path_to_trait (const HIR::TypePath &path,
       return false;
     }
 
-  HirId hir_node = UNKNOWN_HIRID;
-  if (!mappings.lookup_node_to_hir (ref, &hir_node))
+  if (auto hid = mappings.lookup_node_to_hir (ref))
     {
-      rust_error_at (path.get_locus (), "Failed to resolve path to hir-id");
-      return false;
-    }
-
-  HIR::Item *resolved_item = mappings.lookup_hir_item (hir_node);
-  rust_assert (resolved_item != nullptr);
-  rust_assert (resolved_item->get_item_kind () == HIR::Item::ItemKind::Trait);
-  *resolved = static_cast<HIR::Trait *> (resolved_item);
+      HIR::Item *resolved_item = mappings.lookup_hir_item (hid.value ());
+      rust_assert (resolved_item != nullptr);
+      rust_assert (resolved_item->get_item_kind ()
+                  == HIR::Item::ItemKind::Trait);
+      *resolved = static_cast<HIR::Trait *> (resolved_item);
 
-  return true;
+      return true;
+    }
+  rust_error_at (path.get_locus (), "Failed to resolve path to hir-id");
+  return false;
 }
 
 TraitReference *
index 3c7dc29448772a89a6a7cb4cdb83422dde450053..9e284f247fc319d82229a3b614b7ed667312cf36 100644 (file)
@@ -229,8 +229,8 @@ TypeCheckExpr::resolve_root_path (HIR::PathInExpression &expr, size_t *offset,
        }
 
       // node back to HIR
-      HirId ref;
-      if (!mappings.lookup_node_to_hir (ref_node_id, &ref))
+      tl::optional<HirId> hid = mappings.lookup_node_to_hir (ref_node_id);
+      if (!hid.has_value ())
        {
          rust_error_at (seg.get_locus (), "456 reverse lookup failure");
          rust_debug_loc (seg.get_locus (),
@@ -241,6 +241,7 @@ TypeCheckExpr::resolve_root_path (HIR::PathInExpression &expr, size_t *offset,
 
          return new TyTy::ErrorType (expr.get_mappings ().get_hirid ());
        }
+      auto ref = hid.value ();
 
       auto seg_is_module = (nullptr != mappings.lookup_module (ref));
       auto seg_is_crate = mappings.is_local_hirid_crate (ref);
index 3392fff3d9eeb5321411bc7d42682ef4f9492365..c7bfee5209765d3c28514d451370fd949674ce0c 100644 (file)
@@ -372,8 +372,8 @@ TypeCheckType::resolve_root_path (HIR::TypePath &path, size_t *offset,
        }
 
       // node back to HIR
-      HirId ref = UNKNOWN_HIRID;
-      if (!mappings.lookup_node_to_hir (ref_node_id, &ref))
+      tl::optional<HirId> hid = mappings.lookup_node_to_hir (ref_node_id);
+      if (!hid.has_value ())
        {
          if (is_root)
            {
@@ -389,6 +389,7 @@ TypeCheckType::resolve_root_path (HIR::TypePath &path, size_t *offset,
 
          return root_tyty;
        }
+      auto ref = hid.value ();
 
       auto seg_is_module = (nullptr != mappings.lookup_module (ref));
       auto seg_is_crate = mappings.is_local_hirid_crate (ref);
@@ -997,27 +998,24 @@ ResolveWhereClauseItem::visit (HIR::TypeBoundWhereClauseItem &item)
     }
 
   // node back to HIR
-  HirId ref;
-  if (!mappings.lookup_node_to_hir (ref_node_id, &ref))
+  if (auto hid = mappings.lookup_node_to_hir (ref_node_id))
     {
-      // FIXME
-      rust_error_at (UNDEF_LOCATION, "where-clause reverse lookup failure");
-      return;
-    }
+      // the base reference for this name _must_ have a type set
+      TyTy::BaseType *lookup;
+      if (!context->lookup_type (*hid, &lookup))
+       {
+         rust_error_at (mappings.lookup_location (*hid),
+                        "Failed to resolve where-clause binding type: %s",
+                        binding_type_path->as_string ().c_str ());
+         return;
+       }
 
-  // the base reference for this name _must_ have a type set
-  TyTy::BaseType *lookup;
-  if (!context->lookup_type (ref, &lookup))
-    {
-      rust_error_at (mappings.lookup_location (ref),
-                    "Failed to resolve where-clause binding type: %s",
-                    binding_type_path->as_string ().c_str ());
+      // FIXME
+      // rust_assert (binding->is_equal (*lookup));
+      lookup->inherit_bounds (specified_bounds);
       return;
     }
-
-  // FIXME
-  // rust_assert (binding->is_equal (*lookup));
-  lookup->inherit_bounds (specified_bounds);
+  rust_error_at (UNDEF_LOCATION, "where-clause reverse lookup failure");
 }
 
 } // namespace Resolver
index 8b09d9d318e0e3b266d438607c70127b8bfc01c8..29fd4cf6bfd80125d97ec61b94be90f56fd82f44 100644 (file)
@@ -769,15 +769,14 @@ Mappings::insert_node_to_hir (NodeId id, HirId ref)
   hirIdToNodeMappings[ref] = id;
 }
 
-bool
-Mappings::lookup_node_to_hir (NodeId id, HirId *ref)
+tl::optional<HirId>
+Mappings::lookup_node_to_hir (NodeId id)
 {
   auto it = nodeIdToHirMappings.find (id);
   if (it == nodeIdToHirMappings.end ())
-    return false;
+    return tl::nullopt;
 
-  *ref = it->second;
-  return true;
+  return {it->second};
 }
 
 bool
index 2d278b7cc232b12d717b6198688e093ca80bbf42..85bcfdeeda1d24c08e397712b308560ddeaf616a 100644 (file)
@@ -170,7 +170,7 @@ public:
                                    std::function<bool (HIR::Item *)> cb);
 
   void insert_node_to_hir (NodeId id, HirId ref);
-  bool lookup_node_to_hir (NodeId id, HirId *ref);
+  tl::optional<HirId> lookup_node_to_hir (NodeId id);
   bool lookup_hir_to_node (HirId id, NodeId *ref);
 
   void insert_location (HirId id, location_t locus);