]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Convert lookup return type to optional
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Tue, 19 Aug 2025 15:06:38 +0000 (17:06 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 30 Oct 2025 19:58:42 +0000 (20:58 +0100)
Remove usage of error state (but not error state itelf) and use an
optional to convey the missing value meaning instead.

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::generate_closure_fntype):
Unwrap the optional.
* backend/rust-compile.cc: Change return type container. Adapt code to
new return type.
* typecheck/rust-hir-dot-operator.cc: Likewise.
* typecheck/rust-hir-path-probe.cc: Likewise.
* typecheck/rust-hir-type-check-implitem.cc (TypeCheckImplItemWithTrait::visit):
Likewise.
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit):
Likewise.
* typecheck/rust-tyty-bounds.cc (TypeBoundPredicate::contains_item):
Likewise.
(TypeBoundPredicate::lookup_associated_item): Likewise.
(TypeBoundPredicateItem::get_parent): Likewise.
(TypeBoundPredicate::lookup_associated_type): Likewise.
* typecheck/rust-tyty.cc (BaseType::satisfies_bound): Likewise.
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::visit): Change
return type.
* typecheck/rust-tyty.h: Likewise.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/backend/rust-compile-expr.cc
gcc/rust/backend/rust-compile.cc
gcc/rust/typecheck/rust-hir-dot-operator.cc
gcc/rust/typecheck/rust-hir-path-probe.cc
gcc/rust/typecheck/rust-hir-type-check-implitem.cc
gcc/rust/typecheck/rust-hir-type-check-path.cc
gcc/rust/typecheck/rust-hir-type-check-type.cc
gcc/rust/typecheck/rust-tyty-bounds.cc
gcc/rust/typecheck/rust-tyty.cc
gcc/rust/typecheck/rust-tyty.h

