[clang] [llvm] [Clang][ConstExpr] Reject member function calls before base initialization (PR #219358)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 27 21:23:18 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: ahmed mohamed kamel (AhmedKamel10)
<details>
<summary>Changes</summary>
This patch diagnoses undefined behavior during constant evaluation when a
member function is called for an object under construction before all
base classes have been initialized.
Fixes #<!-- -->211286
---
Full diff: https://github.com/llvm/llvm-project/pull/219358.diff
2 Files Affected:
- (modified) clang/lib/AST/ExprConstant.cpp (+6)
- (modified) clang/test/SemaCXX/constant-expression-cxx2a.cpp (+19)
``````````diff
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 9702105951b7b..34c1cb39dd8b1 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -6625,6 +6625,12 @@ static bool
checkNonVirtualMemberCallThisPointer(EvalInfo &Info, const Expr *E,
const LValue &This,
const CXXMethodDecl *NamedMember) {
+ ConstructionPhase Phase =
+ Info.isEvaluatingCtorDtor(This.getLValueBase(), This.Designator.Entries);
+ if (Phase == ConstructionPhase::Bases) {
+ Info.FFDiag(E);
+ return false;
+ }
return checkDynamicType(
Info, E, This,
isa<CXXDestructorDecl>(NamedMember) ? AK_Destroy : AK_MemberCall, false);
diff --git a/clang/test/SemaCXX/constant-expression-cxx2a.cpp b/clang/test/SemaCXX/constant-expression-cxx2a.cpp
index 870865646a61e..88b4808a4671e 100644
--- a/clang/test/SemaCXX/constant-expression-cxx2a.cpp
+++ b/clang/test/SemaCXX/constant-expression-cxx2a.cpp
@@ -1544,3 +1544,22 @@ namespace GH197403 {
}
static_assert(test() == 0);
}
+
+namespace GH211286 {
+ struct A {
+ constexpr A(int x) : val(x) {}
+ int val;
+ };
+
+ struct B : A {
+ constexpr int f() const { return 42; }
+ constexpr B() : A(f()) {} // expected-note 2{{subexpression not valid in a constant expression}}
+ };
+
+ constexpr int foo() {
+ constexpr B b{}; // expected-error {{constexpr variable 'b' must be initialized by a constant expression}} expected-note 2{{in call to 'B()'}}
+ return b.val;
+ }
+
+ constexpr auto x = foo(); // expected-error {{constexpr variable 'x' must be initialized by a constant expression}} expected-note {{in call to 'foo()'}}
+}
\ No newline at end of file
``````````
</details>
https://github.com/llvm/llvm-project/pull/219358
More information about the llvm-commits
mailing list