From patchwork Wed Jul 26 16:57:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 126499 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a985:0:b0:3e4:2afc:c1 with SMTP id t5csp534369vqo; Wed, 26 Jul 2023 09:59:21 -0700 (PDT) X-Google-Smtp-Source: APBJJlHt/dNQFdUnuJx1htWe+eniQAhfb2Tn1ZROUybV+TpslN/OjlvnuawXXEo2S8szaOIL0E2N X-Received: by 2002:a17:906:3f12:b0:982:b920:daad with SMTP id c18-20020a1709063f1200b00982b920daadmr2554933ejj.71.1690390761326; Wed, 26 Jul 2023 09:59:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690390761; cv=none; d=google.com; s=arc-20160816; b=ormgBdKypTTxKcLDP+ZuOeB+6pW6LQDi4TpFSuyCJUqQKezLIcFc8qnRvLiY3y6aEk Gw/W6V+ZrOyBm8ZpByUTo/8aMkYaZsPhnbL3n6PO0Wb/9fpdu11H1WjHQ+Hk12FClXfx tMLr6CR9x2XNaZ8NjtPqfzPU9Tfp7mXLhPoDTScmFWxdpQIp/Z/Sr0UhbrvQAQZluDcy dJiAxDufr/3V8IIeYT2YGjqW6PzN03RlmTPfRV5aGC+GyTrqr2Klym7IbixQSjN7EtXD urNK6zZFqBCm6aP7rbw8cRoCY3I3n5pQsCcLphyNQCtYBXnPl3liOJtInaoohUuKd10E MU9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=eVONlAVSNYRLWUnSPQ8ZEY2Qb0IEKKULAQw61G5O9bU=; fh=M5GO8duY/t9+1sJXvBSRQ4u8k9z8sLFDsSO1kARbltQ=; b=ktoeVu9AYuFPZWvnxPEg89w1f6CsTtizqhqutFuY9o0z+IqFS4mzgoyrq23wYGLgfI g9tAtdWooEWdy3aX/lmp04eYVj0Inq/HpLVUiHs8dI8CNCJEbdn8YXSmFEjC9t74yyxp KAku85ihNVJCexW3GNcrmZqdbof1rc5IUsRghMfP546KqSptVeT0muTy5B8e9VNM6JY4 /B/KZg2tFyFfFlpSOYf1ksT0WuuVxlzDjBM1qGjb+fLHCJVAeZsn7eeLErEAt1QuUC2Z UrYmikvcDt+bGmRFecRRBgkbHS9MjPu7z8j+QRIEnM91BgBVmv3VoUEtXNgSyCQQQTOV mang== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=NOLYKxEI; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id g1-20020a1709061c8100b0099bc58ab34csi1266293ejh.135.2023.07.26.09.59.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jul 2023 09:59:21 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=NOLYKxEI; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E14E93856090 for ; Wed, 26 Jul 2023 16:58:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E14E93856090 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1690390736; bh=eVONlAVSNYRLWUnSPQ8ZEY2Qb0IEKKULAQw61G5O9bU=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=NOLYKxEIuy4tZNRpJLwiV84IDbawgj7BBY3icsl5alwDPql3pb7YcJ66yHEAaH1e6 dq8Q5Gs3reQsoH0MwME7YBb8GUfX/EMdEWYGKB3+NaSl+1Inz6py+aWtOWsorzROsm eQpCMRyrxP6RNc/tu00AXouSvRWiAZnclpkdEHw0= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 4CDA23858C60 for ; Wed, 26 Jul 2023 16:58:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4CDA23858C60 Received: from mail-ua1-f72.google.com (mail-ua1-f72.google.com [209.85.222.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-516-1xH-ck5XN_GORRlqQZRRrw-1; Wed, 26 Jul 2023 12:57:49 -0400 X-MC-Unique: 1xH-ck5XN_GORRlqQZRRrw-1 Received: by mail-ua1-f72.google.com with SMTP id a1e0cc1a2514c-7999ef0f37fso24236241.0 for ; Wed, 26 Jul 2023 09:57:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690390669; x=1690995469; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=eVONlAVSNYRLWUnSPQ8ZEY2Qb0IEKKULAQw61G5O9bU=; b=gXO5BwAn2czqGp69x5xaJSjB715qxZTw8Vm2+Bnre1LX2G/dmcHx+Y0JTkBDH2clWr A61qExstl8k4ukrjosvs597u0gzzxNayYkT6wZSow6kfvS+8cWGRRoBcGoE/uTC9EPb8 3ioHqjceM5C2IVR/BtYMJIiN13HA724VK4fGpzoF/lJsF1yX4aeufN/2GGEyS6FBhlqL /Wwk8OZqVUoU29V4dfDIuVntFnC9oUbo6uudfpoXsD7gGSAAAvaUsf06PiUzodKU4W1v 3LhtjyNHqH7k0jZpLkPlQN02BfhasUFVJs0taaNk8R8YhGB5ajwiec5+u44wrd1u1VIC 8kzg== X-Gm-Message-State: ABy/qLY35smIaR5KRL9/Tv/VOoSCTuf/Y5ph2vLZYqZCFQqUo1ZK9mut g+77oEUtCsK+fsn9NhWBaa23hsMvp9nMNDqiOzrD/qWQeFomlnUG6e7mKMR+dbuhFE8/DZyj/Rk UsHbKPe3e+ngA6XUMj16+2vr2EklG0Z5Hp8vGBdjKcMXutzUrsRn65k/OBY7tJjhvPoLmaeHud2 4= X-Received: by 2002:a05:6102:3169:b0:43b:2fa9:eb3a with SMTP id l9-20020a056102316900b0043b2fa9eb3amr1556453vsm.9.1690390668864; Wed, 26 Jul 2023 09:57:48 -0700 (PDT) X-Received: by 2002:a05:6102:3169:b0:43b:2fa9:eb3a with SMTP id l9-20020a056102316900b0043b2fa9eb3amr1556439vsm.9.1690390668559; Wed, 26 Jul 2023 09:57:48 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id e17-20020a0ce3d1000000b006301d3caba8sm5202981qvl.50.2023.07.26.09.57.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jul 2023 09:57:48 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: constexpr empty subobject confusion [PR110197] Date: Wed, 26 Jul 2023 12:57:34 -0400 Message-ID: <20230726165735.2058945-1-ppalka@redhat.com> X-Mailer: git-send-email 2.41.0.450.ga80be15292 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.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_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772503182938511748 X-GMAIL-MSGID: 1772503182938511748 Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk/13 (later)? -- >8 -- Now that init_subob_ctx no longer sets new_ctx.ctor for a subobject of empty type, it seems we need to ensure its callers cxx_eval_bare_aggregate and cxx_eval_vec_init_1 consistently omit entries for such subobjects in the parent ctx->ctor. We also need to allow cxx_eval_array_reference to synthesize an empty element object even if the array CONSTRUCTOR has CONSTRUCTOR_NO_CLEARING set. PR c++/110197 gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_array_reference): Return a synthesized empty subobject even if CONSTRUCTOR_NO_CLEARING is set. (cxx_eval_bare_aggregate): Set 'no_slot' to true more generally whenever new_ctx.ctor is empty, i.e. for any subobject of empty type. (cxx_eval_vec_init_1): Define 'no_slot' as above and use it accordingly. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-empty18.C: New test. * g++.dg/cpp0x/constexpr-empty19.C: New test. --- gcc/cp/constexpr.cc | 23 +++++++++++++------ .../g++.dg/cpp0x/constexpr-empty18.C | 7 ++++++ .../g++.dg/cpp0x/constexpr-empty19.C | 12 ++++++++++ 3 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-empty18.C create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-empty19.C diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index f2fcb54626d..da2c3116810 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -4297,6 +4297,9 @@ cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, /* Not found. */ + if (is_really_empty_class (elem_type, /*ignore_vptr*/false)) + return build_constructor (elem_type, NULL); + if (TREE_CODE (ary) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (ary)) { @@ -4314,9 +4317,7 @@ cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */ tree val; constexpr_ctx new_ctx; - if (is_really_empty_class (elem_type, /*ignore_vptr*/false)) - return build_constructor (elem_type, NULL); - else if (CP_AGGREGATE_TYPE_P (elem_type)) + if (CP_AGGREGATE_TYPE_P (elem_type)) { tree empty_ctor = build_constructor (init_list_type_node, NULL); val = digest_init (elem_type, empty_ctor, tf_warning_or_error); @@ -5095,9 +5096,9 @@ cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t, FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value) { tree orig_value = value; - /* Like in cxx_eval_store_expression, omit entries for empty fields. */ - bool no_slot = TREE_CODE (type) == RECORD_TYPE && is_empty_field (index); init_subob_ctx (ctx, new_ctx, index, value); + /* Like in cxx_eval_store_expression, omit entries for empty fields. */ + bool no_slot = new_ctx.ctor == NULL_TREE; int pos_hint = -1; if (new_ctx.ctor != ctx->ctor && !no_slot) { @@ -5261,7 +5262,8 @@ cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, bool reuse = false; constexpr_ctx new_ctx; init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype); - if (new_ctx.ctor != ctx->ctor) + bool no_slot = new_ctx.ctor == NULL_TREE; + if (new_ctx.ctor != ctx->ctor && !no_slot) { if (zeroed_out) CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false; @@ -5306,7 +5308,14 @@ cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, } if (*non_constant_p) break; - if (new_ctx.ctor != ctx->ctor) + if (no_slot) + { + /* This is an initializer for an empty subobject; now that we've + checked that it's constant, we can ignore it. */ + gcc_checking_assert (i == 0); + break; + } + else if (new_ctx.ctor != ctx->ctor) { /* We appended this element above; update the value. */ gcc_assert ((*p)->last().index == idx); diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-empty18.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-empty18.C new file mode 100644 index 00000000000..4bb9e3dcb64 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-empty18.C @@ -0,0 +1,7 @@ +// PR c++/110197 +// { dg-do compile { target c++11 } } + +struct A { constexpr A(int) { } }; +struct B { A a; }; +constexpr B f(int n) { return B{A{n}}; } +constexpr B b = f(1); diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-empty19.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-empty19.C new file mode 100644 index 00000000000..5ad67682c5b --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-empty19.C @@ -0,0 +1,12 @@ +// PR c++/110197 +// { dg-do compile { target c++11 } } + +struct A { + constexpr A() : A(__builtin_is_constant_evaluated()) { } + constexpr A(int) { } +}; +constexpr A a1[1] = {{}}; +constexpr A a2[2] = {{}, {}}; +constexpr A a3[3] = {{}, {}, {}}; +constexpr A a4[4] = {}; +constexpr A a5[5] = {};