https://gcc.gnu.org/g:68b42e35b48d1f9254df05603c92d62f71f6d4df
commit r14-12648-g68b42e35b48d1f9254df05603c92d62f71f6d4df Author: Jakub Jelinek <[email protected]> Date: Wed Apr 15 08:55:48 2026 +0200 c++: Mark in cxx_mark_addressable DECL_VALUE_EXPR of DECL_ANON_UNION_VAR_P too [PR124850] The following testcase ICEs since my PR53932 change in checking, because it sees address of the unnamed anon union VAR_DECL taken but without having TREE_ADDRESSABLE set. The following patch sets it during cxx_mark_addressable. I'm wondering how we can get away with not marking other DECL_VALUE_EXPR cases, but couldn't come up with a testcase where it would be needed (e.g. for structured bindings, etc.). So just doing it for anon union vars seems safer to me at least for now. 2026-04-15 Jakub Jelinek <[email protected]> PR c++/124850 * typeck.cc (cxx_mark_addressable): For DECL_ANON_UNION_VAR_P vars also mark their DECL_VALUE_EXPR. * g++.dg/other/anon-union8.C: New test. Reviewed-by: Jason Merrill <[email protected]> (cherry picked from commit 8315b21457d740336295b58d04f0497329e94a67) Diff: --- gcc/cp/typeck.cc | 4 ++++ gcc/testsuite/g++.dg/other/anon-union8.C | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index aaff80575fc0..ef41bfe899a6 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -8076,6 +8076,10 @@ cxx_mark_addressable (tree exp, bool array_ref_p) || DECL_IN_AGGR_P (x) == 0 || TREE_STATIC (x) || DECL_EXTERNAL (x)); + if (VAR_P (x) + && DECL_ANON_UNION_VAR_P (x) + && !TREE_ADDRESSABLE (x)) + cxx_mark_addressable (DECL_VALUE_EXPR (x)); /* Fall through. */ case RESULT_DECL: diff --git a/gcc/testsuite/g++.dg/other/anon-union8.C b/gcc/testsuite/g++.dg/other/anon-union8.C new file mode 100644 index 000000000000..d6a782691c5b --- /dev/null +++ b/gcc/testsuite/g++.dg/other/anon-union8.C @@ -0,0 +1,11 @@ +// PR c++/124850 +// { dg-do compile } + +static union { int i; }; +int &r = i; + +int +foo () +{ + return r; +}
