[llvm] dce8252 - [RadixTree] Use std::optional for Node::Value (#165299)

via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 27 12:30:32 PDT 2025


Author: Vitaly Buka
Date: 2025-10-27T12:30:28-07:00
New Revision: dce825248bb0286d97f199ae4d628923e926083c

URL: https://github.com/llvm/llvm-project/commit/dce825248bb0286d97f199ae4d628923e926083c
DIFF: https://github.com/llvm/llvm-project/commit/dce825248bb0286d97f199ae4d628923e926083c.diff

LOG: [RadixTree] Use std::optional for Node::Value (#165299)

Don't rely on comparison to singular iterator, it's UB.

Fixes bot crashes after
https://github.com/llvm/llvm-project/pull/164524.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/RadixTree.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/RadixTree.h b/llvm/include/llvm/ADT/RadixTree.h
index 9e2ab9753d50c..87e2a3ebecc06 100644
--- a/llvm/include/llvm/ADT/RadixTree.h
+++ b/llvm/include/llvm/ADT/RadixTree.h
@@ -20,6 +20,7 @@
 #include <cstddef>
 #include <iterator>
 #include <list>
+#include <optional>
 #include <utility>
 #include <vector>
 
@@ -92,7 +93,7 @@ template <typename KeyType, typename T> class RadixTree {
     /// If this node does not have a value (i.e., it's an internal node that
     /// only serves as a path to other values), this iterator will be equal
     /// to default constructed `ContainerType::iterator()`.
-    typename ContainerType::iterator Value;
+    std::optional<typename ContainerType::iterator> Value;
 
     /// The first character of the Key. Used for fast child lookup.
     KeyValueType KeyFront;
@@ -215,7 +216,7 @@ template <typename KeyType, typename T> class RadixTree {
                                     KeyConstIteratorType{}};
 
     void findNextValid() {
-      while (Curr && Curr->Value == typename ContainerType::iterator())
+      while (Curr && !Curr->Value.has_value())
         advance();
     }
 
@@ -249,7 +250,7 @@ template <typename KeyType, typename T> class RadixTree {
   public:
     IteratorImpl() = default;
 
-    MappedType &operator*() const { return *Curr->Value; }
+    MappedType &operator*() const { return **Curr->Value; }
 
     IteratorImpl &operator++() {
       advance();
@@ -315,12 +316,12 @@ template <typename KeyType, typename T> class RadixTree {
     const value_type &NewValue = KeyValuePairs.emplace_front(
         std::move(Key), T(std::forward<Ts>(Args)...));
     Node &Node = findOrCreate(NewValue.first);
-    bool HasValue = Node.Value != typename ContainerType::iterator();
+    bool HasValue = Node.Value.has_value();
     if (!HasValue)
       Node.Value = KeyValuePairs.begin();
     else
       KeyValuePairs.pop_front();
-    return {Node.Value, !HasValue};
+    return {*Node.Value, !HasValue};
   }
 
   ///


        


More information about the llvm-commits mailing list