From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id C2A13385803E for ; Fri, 6 May 2022 18:00:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C2A13385803E Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-541-T6Jib2mwM7a_z5-Ir84KHA-1; Fri, 06 May 2022 14:00:27 -0400 X-MC-Unique: T6Jib2mwM7a_z5-Ir84KHA-1 Received: by mail-qt1-f199.google.com with SMTP id cn8-20020a05622a248800b002f3c7be2744so1836042qtb.17 for ; Fri, 06 May 2022 11:00:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:date:to:cc:subject:in-reply-to:message-id :references:mime-version; bh=t8A6DU+b+ILT5tNawB5Gkq5iFJBfyhBMLhtJp2dB5wk=; b=r+3w2PNxZ6fXM9R+9O25Tj0VpRFlg0Tj996hYb8eVaIFWVA7IHbWU+0mp6DrXaYGun /r0nQdHF/5Te6mJdcyaii2gJLEV41RXP7YHnF7+9LOxEgjCgUnJxXy7opN8LhS1IoWl/ aamu4o8YPnMLty3xR9VbGFzF/b0EbXxJIzo0ptCHPFUELBvbrh+JwSSyuEBJaHoEtarD +WfuVVmlDcBx/WKIL26+xAduLyX2qiSsKDmn7a7CHQ0hWjgHdl8kx9r/LOSCz8UIz22V JO4TmvVcaTEOYc0FkxlJ77u/brDMvRAEXxrej7v8ANHk4liZ5K2lTbp2GskL75XFXpMN i6Ag== X-Gm-Message-State: AOAM530fBUBVQfLXeriNqhcFTIh3mCzuiH/G338FV+8vAexEc6NixO1/ XyUQHOH1R7wpqQq+aRjOfDhEKDKmzNH2zs8x1YoL9DedmkH3rt6S5yCX0+Tk9pJh7kpWEwtjodS j6dqKdd1Md5B/KsIeCQ== X-Received: by 2002:a05:622a:c2:b0:2f3:c5be:b0b5 with SMTP id p2-20020a05622a00c200b002f3c5beb0b5mr3985468qtw.410.1651860026254; Fri, 06 May 2022 11:00:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyec6p4t7BPd1e7H+EeQPdIiI9ur2uysjpa8xi0fbWErAolP5fKQU0LCW7pezJ3nEV1QIw92A== X-Received: by 2002:a05:622a:c2:b0:2f3:c5be:b0b5 with SMTP id p2-20020a05622a00c200b002f3c5beb0b5mr3985435qtw.410.1651860025902; Fri, 06 May 2022 11:00:25 -0700 (PDT) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id t31-20020a05622a181f00b002f39b99f66asm3180221qtc.4.2022.05.06.11.00.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 May 2022 11:00:25 -0700 (PDT) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Fri, 6 May 2022 14:00:24 -0400 (EDT) To: Patrick Palka cc: Jason Merrill , gcc-patches@gcc.gnu.org Subject: Re: [PATCH] c++: constexpr init of union sub-aggr w/ base [PR105491] In-Reply-To: <7ab4b646-bade-97b0-8083-c392cf37ea49@idea> Message-ID: <48baf04b-9cb9-8668-cbb6-047eba1033d7@idea> References: <20220506152232.2843007-1-ppalka@redhat.com> <659fd0c4-5fa7-147d-9b97-bebf06ae7c23@redhat.com> <7ab4b646-bade-97b0-8083-c392cf37ea49@idea> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-14.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 06 May 2022 18:00:34 -0000 On Fri, 6 May 2022, Patrick Palka wrote: > On Fri, 6 May 2022, Jason Merrill wrote: > > > On 5/6/22 11:22, Patrick Palka wrote: > > > Here ever since r10-7313-gb599bf9d6d1e18, reduced_constant_expression_p > > > in C++11/14 is rejecting the marked sub-aggregate initializer (of type S) > > > > > > W w = {.D.2445={.s={.D.2387={.m=0}, .b=0}}} > > > ^ > > > > > > ultimately because said initializer has CONSTRUCTOR_NO_CLEARING set, and > > > so the function proceeds to verify that all fields of S are initialized. > > > And before C++17 we don't expect to see base class fields (since > > > next_initializable_field skips over the), so the base class initializer > > > causes r_c_e_p to return false. > > > > That seems like the primary bug. I guess r_c_e_p shouldn't be using > > next_initializable_field. Really that function should only be used for > > aggregates. > > I see, I'll try replacing it in r_c_e_p. Would that be in addition to > or instead of the clear_no_implicit_zero approach? I'm testing the following, which uses a custom predicate instead of next_initializable_field in r_c_e_p. -- >8 -- gcc/cp/ChangeLog: * constexpr.cc (reduced_constant_expression_p): Replace use of next_initializable_field with custom predicate. Refactor to remove the use of goto. * decl.cc (next_initializable_field): Skip over vptr fields. Document that this function is to be used only for aggregate classes. --- gcc/cp/constexpr.cc | 65 ++++++++++++++++++++++++++------------------- gcc/cp/decl.cc | 15 +++++------ 2 files changed, 44 insertions(+), 36 deletions(-) diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 9b1e71857fc..d1cd556591c 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -3016,7 +3016,6 @@ reduced_constant_expression_p (tree t) case CONSTRUCTOR: /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */ - tree field; if (CONSTRUCTOR_NO_CLEARING (t)) { if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE) @@ -3041,42 +3040,54 @@ reduced_constant_expression_p (tree t) } if (find_array_ctor_elt (t, max) == -1) return false; - goto ok; } - else if (cxx_dialect >= cxx20 - && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE) + else if (TREE_CODE (TREE_TYPE (t)) == UNION_TYPE) { - if (CONSTRUCTOR_NELTS (t) == 0) + if (CONSTRUCTOR_NELTS (t) != 1) /* An initialized union has a constructor element. */ return false; - /* And it only initializes one member. */ - field = NULL_TREE; + if (!reduced_constant_expression_p (CONSTRUCTOR_ELT (t, 0)->value)) + return false; } else - field = next_initializable_field (TYPE_FIELDS (TREE_TYPE (t))); + { + /* Similar to the predicate used in next_initializable_field, + except that we additionally skip over empty types (for which + we don't require an initializer), and we always consider base + class fields (not just in C++17 mode) and vptr fields. */ + auto ignorable_field_p = [] (tree field) { + if (!field) + return false; + return (TREE_CODE (field) != FIELD_DECL + || DECL_UNNAMED_BIT_FIELD (field) + || (DECL_ARTIFICIAL (field) + && !DECL_FIELD_IS_BASE (field) + && !DECL_VIRTUAL_P (field)) + || is_really_empty_class (TREE_TYPE (field), + /*ignore_vptr*/false)); + }; + tree field = TYPE_FIELDS (TREE_TYPE (t)); + for (auto &e: CONSTRUCTOR_ELTS (t)) + { + if (!reduced_constant_expression_p (e.value)) + return false; + while (e.index != field && ignorable_field_p (field)) + field = DECL_CHAIN (field); + if (e.index == field) + field = DECL_CHAIN (field); + else + return false; + } + while (ignorable_field_p (field)) + field = DECL_CHAIN (field); + if (field) + return false; + } } else - field = NULL_TREE; - for (auto &e: CONSTRUCTOR_ELTS (t)) - { - /* If VAL is null, we're in the middle of initializing this - element. */ + for (auto &e: CONSTRUCTOR_ELTS (t)) if (!reduced_constant_expression_p (e.value)) return false; - /* Empty class field may or may not have an initializer. */ - for (; field && e.index != field; - field = next_initializable_field (DECL_CHAIN (field))) - if (!is_really_empty_class (TREE_TYPE (field), - /*ignore_vptr*/false)) - return false; - if (field) - field = next_initializable_field (DECL_CHAIN (field)); - } - /* There could be a non-empty field at the end. */ - for (; field; field = next_initializable_field (DECL_CHAIN (field))) - if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false)) - return false; -ok: if (CONSTRUCTOR_NO_CLEARING (t)) /* All the fields are initialized. */ CONSTRUCTOR_NO_CLEARING (t) = false; diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index c9110db796a..c564e5e596d 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -6382,10 +6382,10 @@ struct reshape_iter static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t); -/* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value - returned is the next FIELD_DECL (possibly FIELD itself) that can be - initialized. If there are no more such fields, the return value - will be NULL. */ +/* FIELD is an element of TYPE_FIELDS of an aggregate class, or NULL. In the + former case, the value returned is the next FIELD_DECL (possibly FIELD + itself) that can be initialized. If there are no more such fields, the + return value will be NULL. */ tree next_initializable_field (tree field) @@ -6394,11 +6394,8 @@ next_initializable_field (tree field) && (TREE_CODE (field) != FIELD_DECL || DECL_UNNAMED_BIT_FIELD (field) || (DECL_ARTIFICIAL (field) - /* In C++17, don't skip base class fields. */ - && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)) - /* Don't skip vptr fields. We might see them when we're - called from reduced_constant_expression_p. */ - && !DECL_VIRTUAL_P (field)))) + /* In C++17, aggregates can have base classes. */ + && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))))) field = DECL_CHAIN (field); return field; -- 2.36.0.63.gf5aaf72f1b