[libcxx-commits] [libcxx] [NFC] Prepare for PR #134330 by migrating to std::__static_fancy_pointer_cast (PR #180546)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Feb 9 07:29:42 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Vinay Deshmukh (vinay-deshmukh)

<details>
<summary>Changes</summary>

To reduce the noise in #<!-- -->134330 for `libcxx/include/__tree`, we migrate from certain `static_cast` to `std::__static_fancy_pointer_cast` in this separate patch.

This change is needed to properly work with fancy pointers like `min_pointer` during constant evaluation for `std::map`

---

Patch is 43.32 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/180546.diff


1 Files Affected:

- (modified) libcxx/include/__tree (+172-148) 


``````````diff
diff --git a/libcxx/include/__tree b/libcxx/include/__tree
index 847ea2c0851d4..ac299d1c92700 100644
--- a/libcxx/include/__tree
+++ b/libcxx/include/__tree
@@ -206,10 +206,10 @@ template <class _EndNodePtr, class _NodePtr>
 inline _LIBCPP_HIDE_FROM_ABI _EndNodePtr __tree_next_iter(_NodePtr __x) _NOEXCEPT {
   _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null");
   if (__x->__right_ != nullptr)
-    return static_cast<_EndNodePtr>(std::__tree_min(__x->__right_));
+    return std::__static_fancy_pointer_cast<_EndNodePtr>(std::__tree_min(__x->__right_));
   while (!std::__tree_is_left_child(__x))
     __x = __x->__parent_unsafe();
-  return static_cast<_EndNodePtr>(__x->__parent_);
+  return std::__static_fancy_pointer_cast<_EndNodePtr>(__x->__parent_);
 }
 
 // Returns:  pointer to the previous in-order node before __x.
@@ -219,7 +219,7 @@ inline _LIBCPP_HIDE_FROM_ABI _NodePtr __tree_prev_iter(_EndNodePtr __x) _NOEXCEP
   _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null");
   if (__x->__left_ != nullptr)
     return std::__tree_max(__x->__left_);
-  _NodePtr __xx = static_cast<_NodePtr>(__x);
+  _NodePtr __xx = std::__static_fancy_pointer_cast<_NodePtr>(__x);
   while (std::__tree_is_left_child(__xx))
     __xx = __xx->__parent_unsafe();
   return __xx->__parent_unsafe();
@@ -573,9 +573,11 @@ public:
   __end_node_pointer __parent_;
   bool __is_black_;
 
-  _LIBCPP_HIDE_FROM_ABI pointer __parent_unsafe() const { return static_cast<pointer>(__parent_); }
+  _LIBCPP_HIDE_FROM_ABI pointer __parent_unsafe() const { return std::__static_fancy_pointer_cast<pointer>(__parent_); }
 
-  _LIBCPP_HIDE_FROM_ABI void __set_parent(pointer __p) { __parent_ = static_cast<__end_node_pointer>(__p); }
+  _LIBCPP_HIDE_FROM_ABI void __set_parent(pointer __p) {
+    __parent_ = std::__static_fancy_pointer_cast<__end_node_pointer>(__p);
+  }
 
   _LIBCPP_HIDE_FROM_ABI __tree_node_base()             = default;
   __tree_node_base(__tree_node_base const&)            = delete;
@@ -730,7 +732,7 @@ public:
   }
 
   _LIBCPP_HIDE_FROM_ABI __tree_iterator& operator++() {
-    __ptr_ = std::__tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_));
+    __ptr_ = std::__tree_next_iter<__end_node_pointer>(std::__static_fancy_pointer_cast<__node_base_pointer>(__ptr_));
     return *this;
   }
   _LIBCPP_HIDE_FROM_ABI __tree_iterator operator++(int) {
@@ -740,7 +742,7 @@ public:
   }
 
   _LIBCPP_HIDE_FROM_ABI __tree_iterator& operator--() {
-    __ptr_ = static_cast<__end_node_pointer>(std::__tree_prev_iter<__node_base_pointer>(__ptr_));
+    __ptr_ = std::__static_fancy_pointer_cast<__end_node_pointer>(std::__tree_prev_iter<__node_base_pointer>(__ptr_));
     return *this;
   }
   _LIBCPP_HIDE_FROM_ABI __tree_iterator operator--(int) {
@@ -757,9 +759,12 @@ public:
   }
 
 private:
