[clang] cf7864b - [clang][bytecode] Allow downcasts of constexpr-unknown values (#204084)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 16 07:17:09 PDT 2026
Author: Timm Baeder
Date: 2026-06-16T16:17:04+02:00
New Revision: cf7864b62ae3bac28b44024cbbcfd3800c8f35f9
URL: https://github.com/llvm/llvm-project/commit/cf7864b62ae3bac28b44024cbbcfd3800c8f35f9
DIFF: https://github.com/llvm/llvm-project/commit/cf7864b62ae3bac28b44024cbbcfd3800c8f35f9.diff
LOG: [clang][bytecode] Allow downcasts of constexpr-unknown values (#204084)
They might be diagnosed later.
Added:
Modified:
clang/lib/AST/ByteCode/Interp.cpp
clang/test/SemaCXX/constant-expression-p2280r4.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 954e4e2cddb8d..e5bf9c0c590ac 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -558,9 +558,16 @@ bool CheckDowncast(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
return true;
const auto *E = cast<CastExpr>(S.Current->getExpr(OpPC));
- QualType TargetQT = E->getType()->getPointeeType();
+ QualType ExprTy = E->getType();
+ if (ExprTy->isPointerOrReferenceType())
+ ExprTy = ExprTy->getPointeeType();
+
+ QualType TargetQT = ExprTy;
QualType MostDerivedQT = Ptr.getDeclPtr().getType();
+ if (MostDerivedQT->isPointerOrReferenceType())
+ MostDerivedQT = MostDerivedQT->getPointeeType();
+
S.CCEDiag(E, diag::note_constexpr_invalid_downcast)
<< MostDerivedQT << TargetQT;
@@ -1578,9 +1585,6 @@ bool GetPtrDerivedPop(InterpState &S, CodePtr OpPC, uint32_t Off, bool NullOK,
return true;
}
- if (isConstexprUnknown(Ptr))
- return false;
-
if (!Ptr.getFieldDesc()->isRecord()) {
S.Stk.push<Pointer>(Ptr);
return true;
diff --git a/clang/test/SemaCXX/constant-expression-p2280r4.cpp b/clang/test/SemaCXX/constant-expression-p2280r4.cpp
index e282688ed2248..bb5499e57ac78 100644
--- a/clang/test/SemaCXX/constant-expression-p2280r4.cpp
+++ b/clang/test/SemaCXX/constant-expression-p2280r4.cpp
@@ -296,14 +296,14 @@ namespace casting {
struct B : A {};
struct C : A {};
extern A &a;
- extern B &b; // nointerpreter-note {{declared here}}
+ extern B &b; // expected-note {{declared here}}
constexpr B &t1 = (B&)a; // expected-error {{must be initialized by a constant expression}} \
- // nointerpreter-note {{cannot cast object of dynamic type 'A' to type 'B'}}
+ // expected-note {{cannot cast object of dynamic type 'A' to type 'B'}}
constexpr B &t2 = (B&)(A&)b; // expected-error {{must be initialized by a constant expression}} \
- // nointerpreter-note {{initializer of 'b' is not a constant expression}}
- constexpr bool t3 = &b + 1 == &(B&)(A&)b; // interpreter-error {{constexpr variable 't3' must be initialized by a constant expression}}
+ // expected-note {{initializer of 'b' is not a constant expression}}
+ constexpr bool t3 = &b + 1 == &(B&)(A&)b;
constexpr C &t4 = (C&)(A&)b; // expected-error {{must be initialized by a constant expression}} \
- // nointerpreter-note {{cannot cast object of dynamic type 'B' to type 'C'}}
+ // expected-note {{cannot cast object of dynamic type 'B' to type 'C'}}
}
namespace pointer_comparisons {
More information about the cfe-commits
mailing list