https://github.com/yronglin created 
https://github.com/llvm/llvm-project/pull/100400

The following code should be well-formed:
```C++
float decompose_complex(_Complex float cf) {
  static _Complex float scf;
  auto &[sre, sim] = scf;
  // ok, this is references initialized by constant expressions all the way down
  static_assert(&sre == &__real scf);
  static_assert(&sim == &__imag scf);

  auto [re, im] = cf;
  return re*re + im*im;
}

```

>From 6ede7586fc4602ab15be59c58fe5f7181ffaca16 Mon Sep 17 00:00:00 2001
From: yronglin <yronglin...@gmail.com>
Date: Wed, 24 Jul 2024 23:12:27 +0800
Subject: [PATCH] [Clang][Interp] Fix handling of DecompositionDecl

Signed-off-by: yronglin <yronglin...@gmail.com>
---
 clang/lib/AST/Interp/Compiler.cpp          | 13 +++++++++++--
 clang/test/SemaCXX/cxx1z-decomposition.cpp |  1 +
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/Interp/Compiler.cpp 
b/clang/lib/AST/Interp/Compiler.cpp
index 0fc93c14131e6..6e2f0dbc39dcf 100644
--- a/clang/lib/AST/Interp/Compiler.cpp
+++ b/clang/lib/AST/Interp/Compiler.cpp
@@ -3593,8 +3593,14 @@ VarCreationState Compiler<Emitter>::visitDecl(const 
VarDecl *VD) {
   if (R.notCreated())
     return R;
 
-  if (R)
-    return true;
+  if (R) {
+    bool Ok = true;
+    if (const auto *DD = dyn_cast<DecompositionDecl>(VD))
+      for (const auto *BD : DD->bindings())
+        if (const auto *HoldingVar = BD->getHoldingVar())
+          Ok &= this->visitDecl(HoldingVar);
+    return Ok;
+  }
 
   if (!R && Context::shouldBeGloballyIndexed(VD)) {
     if (auto GlobalIndex = P.getGlobal(VD)) {
@@ -5234,6 +5240,9 @@ bool Compiler<Emitter>::visitDeclRef(const ValueDecl *D, 
const Expr *E) {
           return false;
         };
 
+        if (isa<DecompositionDecl>(VD))
+          return revisit(VD);
+
         // Visit local const variables like normal.
         if ((VD->hasGlobalStorage() || VD->isLocalVarDecl() ||
              VD->isStaticDataMember()) &&
diff --git a/clang/test/SemaCXX/cxx1z-decomposition.cpp 
b/clang/test/SemaCXX/cxx1z-decomposition.cpp
index 305a9ac2ebc24..19c730303625e 100644
--- a/clang/test/SemaCXX/cxx1z-decomposition.cpp
+++ b/clang/test/SemaCXX/cxx1z-decomposition.cpp
@@ -1,5 +1,6 @@
 // RUN: %clang_cc1 -std=c++17 -Wc++20-extensions -verify=expected %s
 // RUN: %clang_cc1 -std=c++20 -Wpre-c++20-compat -verify=expected %s
+// RUN: %clang_cc1 -std=c++20 -Wpre-c++20-compat 
-fexperimental-new-constant-interpreter -verify=expected %s
 
 void use_from_own_init() {
   auto [a] = a; // expected-error {{binding 'a' cannot appear in the 
initializer of its own decomposition declaration}}

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to