-  _LIBCPP_HIDE_FROM_ABI explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
+  _LIBCPP_HIDE_FROM_ABI explicit __tree_iterator(__node_pointer __p) _NOEXCEPT
+      : __ptr_(std::__static_fancy_pointer_cast<__end_node_pointer>(__p)) {}
   _LIBCPP_HIDE_FROM_ABI explicit __tree_iterator(__end_node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
-  _LIBCPP_HIDE_FROM_ABI __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
+  _LIBCPP_HIDE_FROM_ABI __node_pointer __get_np() const {
+    return std::__static_fancy_pointer_cast<__node_pointer>(__ptr_);
+  }
   template <class, class, class>
   friend class __tree;
   template <class, class, class>
@@ -814,7 +819,7 @@ public:
   }
 
   _LIBCPP_HIDE_FROM_ABI __tree_const_iterator& operator++() {
-    __ptr_ = std::__tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_));
+    __ptr_ = std::__tree_next_iter<__end_node_pointer>(std::__static_fancy_pointer_cast<__node_base_pointer>(__ptr_));
     return *this;
   }
 
@@ -825,7 +830,7 @@ public:
   }
 
   _LIBCPP_HIDE_FROM_ABI __tree_const_iterator& operator--() {
-    __ptr_ = static_cast<__end_node_pointer>(std::__tree_prev_iter<__node_base_pointer>(__ptr_));
+    __ptr_ = std::__static_fancy_pointer_cast<__end_node_pointer>(std::__tree_prev_iter<__node_base_pointer>(__ptr_));
     return *this;
   }
 
@@ -843,9 +848,13 @@ public:
   }
 
 private:
