Skip to content

Commit 77cccf5

Browse files
yronglinyuxuanchen1997
authored andcommitted
[Clang][Interp] Visit DecompositionDecl and create a local variable (#100400)
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; } ``` We should visit `DecompositionDecl` and create a local variable but not a create a dummy value directly. --------- Signed-off-by: yronglin <[email protected]> Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60250516
1 parent ed25eb3 commit 77cccf5

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

clang/lib/AST/Interp/Compiler.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5239,6 +5239,10 @@ bool Compiler<Emitter>::visitDeclRef(const ValueDecl *D, const Expr *E) {
52395239
return false;
52405240
};
52415241

5242+
// DecompositionDecls are just proxies for us.
5243+
if (isa<DecompositionDecl>(VD))
5244+
return revisit(VD);
5245+
52425246
// Visit local const variables like normal.
52435247
if ((VD->hasGlobalStorage() || VD->isLocalVarDecl() ||
52445248
VD->isStaticDataMember()) &&

clang/test/SemaCXX/cxx1z-decomposition.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %clang_cc1 -std=c++17 -Wc++20-extensions -verify=expected %s
22
// RUN: %clang_cc1 -std=c++20 -Wpre-c++20-compat -verify=expected %s
3+
// RUN: %clang_cc1 -std=c++20 -Wpre-c++20-compat -fexperimental-new-constant-interpreter -verify=expected %s
34

45
void use_from_own_init() {
56
auto [a] = a; // expected-error {{binding 'a' cannot appear in the initializer of its own decomposition declaration}}

0 commit comments

Comments
 (0)