[clang] [clang][bytecode] Fix discarded dynamic casts (PR #200723)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Sun May 31 22:43:39 PDT 2026
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/200723
If they are checked.
>From 7ee7484b9dfce568a26528f6fc6533f3bd707a0b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Mon, 1 Jun 2026 07:42:56 +0200
Subject: [PATCH] [clang][bytecode] Fix discarded dynamic casts
If they are checked.
---
clang/lib/AST/ByteCode/Compiler.cpp | 2 ++
clang/test/AST/ByteCode/cxx23.cpp | 4 ++++
2 files changed, 6 insertions(+)
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 774a853950837..322c1f827223b 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -3570,6 +3570,8 @@ bool Compiler<Emitter>::VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) {
if (!this->emitCheckDynamicCast(E))
return false;
+ if (DiscardResult)
+ return this->emitPopPtr(E);
return true;
}
diff --git a/clang/test/AST/ByteCode/cxx23.cpp b/clang/test/AST/ByteCode/cxx23.cpp
index 7b2d1c6e7e1e1..4619023af83e6 100644
--- a/clang/test/AST/ByteCode/cxx23.cpp
+++ b/clang/test/AST/ByteCode/cxx23.cpp
@@ -641,4 +641,8 @@ namespace DynamicCast {
// all23-note {{dynamic_cast applied to object 'a' whose dynamic type is not constant}} \
// all20-note {{function parameter 'a' with unknown value cannot be used in a constant expression}}
}
+
+ struct S {};
+ constexpr S s;
+ constexpr int foo = (dynamic_cast<const S &>(s), 0);
}
More information about the cfe-commits
mailing list