-  _LIBCPP_HIDE_FROM_ABI explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
-  _LIBCPP_HIDE_FROM_ABI explicit __tree_const_iterator(__end_node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
-  _LIBCPP_HIDE_FROM_ABI __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
+  _LIBCPP_HIDE_FROM_ABI explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT
+      : __ptr_(std::__static_fancy_pointer_cast<__end_node_pointer>(__p)) {}
+  _LIBCPP_HIDE_FROM_ABI explicit __tree_const_iterator(__end_node_pointer __p) _NOEXCEPT
+      : __ptr_(std::__static_fancy_pointer_cast<__end_node_pointer>(__p)) {}
+  _LIBCPP_HIDE_FROM_ABI __node_pointer __get_np() const {
+    return std::__static_fancy_pointer_cast<__node_pointer>(__ptr_);
+  }
 
   template <class, class, class>
   friend class __tree;
@@ -947,7 +956,7 @@ public:
 
 public:
   _LIBCPP_HIDE_FROM_ABI __node_pointer __root() const _NOEXCEPT {
-    return static_cast<__node_pointer>(__end_node()->__left_);
+    return std::__static_fancy_pointer_cast<__node_pointer>(__end_node()->__left_);
   }
 
   _LIBCPP_HIDE_FROM_ABI __node_base_pointer* __root_ptr() const _NOEXCEPT {
@@ -980,9 +989,9 @@ public:
     if (__other.size() == 0)
       return;
 
-    *__root_ptr()       = static_cast<__node_base_pointer>(__copy_construct_tree(__other.__root()));
+    *__root_ptr() = std::__static_fancy_pointer_cast<__node_base_pointer>(__copy_construct_tree(__other.__root()));
     __root()->__parent_ = __end_node();
-    __begin_node_       = static_cast<__end_node_pointer>(std::__tree_min(__end_node()->__left_));
+    __begin_node_       = std::__static_fancy_pointer_cast<__end_node_pointer>(std::__tree_min(__end_node()->__left_));
     __size_             = __other.size();
   }
 
@@ -1040,11 +1049,11 @@ public:
     return std::__try_key_extraction<key_type>(
         [this](const key_type& __key, _Args&&... __args2) {
           auto [__parent, __child] = __find_equal(__key);
-          __node_pointer __r       = static_cast<__node_pointer>(__child);
+          __node_pointer __r       = std::__static_fancy_pointer_cast<__node_pointer>(__child);
           bool __inserted          = false;
           if (__child == nullptr) {
             __node_holder __h = __construct_node(std::forward<_Args>(__args2)...);
-            __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
+            __insert_node_at(__parent, __child, std::__static_fancy_pointer_cast<__node_base_pointer>(__h.get()));
             __r        = __h.release();
             __inserted = true;
           }
@@ -1053,10 +1062,10 @@ public:
         [this](_Args&&... __args2) {
           __node_holder __h        = __construct_node(std::forward<_Args>(__args2)...);
           auto [__parent, __child] = __find_equal(__h->__get_value());
-          __node_pointer __r       = static_cast<__node_pointer>(__child);
+          __node_pointer __r       = std::__static_fancy_pointer_cast<__node_pointer>(__child);
           bool __inserted          = false;
           if (__child == nullptr) {
-            __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
+            __insert_node_at(__parent, __child, std::__static_fancy_pointer_cast<__node_base_pointer>(__h.get()));
             __r        = __h.release();
             __inserted = true;
           }
@@ -1071,11 +1080,11 @@ public:
         [this, __p](const key_type& __key, _Args&&... __args2) {
           __node_base_pointer __dummy;
           auto [__parent, __child] = __find_equal(__p, __dummy, __key);
-          __node_pointer __r       = static_cast<__node_pointer>(__child);
+          __node_pointer __r       = std::__static_fancy_pointer_cast<__node_pointer>(__child);
           bool __inserted          = false;
           if (__child == nullptr) {
             __node_holder __h = __construct_node(std::forward<_Args>(__args2)...);
-            __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
+            __insert_node_at(__parent, __child, std::__static_fancy_pointer_cast<__node_base_pointer>(__h.get()));
             __r        = __h.release();
             __inserted = true;
           }
@@ -1085,9 +1094,9 @@ public:
           __node_holder __h = __construct_node(std::forward<_Args>(__args2)...);
           __node_base_pointer __dummy;
           auto [__parent, __child] = __find_equal(__p, __dummy, __h->__get_value());
-          __node_pointer __r       = static_cast<__node_pointer>(__child);
+          __node_pointer __r       = std::__static_fancy_pointer_cast<__node_pointer>(__child);
           if (__child == nullptr) {
-            __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
+            __insert_node_at(__parent, __child, std::__static_fancy_pointer_cast<__node_base_pointer>(__h.get()));
             __r = __h.release();
           }
           return pair<iterator, bool>(iterator(__r), __child == nullptr);
@@ -1101,25 +1110,27 @@ public:
       return;
 
     if (__root() == nullptr) { // Make sure we always have a root node
-      __insert_node_at(
-          __end_node(), __end_node()->__left_, static_cast<__node_base_pointer>(__construct_node(*__first).release()));
+      __insert_node_at(__end_node(),
+                       __end_node()->__left_,
+                       std::__static_fancy_pointer_cast<__node_base_pointer>(__construct_node(*__first).release()));
       ++__first;
     }
 
-    auto __max_node = static_cast<__node_pointer>(std::__tree_max(static_cast<__node_base_pointer>(__root())));
+    auto __max_node = std::__static_fancy_pointer_cast<__node_pointer>(
+        std::__tree_max(std::__static_fancy_pointer_cast<__node_base_pointer>(__root())));
 
     for (; __first != __last; ++__first) {
       __node_holder __nd = __construct_node(*__first);
       // Always check the max node first. This optimizes for sorted ranges inserted at the end.
       if (!value_comp()(__nd->__get_value(), __max_node->__get_value())) { // __node >= __max_val
-        __insert_node_at(static_cast<__end_node_pointer>(__max_node),
+        __insert_node_at(std::__static_fancy_pointer_cast<__end_node_pointer>(__max_node),
                          __max_node->__right_,
-                         static_cast<__node_base_pointer>(__nd.get()));
+                         std::__static_fancy_pointer_cast<__node_base_pointer>(__nd.get()));
         __max_node = __nd.release();
       } else {
         __end_node_pointer __parent;
         __node_base_pointer& __child = __find_leaf_high(__parent, __nd->__get_value());
-        __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd.release()));
+        __insert_node_at(__parent, __child, std::__static_fancy_pointer_cast<__node_base_pointer>(__nd.release()));
       }
     }
   }
@@ -1130,12 +1141,14 @@ public:
       return;
 
     if (__root() == nullptr) {
-      __insert_node_at(
-          __end_node(), __end_node()->__left_, static_cast<__node_base_pointer>(__construct_node(*__first).release()));
+      __insert_node_at(__end_node(),
+                       __end_node()->__left_,
+                       std::__static_fancy_pointer_cast<__node_base_pointer>(__construct_node(*__first).release()));
       ++__first;
     }
 
-    auto __max_node = static_cast<__node_pointer>(std::__tree_max(static_cast<__node_base_pointer>(__root())));
+    auto __max_node = std::__static_fancy_pointer_cast<__node_pointer>(
+        std::__tree_max(std::__static_fancy_pointer_cast<__node_base_pointer>(__root())));
 
     using __reference = decltype(*__first);
 
@@ -1144,29 +1157,31 @@ public:
           [this, &__max_node](const key_type& __key, __reference&& __val) {
             if (value_comp()(__max_node->__get_value(), __key)) { // __key > __max_node
               __node_holder __nd = __construct_node(std::forward<__reference>(__val));
-              __insert_node_at(static_cast<__end_node_pointer>(__max_node),
+              __insert_node_at(std::__static_fancy_pointer_cast<__end_node_pointer>(__max_node),
                                __max_node->__right_,
-                               static_cast<__node_base_pointer>(__nd.get()));
+                               std::__static_fancy_pointer_cast<__node_base_pointer>(__nd.get()));
               __max_node = __nd.release();
             } else {
               auto [__parent, __child] = __find_equal(__key);
               if (__child == nullptr) {
                 __node_holder __nd = __construct_node(std::forward<__reference>(__val));
-                __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd.release()));
+                __insert_node_at(
+                    __parent, __child, std::__static_fancy_pointer_cast<__node_base_pointer>(__nd.release()));
               }
             }
           },
           [this, &__max_node](__reference&& __val) {
             __node_holder __nd = __construct_node(std::forward<__reference>(__val));
             if (value_comp()(__max_node->__get_value(), __nd->__get_value())) { // __node > __max_node
-              __insert_node_at(static_cast<__end_node_pointer>(__max_node),
+              __insert_node_at(std::__static_fancy_pointer_cast<__end_node_pointer>(__max_node),
                                __max_node->__right_,
-                               static_cast<__node_base_pointer>(__nd.get()));
+                               std::__static_fancy_pointer_cast<__node_base_pointer>(__nd.get()));
               __max_node = __nd.release();
             } else {
               auto [__parent, __child] = __find_equal(__nd->__get_value());
               if (__child == nullptr) {
-                __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd.release()));
+                __insert_node_at(
+                    __parent, __child, std::__static_fancy_pointer_cast<__node_base_pointer>(__nd.release()));
               }
             }
           },
