https://github.com/tbaederr updated 
https://github.com/llvm/llvm-project/pull/206468

>From 9561cf3d767185959a78436c57117ec3937f30e0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]>
Date: Mon, 29 Jun 2026 14:11:47 +0200
Subject: [PATCH] [clang][bytecode] Ignore irrelevant InitializingPtrs in
 dynamic_cast

If the initializing pointer is unrealted to the one we're operating on,
ignore it.
---
 clang/lib/AST/ByteCode/Interp.cpp        |  4 ++--
 clang/test/AST/ByteCode/dynamic-cast.cpp | 14 ++++++++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)

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'}}
+}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to