This is an automated email from the ASF dual-hosted git repository.

Mryange pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new df8bf1675f9 [refine](core) add compile-time safety checks to 
assert_cast (#63133)
df8bf1675f9 is described below

commit df8bf1675f96fbf935feaa05cd3bbce6a0e4e4af
Author: Mryange <[email protected]>
AuthorDate: Tue May 12 19:05:51 2026 +0800

    [refine](core) add compile-time safety checks to assert_cast (#63133)
    
    ### What problem does this PR solve?
    
    Issue Number: N/A
    
    Problem Summary:
    
    `assert_cast` previously only performed runtime type checks (via
    `typeid` comparison). Misuse — such as casting between unrelated types
    or upcasting — would only be caught at runtime, making it easy for
    incorrect usage to slip into release builds where
    `TypeCheckOnRelease::DISABLE` turns checks into `static_cast` without
    any verification.
    
    
    This catches invalid casts at build time instead of producing undefined
    behavior or hard-to-diagnose runtime errors.
---
 be/src/core/assert_cast.h | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/be/src/core/assert_cast.h b/be/src/core/assert_cast.h
index 57e1125f88c..acadf8705a7 100644
--- a/be/src/core/assert_cast.h
+++ b/be/src/core/assert_cast.h
@@ -42,6 +42,9 @@ struct AssertCastNormalizedType {
 template <typename T>
 using AssertCastNormalizedType_t = typename AssertCastNormalizedType<T>::type;
 
+template <typename T>
+using AssertCastClassType_t = 
std::remove_pointer_t<AssertCastNormalizedType_t<T>>;
+
 /** Perform static_cast in release build when TypeCheckOnRelease is set to 
DISABLE.
   * Checks type by comparing typeid and throw an exception in all the other 
situations.
   * The exact match of the type is checked. That is, cast to the ancestor will 
be unsuccessful.
@@ -50,6 +53,11 @@ template <typename To, TypeCheckOnRelease check = 
TypeCheckOnRelease::ENABLE, ty
 PURE To assert_cast(From&& from) {
     static_assert(!std::is_same_v<AssertCastNormalizedType_t<To>, 
AssertCastNormalizedType_t<From>>,
                   "assert_cast is redundant for the same type after removing 
cv/ref qualifiers");
+    static_assert(std::is_class_v<AssertCastClassType_t<To>> &&
+                          std::is_class_v<AssertCastClassType_t<From>>,
+                  "assert_cast requires casting between class 
pointer/reference types");
+    static_assert(std::is_base_of_v<AssertCastClassType_t<From>, 
AssertCastClassType_t<To>>,
+                  "assert_cast only supports downcast from a base type to a 
derived type");
 
     // https://godbolt.org/z/nrsx7nYhs
     // perform_cast will not be compiled to asm in release build with 
TypeCheckOnRelease::DISABLE


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to