@@ -1212,7 +1227,7 @@ public:
     auto [__, __match] = __find_equal(__key);
     if (__match == nullptr)
       return end();
-    return iterator(static_cast<__node_pointer>(__match));
+    return iterator(std::__static_fancy_pointer_cast<__node_pointer>(__match));
   }
 
   template <class _Key>
@@ -1220,7 +1235,7 @@ public:
     auto [__, __match] = __find_equal(__key);
     if (__match == nullptr)
       return end();
-    return const_iterator(static_cast<__node_pointer>(__match));
+    return const_iterator(std::__static_fancy_pointer_cast<__node_pointer>(__match));
   }
 
   template <class _Key>
@@ -1237,14 +1252,15 @@ public:
       auto __comp_res = __comp(__v, __rt->__get_value());
 
       if (__comp_res.__less()) {
-        __result = static_cast<__end_node_pointer>(__rt);
-        __rt     = static_cast<__node_pointer>(__rt->__left_);
+        __result = std::__static_fancy_pointer_cast<__end_node_pointer>(__rt);
+        __rt     = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__left_);
       } else if (__comp_res.__greater()) {
-        __rt = static_cast<__node_pointer>(__rt->__right_);
+        __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__right_);
       } else if _LIBCPP_CONSTEXPR (_LowerBound) {
-        return static_cast<__end_node_pointer>(__rt);
+        return std::__static_fancy_pointer_cast<__end_node_pointer>(__rt);
       } else {
-        return __rt->__right_ ? static_cast<__end_node_pointer>(std::__tree_min(__rt->__right_)) : __result;
+        return __rt->__right_ ? std::__static_fancy_pointer_cast<__end_node_pointer>(std::__tree_min(__rt->__right_))
+                              : __result;
       }
     }
     return __result;
@@ -1410,14 +1426,14 @@ private:
       if (!__ptr)
         return;
 
-      (*this)(static_cast<__node_pointer>(__ptr->__left_));
+      (*this)(std::__static_fancy_pointer_cast<__node_pointer>(__ptr->__left_));
 
       auto __right = __ptr->__right_;
 
       __node_traits::destroy(__alloc_, std::addressof(__ptr->__get_value()));
       __node_traits::deallocate(__alloc_, __ptr, 1);
 
