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

via cfe-commits cfe-commits at lists.llvm.org
Thu May 22 04:35:49 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>



---
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
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';

``````````

</details>


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


More information about the cfe-commits mailing list