From patchwork Fri May 6 15:22:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 53556 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 64455388883C for ; Fri, 6 May 2022 15:23:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 64455388883C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1651850592; bh=flXGKxEVYLAkyYyBcbWfoOL3Nu6smtzi4n1mr99AEmE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=ob/vCKFZjJfRQDzfevTe1ktTElFdA/VLkxYtmPe0wDEzF97KXmNjHaFxUVXsoXAdY Xy7LUxbGu6zD6/NQ7DgYBvxfbeTtvRXeZTgbjFe1CXHAlHKdaRZPBykVHtBCuEECgY KmS4s5DU6GORMk5G+3emhcZzAZLLY9Qsfc3Dz8ms= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 48FCA385DC05 for ; Fri, 6 May 2022 15:22:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 48FCA385DC05 Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-649-hS0mc64gN2KAUt4I_Lzy2w-1; Fri, 06 May 2022 11:22:39 -0400 X-MC-Unique: hS0mc64gN2KAUt4I_Lzy2w-1 Received: by mail-qt1-f197.google.com with SMTP id d8-20020a05622a05c800b002f3bb450e94so4661660qtb.2 for ; Fri, 06 May 2022 08:22:39 -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:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=flXGKxEVYLAkyYyBcbWfoOL3Nu6smtzi4n1mr99AEmE=; b=WrSLNb6BLd3E6JXlSjBxbaZjLX0dvdoZ4Ftf3BRuBd4s2ffdRWSHFnm4kzo5g6p8Bj QvdqwWvLQnBAWpMnlWul/dSIKjtyqylf6Sg67sh8X6JuIRJruDymlZ2bHkEqIK+J3V/p tfjU+RbTA5uaW1D1Tv6AIaG300M1P8cODO/bBwjrtulZTVwB/CJ10ZnTBd6BBcE223zB 6IWSV8S4zGK22UV9WbRN6x1Dp5zppsemmaKjUBd2Y1O3IoNsLCcaSxeyF1cbJ3SOQf4W vWoBU5UcYzzP/v/rqiiFvBUVEZbJS6oBiy094NRBKmRJcaIUc1Oc1TiYyiOUByDdm8KN 38Jw== X-Gm-Message-State: AOAM532rn2uLTtWXcZNL+nJG5Gv5CyeY1MKfeFU0Xtqgck146r4bvE+Y RgjsQfljSBsrwSfZ8RHHYEL+iAOt9i6JCLSxbALjWAPI7MDY0gMFurW7JoSGRQMEThedZ6ytWir jaKRO6D65IHvw6UnKQJIaNyUyVjZSjlHQ3CEG3R0WCuT9loT/7Fo+hT0cK6ckSSpSkL4= X-Received: by 2002:a0c:ab02:0:b0:443:a395:cc1c with SMTP id h2-20020a0cab02000000b00443a395cc1cmr2930930qvb.72.1651850558634; Fri, 06 May 2022 08:22:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy2aHt/fZe/i3uVY6VsgzvnVHKomhGNMYSaK+q1/Nkg4Q+xpwOtHf6PyEfVu1dVqi6kdE1wyg== X-Received: by 2002:a0c:ab02:0:b0:443:a395:cc1c with SMTP id h2-20020a0cab02000000b00443a395cc1cmr2930903qvb.72.1651850558311; Fri, 06 May 2022 08:22:38 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id i22-20020ae9ee16000000b0069fc13ce1e5sm2536684qkg.22.2022.05.06.08.22.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 May 2022 08:22:38 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: constexpr init of union sub-aggr w/ base [PR105491] Date: Fri, 6 May 2022 11:22:32 -0400 Message-Id: <20220506152232.2843007-1-ppalka@redhat.com> X-Mailer: git-send-email 2.36.0.63.gf5aaf72f1b MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.7 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-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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. The base class initializer comes from the constructor call S::S(int). The reason this is caused by r10-7313-gb599bf9d6d1e18 is because in that commit we began using CONSTRUCTOR_NO_CLEARING to also track whether we're in middle of activating a union member. This overloaded use of the flag affects clear_no_implicit_zero, which recurses into sub-aggregate initializers only if the outer initializer has CONSTRUCTOR_NO_CLEARING set. After that commit, the outer union initializer above no longer has the flag set at this point and so clear_no_implicit_zero no longer clears CONSTRUCTOR_NO_CLEARING for the marked inner initializer. This patch fixes this by restoring the recursive behavior of clear_no_implicit_zero for union initializers. Arguably we should we could improve reduced_constant_expression_p to accept the marked initializer in C++11/14 even if it has CONSTRUCTOR_NO_CLEARING set, but adjusting clear_no_implicit_zero seems safer to backport. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk/12/11/10? PR c++/105491 gcc/cp/ChangeLog: * constexpr.cc (clear_no_implicit_zero): Recurse into a union initializer even if CONSTRUCTOR_NO_CLEARING is already cleared. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-union7.C: New test. * g++.dg/cpp0x/constexpr-union7a.C: New test. * g++.dg/cpp2a/constinit17.C: New test. --- gcc/cp/constexpr.cc | 7 +++++- gcc/testsuite/g++.dg/cpp0x/constexpr-union7.C | 17 +++++++++++++ .../g++.dg/cpp0x/constexpr-union7a.C | 15 ++++++++++++ gcc/testsuite/g++.dg/cpp2a/constinit17.C | 24 +++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-union7.C create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-union7a.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/constinit17.C diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 9b1e71857fc..75fecbcbcb7 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -1886,7 +1886,12 @@ cxx_eval_internal_function (const constexpr_ctx *ctx, tree t, static void clear_no_implicit_zero (tree ctor) { - if (CONSTRUCTOR_NO_CLEARING (ctor)) + if (CONSTRUCTOR_NO_CLEARING (ctor) + /* For a union initializer, the flag could already be cleared but not + necessarily yet for its sub-aggregates, since for unions the flag is + also used by cxx_eval_store_expression to track whether we're in the + middle of activating one of its members. */ + || TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE) { CONSTRUCTOR_NO_CLEARING (ctor) = false; for (auto &e: CONSTRUCTOR_ELTS (ctor)) diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-union7.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-union7.C new file mode 100644 index 00000000000..b3147d9db50 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-union7.C @@ -0,0 +1,17 @@ +// PR c++/105491 +// { dg-do compile { target c++11 } } + +struct V { + int m = 0; +}; +struct S : V { + constexpr S(int) : b() { } + bool b; +}; +struct W { + constexpr W() : s(0) { } + union { + S s; + }; +}; +constexpr W w; diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-union7a.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-union7a.C new file mode 100644 index 00000000000..b676e7d1748 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-union7a.C @@ -0,0 +1,15 @@ +// PR c++/105491 +// { dg-do compile { target c++11 } } + +struct V { + int m = 0; +}; +struct S : V { + constexpr S(int) : b() { } + bool b; +}; +union W { + constexpr W() : s(0) { } + S s; +}; +constexpr W w; diff --git a/gcc/testsuite/g++.dg/cpp2a/constinit17.C b/gcc/testsuite/g++.dg/cpp2a/constinit17.C new file mode 100644 index 00000000000..6431654ac85 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/constinit17.C @@ -0,0 +1,24 @@ +// PR c++/105491 +// { dg-do compile { target c++11 } } + +class Message { + virtual int GetMetadata(); +}; +class ProtobufCFileOptions : Message { +public: + constexpr ProtobufCFileOptions(int); + bool no_generate_; + bool const_strings_; + bool use_oneof_field_name_; + bool gen_pack_helpers_; + bool gen_init_helpers_; +}; +constexpr ProtobufCFileOptions::ProtobufCFileOptions(int) + : no_generate_(), const_strings_(), use_oneof_field_name_(), + gen_pack_helpers_(), gen_init_helpers_() {} +struct ProtobufCFileOptionsDefaultTypeInternal { + constexpr ProtobufCFileOptionsDefaultTypeInternal() : _instance({}) {} + union { + ProtobufCFileOptions _instance; + }; +} __constinit _ProtobufCFileOptions_default_instance_;