From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1734) id D3E463858D20; Fri, 3 Feb 2023 18:58:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D3E463858D20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675450685; bh=StLgvCmYuDn5RjSgFxpUU+WVXf/EqN5QsC1TdoX6g8Q=; h=From:To:Subject:Date:From; b=N54dVRGE13At7PcM6vn81E+0mnC7c7QBdZjJKvjhOXwfc+Ce4bEuLBiu/PebfDd1n iKIVryZ+NSJN1QIeKVNFxNumu6gfjkaeQ/ApkJrhs1wmaU/l+lao8NI67QP4yZjMJN ystJBpiNuZX0jLqTAIyTZClfSfYtPTF6Bj/Scn1o= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Marek Polacek To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-5693] c++: wrong error with constexpr array and value-init [PR108158] X-Act-Checkin: gcc X-Git-Author: Marek Polacek X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 60fca1802a25034f49fa1e3769b3a5656f392e89 X-Git-Newrev: 27ac6a707e7438c3cec79c24f5d53de79493e2f8 Message-Id: <20230203185805.D3E463858D20@sourceware.org> Date: Fri, 3 Feb 2023 18:58:05 +0000 (GMT) List-Id: https://gcc.gnu.org/g:27ac6a707e7438c3cec79c24f5d53de79493e2f8 commit r13-5693-g27ac6a707e7438c3cec79c24f5d53de79493e2f8 Author: Marek Polacek Date: Mon Jan 30 09:02:00 2023 -0500 c++: wrong error with constexpr array and value-init [PR108158] In this test case, we find ourselves evaluating 't' which is ((const struct carray *) this)->data_[VIEW_CONVERT_EXPR(index)] in cxx_eval_array_reference. ctx->object is non-null, a RESULT_DECL, so we replace it with 't': new_ctx.object = t; // result_decl replaced and then we go to cxx_eval_constant_expression to evaluate an AGGR_INIT_EXPR, where we end up evaluating an INIT_EXPR (which is in the body of the constructor for seed_or_index): ((struct seed_or_index *) this)->value_ = NON_LVALUE_EXPR <0> whereupon in cxx_eval_store_expression we go to the probe loop where the 'this' is evaluated to ze_set.tables_.first_table_.data_[0] so the 'object' is ze_set, but that isn't in ctx->global->get_value_ptr so we fail with a bogus error. ze_set is not there because it comes from a different constexpr context (it's not in cv_cache either). The problem started with r12-2304 where I added the new_ctx.object replacement. That was to prevent a type mismatch: the type of 't' and ctx.object were different. It seems clear that we shouldn't have replaced ctx.object here. The cxx_eval_array_reference I mentioned earlier is called from cxx_eval_store_expression: 6257 init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue, 6258 non_constant_p, overflow_p); which already created a new context, whose .object we should be using unless, for instance, INIT contained a.b and we're evaluating the 'a' part, which I think was the case for r12-2304; in that case ctx.object has to be something different. It no longer seems necessary to replace new_ctx.object (likely due to changes in empty class handling). PR c++/108158 gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_array_reference): Don't replace new_ctx.object. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/constexpr-108158.C: New test. Diff: --- gcc/cp/constexpr.cc | 4 ---- gcc/testsuite/g++.dg/cpp1y/constexpr-108158.C | 32 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 5b31f9c27d1..564766c8a00 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -4301,10 +4301,6 @@ cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, if (!SCALAR_TYPE_P (elem_type)) { new_ctx = *ctx; - if (ctx->object) - /* If there was no object, don't add one: it could confuse us - into thinking we're modifying a const object. */ - new_ctx.object = t; new_ctx.ctor = build_constructor (elem_type, NULL); ctx = &new_ctx; } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-108158.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-108158.C new file mode 100644 index 00000000000..e5f5e9954e5 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-108158.C @@ -0,0 +1,32 @@ +// PR c++/108158 +// { dg-do compile { target c++14 } } + +template struct carray { + T data_[N]{}; + constexpr T operator[](long index) const { return data_[index]; } +}; +struct seed_or_index { +private: + long value_ = 0; +}; +template struct pmh_tables { + carray first_table_; + template + constexpr void lookup(KeyType, HasherType) const { + first_table_[0]; + } +}; +template struct unordered_set { + int equal_; + carray keys_; + pmh_tables tables_; + constexpr unordered_set() : equal_{} {} + template + constexpr auto lookup(KeyType key, Hasher hash) const { + tables_.lookup(key, hash); + return keys_; + } +}; +constexpr unordered_set<3> ze_set; +constexpr auto nocount = ze_set.lookup(4, int()); +constexpr auto nocount2 = unordered_set<3>{}.lookup(4, int());