index 64339236f89ac2c4f00f606211c114c2a38d53d8..d059eade2cafcb2c5f4bd19b44ad6e29c183c0cf 100644 (file)
@@ -2617,15 +2617,15 @@ CompileExpr::generate_closure_fntype (HIR::ClosureExpr &expr,
   TyTy::TypeBoundPredicateItem item = TyTy::TypeBoundPredicateItem::error ();
   if (predicate.get_name ().compare ("FnOnce") == 0)
     {
-      item = predicate.lookup_associated_item ("call_once");
+      item = predicate.lookup_associated_item ("call_once").value ();
     }
   else if (predicate.get_name ().compare ("FnMut") == 0)
     {
-      item = predicate.lookup_associated_item ("call_mut");
+      item = predicate.lookup_associated_item ("call_mut").value ();
     }
   else if (predicate.get_name ().compare ("Fn") == 0)
     {
-      item = predicate.lookup_associated_item ("call");
+      item = predicate.lookup_associated_item ("call").value ();
     }
   else
     {
index dbd85158aef88c5fdd238209c937eb569848a6c9..a3e047a2dab113c193fc2be081a88916ca5da271 100644 (file)
@@ -241,12 +241,13 @@ HIRCompileBase::compute_address_for_trait_item (
     &receiver_bounds,
   const TyTy::BaseType *receiver, const TyTy::BaseType *root, location_t locus)
 {
-  TyTy::TypeBoundPredicateItem predicate_item
+  tl::optional<TyTy::TypeBoundPredicateItem> predicate_item
     = predicate->lookup_associated_item (ref->get_identifier ());
-  rust_assert (!predicate_item.is_error ());
+  rust_assert (predicate_item.has_value ());
 
   // This is the expected end type
-  TyTy::BaseType *trait_item_type = predicate_item.get_tyty_for_receiver (root);
+  TyTy::BaseType *trait_item_type
+    = predicate_item->get_tyty_for_receiver (root);
   rust_assert (trait_item_type->get_kind () == TyTy::TypeKind::FNDEF);
   TyTy::FnType *trait_item_fntype
     = static_cast<TyTy::FnType *> (trait_item_type);
index 29919aaaec8072380ebd2bdddeadc8d152258de2..296cc440b7f5b6e3ab87acb55082297313ff4115 100644 (file)
@@ -471,17 +471,17 @@ MethodResolver::get_predicate_items (
   std::vector<predicate_candidate> predicate_items;
   for (auto &bound : specified_bounds)
     {
-      TyTy::TypeBoundPredicateItem lookup
+      tl::optional<TyTy::TypeBoundPredicateItem> lookup
        = bound.lookup_associated_item (segment_name.as_string ());
-      if (lookup.is_error ())
+      if (!lookup.has_value ())
        continue;
 
-      TyTy::BaseType *ty = lookup.get_tyty_for_receiver (&receiver);
+      TyTy::BaseType *ty = lookup->get_tyty_for_receiver (&receiver);
       if (ty->get_kind () == TyTy::TypeKind::FNDEF)
        {
          TyTy::FnType *fnty = static_cast<TyTy::FnType *> (ty);
          if (fnty->is_method ())
-           predicate_items.emplace_back (lookup, fnty);
+           predicate_items.emplace_back (lookup.value (), fnty);
        }
     }
 
index 6ed6e25858f9add5796edea7425dc1efab9a2713..bd42e906b4d8c575ee4ed74d2620162cf569a256 100644 (file)
@@ -367,15 +367,15 @@ PathProbeType::process_predicate_for_candidates (
 {
   const TraitReference *trait_ref = predicate.get ();
 
-  TyTy::TypeBoundPredicateItem item
+  tl::optional<TyTy::TypeBoundPredicateItem> item
     = predicate.lookup_associated_item (search.as_string ());
-  if (item.is_error ())
+  if (!item.has_value ())
     return;
 
-  if (ignore_mandatory_trait_items && item.needs_implementation ())
+  if (ignore_mandatory_trait_items && item->needs_implementation ())
     return;
 
-  const TraitItemReference *trait_item_ref = item.get_raw_item ();
+  const TraitItemReference *trait_item_ref = item->get_raw_item ();
   PathProbeCandidate::CandidateType candidate_type;
   switch (trait_item_ref->get_trait_item_type ())
     {
@@ -395,9 +395,9 @@ PathProbeType::process_predicate_for_candidates (
       break;
     }
 
-  TyTy::BaseType *trait_item_tyty = item.get_raw_item ()->get_tyty ();
+  TyTy::BaseType *trait_item_tyty = item->get_raw_item ()->get_tyty ();
   if (receiver->get_kind () != TyTy::DYNAMIC)
-    trait_item_tyty = item.get_tyty_for_receiver (receiver);
+    trait_item_tyty = item->get_tyty_for_receiver (receiver);
 
   PathProbeCandidate::TraitItemCandidate trait_item_candidate{trait_ref,
                                                              trait_item_ref,
index 077a228e52b157f786dd16c36f328731f8080a37..e7f66327d02ddbf6aaafab9ae00b4321e14932da 100644 (file)
@@ -469,8 +469,8 @@ TypeCheckImplItemWithTrait::visit (HIR::ConstantItem &constant)
     }
 
   // get the item from the predicate
-  resolved_trait_item = trait_reference.lookup_associated_item (raw_trait_item);
-  rust_assert (!resolved_trait_item.is_error ());
+  resolved_trait_item
+    = trait_reference.lookup_associated_item (raw_trait_item).value ();
 
   // merge the attributes
   const HIR::TraitItem *hir_trait_item
@@ -519,8 +519,8 @@ TypeCheckImplItemWithTrait::visit (HIR::TypeAlias &type)
     }
 
   // get the item from the predicate
-  resolved_trait_item = trait_reference.lookup_associated_item (raw_trait_item);
-  rust_assert (!resolved_trait_item.is_error ());
+  resolved_trait_item
+    = trait_reference.lookup_associated_item (raw_trait_item).value ();
 
   // merge the attributes
   const HIR::TraitItem *hir_trait_item
@@ -578,8 +578,8 @@ TypeCheckImplItemWithTrait::visit (HIR::Function &function)
     }
 
   // get the item from the predicate
-  resolved_trait_item = trait_reference.lookup_associated_item (raw_trait_item);
-  rust_assert (!resolved_trait_item.is_error ());
+  resolved_trait_item
+    = trait_reference.lookup_associated_item (raw_trait_item).value ();
 
   // merge the attributes
   const HIR::TraitItem *hir_trait_item
index cc5c412f9868a02e6fa258cd04a2e9f6effd621f..3068f0c6e3573c452d6a1d7f4b7e901fb9bd7da0 100644 (file)
@@ -77,9 +77,9 @@ TypeCheckExpr::visit (HIR::QualifiedPathInExpression &expr)
   // lookup the associated item from the specified bound
   HIR::PathExprSegment &item_seg = expr.get_segments ().at (0);
   HIR::PathIdentSegment item_seg_identifier = item_seg.get_segment ();
-  TyTy::TypeBoundPredicateItem item
+  tl::optional<TyTy::TypeBoundPredicateItem> item
     = specified_bound.lookup_associated_item (item_seg_identifier.as_string ());
-  if (item.is_error ())
+  if (!item.has_value ())
     {
       rust_error_at (item_seg.get_locus (), "unknown associated item");
       return;
@@ -116,9 +116,9 @@ TypeCheckExpr::visit (HIR::QualifiedPathInExpression &expr)
       // and we dont need to worry if the trait item is actually implemented or
       // not because this will have already been validated as part of the trait
       // impl block
-      infered = item.get_tyty_for_receiver (root);
+      infered = item->get_tyty_for_receiver (root);
       root_resolved_node_id
-       = item.get_raw_item ()->get_mappings ().get_nodeid ();
+       = item->get_raw_item ()->get_mappings ().get_nodeid ();
     }
   else
     {
index 4a6c703d764bfa2f485706d06fcfd9c47643dd9a..39772b42a0db48fbffda8ee4f102602e919cc2bd 100644 (file)
@@ -211,9 +211,9 @@ TypeCheckType::visit (HIR::QualifiedPathInType &path)
   // lookup the associated item from the specified bound
   HIR::TypePathSegment &item_seg = path.get_associated_segment ();
   HIR::PathIdentSegment item_seg_identifier = item_seg.get_ident_segment ();
-  TyTy::TypeBoundPredicateItem item
+  tl::optional<TyTy::TypeBoundPredicateItem> item
     = specified_bound.lookup_associated_item (item_seg_identifier.as_string ());
-  if (item.is_error ())
+  if (!item.has_value ())
     {
       std::string item_seg_ident_name, rich_msg;
       item_seg_ident_name = qual_path_type.get_trait ().as_string ();
@@ -265,7 +265,7 @@ TypeCheckType::visit (HIR::QualifiedPathInType &path)
       // and we dont need to worry if the trait item is actually implemented or
       // not because this will have already been validated as part of the trait
       // impl block
-      translated = item.get_tyty_for_receiver (root);
+      translated = item->get_tyty_for_receiver (root);
     }
   else
     {
index 91de35976b667ebe2a776c29cbcd9ec2a20e564e..bb2e6d4d708f389a59d852fb0faf244966093d85 100644 (file)
@@ -575,10 +575,13 @@ TypeBoundPredicate::apply_argument_mappings (
       std::string identifier = it.first;
       TyTy::BaseType *type = it.second;
 
-      TypeBoundPredicateItem item = lookup_associated_item (identifier);
-      rust_assert (!item.is_error ());
+      tl::optional<TypeBoundPredicateItem> item
+       = lookup_associated_item (identifier);
 
-      const auto item_ref = item.get_raw_item ();
+      if (!item.has_value ())
+       continue;
+
+      const auto item_ref = item->get_raw_item ();
       item_ref->associated_type_set (type);
     }
 
@@ -599,7 +602,7 @@ TypeBoundPredicate::contains_item (const std::string &search) const
   return trait_ref->lookup_trait_item (search, &trait_item_ref);
 }
 
-TypeBoundPredicateItem
+tl::optional<TypeBoundPredicateItem>
 TypeBoundPredicate::lookup_associated_item (const std::string &search) const
 {
   auto trait_ref = get ();
@@ -611,11 +614,11 @@ TypeBoundPredicate::lookup_associated_item (const std::string &search) const
   for (auto &super_trait : super_traits)
     {
       auto lookup = super_trait.lookup_associated_item (search);
-      if (!lookup.is_error ())
+      if (lookup.has_value ())
        return lookup;
     }
 
-  return TypeBoundPredicateItem::error ();
+  return tl::nullopt;
 }
 
 TypeBoundPredicateItem::TypeBoundPredicateItem (
@@ -656,7 +659,7 @@ TypeBoundPredicateItem::get_parent () const
   return &parent;
 }
 
-TypeBoundPredicateItem
+tl::optional<TypeBoundPredicateItem>
 TypeBoundPredicate::lookup_associated_item (
   const Resolver::TraitItemReference *ref) const
 {
@@ -732,10 +735,11 @@ TypeBoundPredicate::handle_substitions (
       std::string identifier = it.first;
       TyTy::BaseType *type = it.second;
 
-      TypeBoundPredicateItem item = lookup_associated_item (identifier);
-      if (!item.is_error ())
+      tl::optional<TypeBoundPredicateItem> item
+       = lookup_associated_item (identifier);
+      if (item.has_value ())
        {
-         const auto item_ref = item.get_raw_item ();
+         const auto item_ref = item->get_raw_item ();
          item_ref->associated_type_set (type);
        }
     }
@@ -799,18 +803,18 @@ TypeBoundPredicate::get_trait_hierachy (
 TypeBoundPredicateItem
 TypeBoundPredicate::lookup_associated_type (const std::string &search)
 {
-  TypeBoundPredicateItem item = lookup_associated_item (search);
+  tl::optional<TypeBoundPredicateItem> item = lookup_associated_item (search);
 
   // only need to check that it is infact an associated type because other
   // wise if it was not found it will just be an error node anyway
-  if (!item.is_error ())
+  if (item.has_value ())
     {
-      const auto raw = item.get_raw_item ();
+      const auto raw = item->get_raw_item ();
       if (raw->get_trait_item_type ()
          != Resolver::TraitItemReference::TraitItemType::TYPE)
        return TypeBoundPredicateItem::error ();
     }
-  return item;
+  return item.value ();
 }
 
 std::vector<TypeBoundPredicateItem>
index af5f9777fd413305bc5671088e00abf34c8d4954..95e9bc2c17f76b514b4da04b3b56984f1acda81a 100644 (file)
@@ -351,12 +351,12 @@ BaseType::satisfies_bound (const TypeBoundPredicate &predicate,
            return false;
 
          std::string item_name = item->get_impl_item_name ();
-         TypeBoundPredicateItem lookup
+         tl::optional<TypeBoundPredicateItem> lookup
            = predicate.lookup_associated_item (item_name);
-         if (lookup.is_error ())
+         if (!lookup.has_value ())
            return false;
 
-         const auto *item_ref = lookup.get_raw_item ();
+         const auto *item_ref = lookup->get_raw_item ();
          TyTy::BaseType *bound_ty = item_ref->get_tyty ();
 
          // compare the types
index 49415ea0b31ae0e71a35d98a038d18fd6ad41020..b90bc83e8bf3b682b5bd0b6330556dcb820db5a0 100644 (file)
@@ -617,10 +617,10 @@ public:
 
   bool contains_item (const std::string &search) const;
 
-  TypeBoundPredicateItem
+  tl::optional<TypeBoundPredicateItem>
   lookup_associated_item (const std::string &search) const;
 
-  TypeBoundPredicateItem
+  tl::optional<TypeBoundPredicateItem>
   lookup_associated_item (const Resolver::TraitItemReference *ref) const;
 
   // WARNING THIS WILL ALWAYS RETURN NULLPTR