Skip to content

[clang] Handle consteval constructors with default initialization. #144970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

efriedma-quic
Copy link
Collaborator

This is a simple extension of 443377a 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 .

This is a simple extension of 443377a
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 llvm#135281 .
@efriedma-quic efriedma-quic requested a review from cor3ntin June 20, 2025 02:24
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 20, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 20, 2025

@llvm/pr-subscribers-clang

Author: Eli Friedman (efriedma-quic)

Changes

This is a simple extension of 443377a 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 .


Full diff: https://github.com/llvm/llvm-project/pull/144970.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaDecl.cpp (+6)
  • (modified) clang/test/SemaCXX/cxx2b-consteval-propagate.cpp (+18)
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@-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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

consteval constructor cannot store this inside object
2 participants