From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1666) id 69D86385841A; Fri, 12 Aug 2022 08:11:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 69D86385841A MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Richard Biener To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-8684] c++: constexpr, empty base after non-empty [PR106369] X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 60fed79c1e9a968eaedf292b298d19c9475ad37a X-Git-Newrev: 6b7d570a5001bb79e34c0d1626a8c7f55386dac7 Message-Id: <20220812081115.69D86385841A@sourceware.org> Date: Fri, 12 Aug 2022 08:11:15 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 12 Aug 2022 08:11:15 -0000 https://gcc.gnu.org/g:6b7d570a5001bb79e34c0d1626a8c7f55386dac7 commit r12-8684-g6b7d570a5001bb79e34c0d1626a8c7f55386dac7 Author: Jason Merrill Date: Tue Jul 26 11:02:21 2022 -0400 c++: constexpr, empty base after non-empty [PR106369] Here the CONSTRUCTOR we were providing for D{} had an entry for the B base subobject at offset 0 following the entry for the C base, causing output_constructor_regular_field to ICE due to going backwards. It might be nice for that function to be more tolerant of empty fields, but it also seems reasonable for the front end to prune the useless entry. PR c++/106369 gcc/cp/ChangeLog: * constexpr.cc (reduced_constant_expression_p): Return false if a CONSTRUCTOR initializes an empty field. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/constexpr-lambda27.C: New test. (cherry picked from commit 9efe4e153d994974afcbba09c3c683f5f4a19c63) Diff: --- gcc/cp/constexpr.cc | 8 +++++++- gcc/testsuite/g++.dg/cpp1z/constexpr-lambda27.C | 26 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 1c17f0d14b8..28f8785dea3 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -3063,7 +3063,13 @@ reduced_constant_expression_p (tree t) element. */ if (!reduced_constant_expression_p (e.value)) return false; - /* Empty class field may or may not have an initializer. */ + /* We want to remove initializers for empty fields in a struct to + avoid confusing output_constructor. */ + if (is_empty_field (e.index) + && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE) + return false; + /* Check for non-empty fields between initialized fields when + CONSTRUCTOR_NO_CLEARING. */ for (; field && e.index != field; field = next_subobject_field (DECL_CHAIN (field))) if (!is_really_empty_class (TREE_TYPE (field), diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-lambda27.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-lambda27.C new file mode 100644 index 00000000000..24e2e9b6c98 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-lambda27.C @@ -0,0 +1,26 @@ +// PR c++/106369 +// { dg-do compile { target c++17 } } + +struct A { + int a[256]; + constexpr int &operator[] (int n) noexcept { return a[n]; } + constexpr const int &operator[] (int n) const noexcept { return a[n]; } +}; +struct B {}; +template +struct C { + constexpr T &foo (const char x) noexcept { c = T::d[x]; return static_cast(*this); } + int c; +}; +struct D : public C, public B +{ + D () noexcept = default; + static constexpr char e[9] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' }; + static constexpr A d = [] () constexpr { + A f {}; + for (int i = 0; i < 9; ++i) + f[e[i]] = 1; + return f; + } (); +}; +constexpr auto g = D{}.foo ('E');