[clang] [clang] Handle consteval constructors with default initialization. (PR #144970)
Eli Friedman via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 29 09:24:34 PDT 2025
https://github.com/efriedma-quic updated https://github.com/llvm/llvm-project/pull/144970
>From ef713152a6fb5d2321330d8fbaa77970e8666b13 Mon Sep 17 00:00:00 2001
From: Eli Friedman <efriedma at quicinc.com>
Date: Thu, 19 Jun 2025 18:29:49 -0700
Subject: [PATCH 1/2] [clang] Handle consteval constructors with default
initialization.
This is a simple extension of 443377a9d1a8d4a69a317a1a892184c59dd0aec6
to also handle the implicit expressions created by default
initialization. This usually doesn't matter, but it's relevant if the
constructor stores "this" as a member.
Fixes #135281 .
---
clang/lib/Sema/SemaDecl.cpp | 6 ++++++
.../test/SemaCXX/cxx2b-consteval-propagate.cpp | 18 ++++++++++++++++++
2 files changed, 24 insertions(+)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index bbd63372c168b..e10dc65897b8a 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -14423,6 +14423,9 @@ void Sema::ActOnUninitializedDecl(Decl *RealDecl) {
Var->getType().getAddressSpace() == LangAS::hlsl_input)
return;
+ if (getLangOpts().CPlusPlus)
+ ActOnCXXEnterDeclInitializer(nullptr, Var);
+
// C++03 [dcl.init]p9:
// If no initializer is specified for an object, and the
// object is of (possibly cv-qualified) non-POD class type (or
@@ -14458,6 +14461,9 @@ void Sema::ActOnUninitializedDecl(Decl *RealDecl) {
}
CheckCompleteVariableDeclaration(Var);
+
+ if (getLangOpts().CPlusPlus)
+ ActOnCXXExitDeclInitializer(nullptr, Var);
}
}
diff --git a/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp b/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
index dd5063cb29c5b..aa5d79af589ca 100644
--- a/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
+++ b/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
@@ -576,3 +576,21 @@ int f() {
//expected-note at -2 {{read of non-const variable 'a' is not allowed in a constant expression}}
}
}
+
+#if __cplusplus >= 202302L
+namespace GH135281 {
+ struct B {
+ const void* p;
+ consteval B() : p{this} {}
+ };
+ B b;
+ B b2{};
+ B &&b3{};
+ void f() {
+ static B b4;
+ B b5; // expected-error {{call to consteval function 'GH135281::B::B' is not a constant expression}} \
+ // expected-note {{pointer to temporary is not a constant expression}} \
+ // expected-note {{temporary created here}}
+ }
+}
+#endif
>From b508252e5d378629119ce82aa3ae2fc2e042dcf5 Mon Sep 17 00:00:00 2001
From: Eli Friedman <efriedma at quicinc.com>
Date: Sun, 29 Jun 2025 09:24:26 -0700
Subject: [PATCH 2/2] Apply suggestions from code review
Co-authored-by: Vassil Vassilev <v.g.vassilev at gmail.com>
---
clang/lib/Sema/SemaDecl.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index e10dc65897b8a..06d0ca2adb955 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -14424,7 +14424,7 @@ void Sema::ActOnUninitializedDecl(Decl *RealDecl) {
return;
if (getLangOpts().CPlusPlus)
- ActOnCXXEnterDeclInitializer(nullptr, Var);
+ ActOnCXXEnterDeclInitializer(/*Scope=*/nullptr, Var);
// C++03 [dcl.init]p9:
// If no initializer is specified for an object, and the
@@ -14463,7 +14463,7 @@ void Sema::ActOnUninitializedDecl(Decl *RealDecl) {
CheckCompleteVariableDeclaration(Var);
if (getLangOpts().CPlusPlus)
- ActOnCXXExitDeclInitializer(nullptr, Var);
+ ActOnCXXExitDeclInitializer(/*Scope=*/nullptr, Var);
}
}
More information about the cfe-commits
mailing list