Skip to content

[clang][bytecode] Fix self-init diagnostics in C++23 #141044

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

Merged
merged 1 commit into from
May 22, 2025

Conversation

tbaederr
Copy link
Contributor

No description provided.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:bytecode Issues for the clang bytecode constexpr interpreter labels May 22, 2025
@llvmbot
Copy link
Member

llvmbot commented May 22, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

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

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Interp.cpp (+2-1)
  • (modified) clang/test/AST/ByteCode/cxx23.cpp (+6)
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index b3ac84ce9278f..7cc0d2a526480 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -670,7 +670,8 @@ bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
   if (const auto *VD = Ptr.getDeclDesc()->asVarDecl();
       VD && (VD->isConstexpr() || VD->hasGlobalStorage())) {
 
-    if (!S.getLangOpts().CPlusPlus23 && VD == S.EvaluatingDecl) {
+    if (VD == S.EvaluatingDecl &&
+        !(S.getLangOpts().CPlusPlus23 && VD->getType()->isReferenceType())) {
       if (!S.getLangOpts().CPlusPlus14 &&
           !VD->getType().isConstant(S.getASTContext())) {
         // Diagnose as non-const read.
diff --git a/clang/test/AST/ByteCode/cxx23.cpp b/clang/test/AST/ByteCode/cxx23.cpp
index ce18a9d473302..417d35dbca946 100644
--- a/clang/test/AST/ByteCode/cxx23.cpp
+++ b/clang/test/AST/ByteCode/cxx23.cpp
@@ -73,6 +73,12 @@ constexpr int k(int n) {
 }
 constexpr int k0 = k(0);
 
+#if __cplusplus >= 202302L
+constexpr int &b = b; // all-error {{must be initialized by a constant expression}} \
+                      // all-note {{initializer of 'b' is not a constant expression}} \
+                      // all-note {{declared here}}
+#endif
+
 namespace StaticLambdas {
   constexpr auto static_capture_constexpr() {
     char n = 'n';

@tbaederr tbaederr merged commit d45f1d0 into llvm:main May 22, 2025
15 checks passed
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
ajaden-codes pushed a commit to Jaddyen/llvm-project that referenced this pull request Jun 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:bytecode Issues for the clang bytecode constexpr interpreter 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.

2 participants