]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Change return type to optional in get_lang_item
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Fri, 3 May 2024 12:54:06 +0000 (14:54 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Mon, 17 Mar 2025 15:35:25 +0000 (16:35 +0100)
Wrap the function's return type with an optional.

gcc/rust/ChangeLog:

* typecheck/rust-autoderef.cc: Adapt calling code to new return type.
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit):
Likewise.
(TypeCheckExpr::resolve_operator_overload): Likewise.
* typecheck/rust-tyty-bounds.cc (TypeBoundsProbe::assemble_builtin_candidate):
Likewise.
* typecheck/rust-tyty.cc (ClosureType::setup_fn_once_output): Likewise.
* util/rust-hir-map.cc (Mappings::get_lang_item): Change return type.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/typecheck/rust-autoderef.cc
gcc/rust/typecheck/rust-hir-type-check-expr.cc
gcc/rust/typecheck/rust-tyty-bounds.cc
gcc/rust/typecheck/rust-tyty.cc
gcc/rust/util/rust-hir-map.cc
gcc/rust/util/rust-hir-map.h

index 233d5d459d12659754d4ec12b9ad13e7a589c79e..7e80b8efddf38a85cc81377bd295e04cca747c45 100644 (file)
@@ -129,12 +129,11 @@ resolve_operator_overload_fn (
 
   // look up lang item for arithmetic type
   std::string associated_item_name = LangItem::ToString (lang_item_type);
-  DefId respective_lang_item_id = UNKNOWN_DEFID;
-  bool lang_item_defined
-    = mappings.lookup_lang_item (lang_item_type, &respective_lang_item_id);
+  auto lang_item_defined = mappings.lookup_lang_item (lang_item_type);
 
   if (!lang_item_defined)
     return false;
+  DefId &respective_lang_item_id = lang_item_defined.value ();
 
   // we might be in a static or const context and unknown is fine
   TypeCheckContextItem current_context = TypeCheckContextItem::get_error ();
index 95e421d87200daedd70b135cac738b101a086f4f..7cbcdedbe4dd9a4bf5e891d9465d790a1a96ef07 100644 (file)
@@ -628,10 +628,7 @@ TypeCheckExpr::visit (HIR::RangeFromToExpr &expr)
 {
   auto lang_item_type = LangItem::Kind::RANGE;
 
-  DefId respective_lang_item_id = UNKNOWN_DEFID;
-  bool lang_item_defined
-    = mappings.lookup_lang_item (lang_item_type, &respective_lang_item_id);
-
+  auto lang_item_defined = mappings.lookup_lang_item (lang_item_type);
   // we need to have it maybe
   if (!lang_item_defined)
     {
@@ -640,6 +637,7 @@ TypeCheckExpr::visit (HIR::RangeFromToExpr &expr)
                              LangItem::ToString (lang_item_type).c_str ());
       return;
     }
+  DefId respective_lang_item_id = lang_item_defined.value ();
 
   // look it up and it _must_ be a struct definition
   HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value ();
@@ -682,10 +680,7 @@ TypeCheckExpr::visit (HIR::RangeFromExpr &expr)
 {
   auto lang_item_type = LangItem::Kind::RANGE_FROM;
 
-  DefId respective_lang_item_id = UNKNOWN_DEFID;
-  bool lang_item_defined
-    = mappings.lookup_lang_item (lang_item_type, &respective_lang_item_id);
-
+  auto lang_item_defined = mappings.lookup_lang_item (lang_item_type);
   // we need to have it maybe
   if (!lang_item_defined)
     {
@@ -694,6 +689,7 @@ TypeCheckExpr::visit (HIR::RangeFromExpr &expr)
                              LangItem::ToString (lang_item_type).c_str ());
       return;
     }
+  DefId &respective_lang_item_id = lang_item_defined.value ();
 
   // look it up and it _must_ be a struct definition
   HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value ();
@@ -729,10 +725,7 @@ TypeCheckExpr::visit (HIR::RangeToExpr &expr)
 {
   auto lang_item_type = LangItem::Kind::RANGE_TO;
 
-  DefId respective_lang_item_id = UNKNOWN_DEFID;
-  bool lang_item_defined
-    = mappings.lookup_lang_item (lang_item_type, &respective_lang_item_id);
-
+  auto lang_item_defined = mappings.lookup_lang_item (lang_item_type);
   // we need to have it maybe
   if (!lang_item_defined)
     {
@@ -742,6 +735,7 @@ TypeCheckExpr::visit (HIR::RangeToExpr &expr)
       return;
     }
 
+  DefId &respective_lang_item_id = lang_item_defined.value ();
   // look it up and it _must_ be a struct definition
   HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value ();
 
@@ -775,10 +769,7 @@ TypeCheckExpr::visit (HIR::RangeFullExpr &expr)
 {
   auto lang_item_type = LangItem::Kind::RANGE_FULL;
 
-  DefId respective_lang_item_id = UNKNOWN_DEFID;
-  bool lang_item_defined
-    = mappings.lookup_lang_item (lang_item_type, &respective_lang_item_id);
-
+  auto lang_item_defined = mappings.lookup_lang_item (lang_item_type);
   // we need to have it maybe
   if (!lang_item_defined)
     {
@@ -787,6 +778,7 @@ TypeCheckExpr::visit (HIR::RangeFullExpr &expr)
                              LangItem::ToString (lang_item_type).c_str ());
       return;
     }
+  DefId &respective_lang_item_id = lang_item_defined.value ();
 
   // look it up and it _must_ be a struct definition
   HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value ();
@@ -805,10 +797,7 @@ TypeCheckExpr::visit (HIR::RangeFromToInclExpr &expr)
 {
   auto lang_item_type = LangItem::Kind::RANGE_INCLUSIVE;
 
-  DefId respective_lang_item_id = UNKNOWN_DEFID;
-  bool lang_item_defined
-    = mappings.lookup_lang_item (lang_item_type, &respective_lang_item_id);
-
+  auto lang_item_defined = mappings.lookup_lang_item (lang_item_type);
   // we need to have it maybe
   if (!lang_item_defined)
     {
@@ -817,6 +806,7 @@ TypeCheckExpr::visit (HIR::RangeFromToInclExpr &expr)
                              LangItem::ToString (lang_item_type).c_str ());
       return;
     }
+  DefId respective_lang_item_id = lang_item_defined.value ();
 
   // look it up and it _must_ be a struct definition
   HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value ();
@@ -1580,9 +1570,8 @@ TypeCheckExpr::visit (HIR::ClosureExpr &expr)
   // closure
 
   LangItem::Kind lang_item_type = LangItem::Kind::FN_ONCE;
-  DefId respective_lang_item_id = UNKNOWN_DEFID;
-  bool lang_item_defined
-    = mappings.lookup_lang_item (lang_item_type, &respective_lang_item_id);
+
+  auto lang_item_defined = mappings.lookup_lang_item (lang_item_type);
   if (!lang_item_defined)
     {
       // FIXME
@@ -1591,7 +1580,7 @@ TypeCheckExpr::visit (HIR::ClosureExpr &expr)
       rust_fatal_error (expr.get_locus (), "unable to find lang item: %qs",
                        LangItem::ToString (lang_item_type).c_str ());
     }
-  rust_assert (lang_item_defined);
+  DefId &respective_lang_item_id = lang_item_defined.value ();
 
   // these lang items are always traits
   HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value ();
@@ -1635,13 +1624,12 @@ TypeCheckExpr::resolve_operator_overload (LangItem::Kind lang_item_type,
 {
   // look up lang item for arithmetic type
   std::string associated_item_name = LangItem::ToString (lang_item_type);
-  DefId respective_lang_item_id = UNKNOWN_DEFID;
-  bool lang_item_defined
-    = mappings.lookup_lang_item (lang_item_type, &respective_lang_item_id);
 
+  auto lang_item_defined = mappings.lookup_lang_item (lang_item_type);
   // probe for the lang-item
   if (!lang_item_defined)
     return false;
+  DefId &respective_lang_item_id = lang_item_defined.value ();
 
   // we might be in a static or const context and unknown is fine
   TypeCheckContextItem current_context = TypeCheckContextItem::get_error ();
index 43404385cdd8e0e1150c5e4a58b625975e814fe4..b73e592912978f29d98fc65b3333887810019ea4 100644 (file)
@@ -152,10 +152,10 @@ TypeBoundsProbe::assemble_sized_builtin ()
 void
 TypeBoundsProbe::assemble_builtin_candidate (LangItem::Kind lang_item)
 {
-  DefId id;
-  bool found_lang_item = mappings.lookup_lang_item (lang_item, &id);
-  if (!found_lang_item)
+  auto lang_item_defined = mappings.lookup_lang_item (lang_item);
+  if (!lang_item_defined)
     return;
+  DefId &id = lang_item_defined.value ();
 
   auto defid = mappings.lookup_defid (id);
   if (!defid)
index 565f2bc58aabaa2ea810b03b686aff44fb8b8b88..b877c0c740b18048090236a54fc65fe3613796f0 100644 (file)
@@ -2218,15 +2218,9 @@ ClosureType::setup_fn_once_output () const
   auto fn_once_lang_item = LangItem::Kind::FN_ONCE;
   auto fn_once_output_lang_item = LangItem::Kind::FN_ONCE_OUTPUT;
 
-  DefId trait_id = UNKNOWN_DEFID;
-  bool trait_lang_item_defined
-    = mappings.lookup_lang_item (fn_once_lang_item, &trait_id);
-  rust_assert (trait_lang_item_defined);
-
-  DefId trait_item_id = UNKNOWN_DEFID;
-  bool trait_item_lang_item_defined
-    = mappings.lookup_lang_item (fn_once_output_lang_item, &trait_item_id);
-  rust_assert (trait_item_lang_item_defined);
+  DefId &trait_id = mappings.lookup_lang_item (fn_once_lang_item).value ();
+  DefId &trait_item_id
+    = mappings.lookup_lang_item (fn_once_output_lang_item).value ();
 
   // resolve to the trait
   HIR::Item *item = mappings.lookup_defid (trait_id).value ();
index c77ffea354ef589d3b4cc898bbc94e1ee0487686..4a78ae155256414fc013db6e587310a861e310b0 100644 (file)
@@ -1253,13 +1253,11 @@ Mappings::lookup_builtin_marker ()
 DefId
 Mappings::get_lang_item (LangItem::Kind item_type, location_t locus)
 {
-  DefId item = UNKNOWN_DEFID;
-  bool ok = lookup_lang_item (item_type, &item);
-  if (!ok)
-    rust_fatal_error (locus, "failed to find lang item %s",
-                     LangItem::ToString (item_type).c_str ());
+  if (auto item = lookup_lang_item (item_type))
+    return *item;
 
-  return item;
+  rust_fatal_error (locus, "failed to find lang item %s",
+                   LangItem::ToString (item_type).c_str ());
 }
 
 tl::optional<HIR::TraitItem *>
index 753e06f796663fb3981f6dfd2bc7ace85a07dab0..3ca980ea3a92e00a8b1387836f7cef7edae5c41f 100644 (file)
@@ -267,14 +267,13 @@ public:
     lang_item_mappings[item_type] = id;
   }
 
-  bool lookup_lang_item (LangItem::Kind item_type, DefId *id)
+  tl::optional<DefId &> lookup_lang_item (LangItem::Kind item_type)
   {
     auto it = lang_item_mappings.find (item_type);
     if (it == lang_item_mappings.end ())
-      return false;
+      return tl::nullopt;
 
-    *id = it->second;
-    return true;
+    return it->second;
   }
 
   // This will fatal_error when this lang item does not exist