[clang] [Clang][Interp] Fix handling of `DecompositionDecl` (PR #100400)

via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 24 08:14:39 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (yronglin)

<details>
<summary>Changes</summary>

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

```

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


2 Files Affected:

- (modified) clang/lib/AST/Interp/Compiler.cpp (+11-2) 
- (modified) clang/test/SemaCXX/cxx1z-decomposition.cpp (+1) 


``````````diff
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}}

``````````

</details>


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


More information about the cfe-commits mailing list