From: Jakub Jelinek Date: Fri, 28 Feb 2025 14:22:47 +0000 (+0100) Subject: c++: Fix cxx_eval_store_expression {REAL,IMAG}PART_EXPR handling [PR119045] X-Git-Tag: basepoints/gcc-16~1797 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7eb8ec1856f71b039d1c2235b1c941934fa28e22;p=thirdparty%2Fgcc.git c++: Fix cxx_eval_store_expression {REAL,IMAG}PART_EXPR handling [PR119045] I've added the asserts that probe == target because {REAL,IMAG}PART_EXPR always implies a scalar type and so applying ARRAY_REF/COMPONENT_REF etc. on it further doesn't make sense and the later code relies on it to be the last one in refs array. But as the following testcase shows, we can fail those assertions in case there is a reference or pointer to the __real__ or __imag__ part, in that case we just evaluate the constant expression and so probe won't be the same as target. That case doesn't push anything into the refs array though. The following patch changes those asserts to verify that refs is still empty, which fixes it. 2025-02-28 Jakub Jelinek PR c++/119045 * constexpr.cc (cxx_eval_store_expression) : Assert that refs->is_empty () rather than probe == target. (cxx_eval_store_expression) : Likewise. * g++.dg/cpp1y/constexpr-complex2.C: New test. --- diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index c68666cc5dd..3cbc9c8b302 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -6415,7 +6415,7 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, break; case REALPART_EXPR: - gcc_assert (probe == target); + gcc_assert (refs->is_empty ()); vec_safe_push (refs, NULL_TREE); vec_safe_push (refs, probe); vec_safe_push (refs, TREE_TYPE (probe)); @@ -6423,7 +6423,7 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, break; case IMAGPART_EXPR: - gcc_assert (probe == target); + gcc_assert (refs->is_empty ()); vec_safe_push (refs, NULL_TREE); vec_safe_push (refs, probe); vec_safe_push (refs, TREE_TYPE (probe)); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-complex2.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-complex2.C new file mode 100644 index 00000000000..7baafd83a32 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-complex2.C @@ -0,0 +1,18 @@ +// PR c++/119045 +// { dg-do compile { target c++14 } } + +constexpr float +foo () +{ + __complex__ float f {1, 2}; + float s = __real__ f + __imag__ f; + float &r = __real__ f; + float &i = __imag__ f; + r = 42; + s += __real__ f; + i = 3; + s += __imag__ f; + return s; +} + +static_assert (foo () == 48.0f, "");