-      (*this)(static_cast<__node_pointer>(__right));
+      (*this)(std::__static_fancy_pointer_cast<__node_pointer>(__right));
     }
   };
 
@@ -1436,18 +1452,20 @@ private:
     __node_holder __new_node = __construct(__src->__get_value());
 
     unique_ptr<__node, __tree_deleter> __left(
-        __construct_from_tree(static_cast<__node_pointer>(__src->__left_), __construct), __node_alloc_);
-    __node_pointer __right = __construct_from_tree(static_cast<__node_pointer>(__src->__right_), __construct);
+        __construct_from_tree(std::__static_fancy_pointer_cast<__node_pointer>(__src->__left_), __construct),
+        __node_alloc_);
+    __node_pointer __right =
+        __construct_from_tree(std::__static_fancy_pointer_cast<__node_pointer>(__src->__right_), __construct);
 
     __node_pointer __new_node_ptr = __new_node.release();
 
     __new_node_ptr->__is_black_ = __src->__is_black_;
-    __new_node_ptr->__left_     = static_cast<__node_base_pointer>(__left.release());
-    __new_node_ptr->__right_    = static_cast<__node_base_pointer>(__right);
+    __new_node_ptr->__left_     = std::__static_fancy_pointer_cast<__node_base_pointer>(__left.release());
+    __new_node_ptr->__right_    = std::__static_fancy_pointer_cast<__node_base_pointer>(__right);
     if (__new_node_ptr->__left_)
-      __new_node_ptr->__left_->__parent_ = static_cast<__end_node_pointer>(__new_node_ptr);
+      __new_node_ptr->__left_->__parent_ = std::__static_fancy_pointer_cast<__end_node_pointer>(__new_node_ptr);
     if (__new_node_ptr->__right_)
-      __new_node_ptr->__right_->__parent_ = static_cast<__end_node_pointer>(__new_node_ptr);
+      __new_node_ptr->__right_->__parent_ = std::__static_fancy_pointer_cast<__end_node_pointer>(__new_node_ptr);
     return __new_node_ptr;
   }
 
@@ -1486,30 +1504,30 @@ private:
 
     // If we already have a left node in the destination tree, reuse it and copy-assign recursively
     if (__dest->__left_) {
-      __dest->__left_ = static_cast<__node_base_pointer>(__assign_from_tree(
-          static_cast<__node_pointer>(__dest->__left_),
-          static_cast<__node_pointer>(__src->__left_),
+      __dest->__left_ = std::__static_fancy_pointer_cast<__node_base_pointer>(__assign_from_tree(
+          std::__static_fancy_pointer_cast<__node_pointer>(__dest->__left_),
+          std::__static_fancy_pointer_cast<__node_pointer>(__src->__left_),
           __assign,
           __construct_subtree));
 
       // Otherwise, we must create new nodes; copy-construct from here on
     } else if (__src->__left_) {
-      auto __new_left       = __construct_subtree(static_cast<__node_pointer>(__src->__left_));
-      __dest->__left_       = static_cast<__node_base_pointer>(__new_left);
-      __new_left->__parent_ = static_cast<__end_node_pointer>(__dest);
+      auto __new_left       = __construct_subtree(std::__static_fancy_pointer_cast<__node_pointer>(__src->__left_));
+      __dest->__left_       = std::__static_fancy_pointer_cast<__node_base_pointer>(__new_left);
+      __new_left->__parent_ = std::__static_fancy_pointer_cast<__end_node_pointer>(__dest);
     }
 
     // Identical to the left case above, just for the right nodes
     if (__dest->__right_) {
-      __dest->__right_ = static_cast<__node_base_pointer>(__assign_from_tree(
-          static_cast<__node_pointer>(__dest->__right_),
-          static_cast<__node_pointer>(__src->__right_),
+      __dest->__right_ = std::__static_fancy_pointer_cast<__node_base_pointer>(__assign_from_tree(
+          std::__static_fancy_pointer_cast<__node_pointer>(__dest->__right_),
+          std::__static_fancy_pointer_cast<__node_pointer>(__src->__right_),
           __assign,
           __construct_subtree));
     } else if (__src->__right_) {
-      auto __new_right       = __construct_subtree(static_cast<__node_pointer>(__src->__right_));
-      __dest->__right_       = static_cast<__node_base_pointer>(__new_right);
-      __new_right->__parent_ = static_cast<__end_node_pointer>(__dest);
+  ...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/180546


More information about the libcxx-commits mailing list