[clang] [clang][bytecode] Ignore irrelevant InitializingPtrs in dynamic_cast (PR #206468)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 29 05:16:08 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
If the initializing pointer is unrealted to the one we're operating on, ignore it.
---
Full diff: https://github.com/llvm/llvm-project/pull/206468.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/Interp.cpp (+2-2)
- (modified) clang/test/AST/ByteCode/dynamic-cast.cpp (+14)
``````````diff
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 0396482517046..249733fb1ea14 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -2058,10 +2058,10 @@ bool DynamicCast(InterpState &S, CodePtr OpPC, const Type *DestTypePtr,
// Our given pointer, limited by the base that's currently being initialized,
// if any.
PtrView LimitedPtr;
- if (S.InitializingPtrs.empty()) {
+ if (S.InitializingPtrs.empty() ||
+ S.InitializingPtrs.back().block() != Ptr.block()) {
LimitedPtr = Ptr.stripBaseCasts().view();
} else {
- // FIXME: Is this always the correct block?
LimitedPtr = S.InitializingPtrs.back();
assert(LimitedPtr.block() == Ptr.block());
}
diff --git a/clang/test/AST/ByteCode/dynamic-cast.cpp b/clang/test/AST/ByteCode/dynamic-cast.cpp
index a40b455cecabf..fa947e14cfa13 100644
--- a/clang/test/AST/ByteCode/dynamic-cast.cpp
+++ b/clang/test/AST/ByteCode/dynamic-cast.cpp
@@ -310,3 +310,17 @@ namespace Invalid {
static_assert(&dynamic_cast<S&>((X&)x), ""); // both-error {{not an integral constant expression}} \
// both-note {{initializer of 'x' is not a constant expression}}
}
+
+namespace UnrelatedInitializingPtr {
+ struct A {
+ virtual void foo();
+ };
+ struct B : A {};
+ struct C : A {};
+ struct D : B {};
+
+ constexpr D d;
+ constexpr A &a = (B &)d;
+ constexpr auto p = dynamic_cast<C &>(a); // both-error {{must be initialized by a constant expression}} \
+ // both-note {{reference dynamic_cast failed: dynamic type 'UnrelatedInitializingPtr::D' of operand does not have a base class of type 'C'}}
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/206468
More information about the cfe-commits
mailing list