From patchwork Sat Apr 9 03:27:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 52746 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 17A57385782C for ; Sat, 9 Apr 2022 03:28:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 17A57385782C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1649474888; bh=FC3d38y7Vn/2yrmK0/4+dgP5Sew1q425KQ86o3PTrvg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=ZYVDZx3Oiy2Ibbf+v+Gxo1bJACpvsa7JPmyCkCn3iztsR/pNYDlcxrQh27cGmG+Ij ZGtLdNRHOCggFZ3xQPZWN9YEb61xtFkOMsjFxLB3mMTvPe+fQi1KtSEXgK/6kxFx7t tlPXmGP9s/wwWPJTy25hFbXU4QbJfpZKo6hjR8Uc= 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 4593C385803D for ; Sat, 9 Apr 2022 03:27:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4593C385803D Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-616-o8xMNbf7O_SB4txjQbi0-Q-1; Fri, 08 Apr 2022 23:27:34 -0400 X-MC-Unique: o8xMNbf7O_SB4txjQbi0-Q-1 Received: by mail-qv1-f72.google.com with SMTP id jt1-20020a05621427e100b004442a992b37so2101499qvb.0 for ; Fri, 08 Apr 2022 20:27:34 -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:subject:date:message-id:mime-version :content-transfer-encoding; bh=FC3d38y7Vn/2yrmK0/4+dgP5Sew1q425KQ86o3PTrvg=; b=joQ8/VzeLEagHFMQQwHVr+3YgKIZ1Wg48E77mld5p9z7/9Dczi/CdEYlDli+AyABAK CQ99AVJd3D5aYofgg+M7/ZMrSAmf3+dYewhTLaqwvJ5HBQXe6E4/qIS7qrS+GB83e/Q5 nafeFkiDfFGUS60fqB4FjQJubB8+/wDCCgUuRbqKf/HGfIvJIpzh7lmaJ98sxsdfxP+/ ZeFpieVtY2zV9CheT32CWBDRp8U5wxJmSigyAkeKv6YhbmaM1hAT+rQznwGk5EZS9Mxm 7+6EX4xMDkGsOhSJ3lI/bjNPgk7A3kfPxBRQktENhRgcSQPewAyq8r6Gqvu6TvpYjz65 UDpQ== X-Gm-Message-State: AOAM533LaTBBI3kJgRSxGW3k6L797w5UVMiICfNLc6YOzZujV3pJfnIr Oh30e1hBNkckrFcPBIKnxkvHF7DxGtD5sB/Blb+z3VW8NJBL96JW2jvkE+XhK4oUQuXS/0opyoO PL8LrA2ujlgdfPsDJgCD+seVEAc634/iP3bsqhW3jbHwZNn5g6RuJOXWYWN6AaMVYIw== X-Received: by 2002:a05:620a:4092:b0:67b:31a3:881b with SMTP id f18-20020a05620a409200b0067b31a3881bmr15008840qko.710.1649474853437; Fri, 08 Apr 2022 20:27:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz4NCPNql9R1Bkl7WNfeuZi1EP/i/NtsejN+PpuMsFam8HgHjqJoLtEr62PPccHnyob3BvK7A== X-Received: by 2002:a05:620a:4092:b0:67b:31a3:881b with SMTP id f18-20020a05620a409200b0067b31a3881bmr15008826qko.710.1649474853023; Fri, 08 Apr 2022 20:27:33 -0700 (PDT) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id l8-20020a05622a174800b002e1e3f7d4easm20757067qtk.86.2022.04.08.20.27.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Apr 2022 20:27:31 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: constexpr non-trivial aggregate init [PR105191] Date: Fri, 8 Apr 2022 23:27:29 -0400 Message-Id: <20220409032729.2705255-1-jason@redhat.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.0 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, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" My patch for PR92385 made us use VEC_INIT_EXPR for aggregate initialization of an array where some elements are not explicitly initialized. Constexpr handling of that was treating initialization from {} as equivalent to value-initialization, which is problematic for classes with default member initializers that make the default constructor non-trivial; in older standard modes, not initializing all members makes a constructor non-constexpr, but aggregate initialization is fine. Tested x86_64-pc-linux-gnu, applying to trunk. PR c++/105191 PR c++/92385 gcc/cp/ChangeLog: * tree.cc (build_vec_init_elt): Do {}-init for aggregates. * constexpr.cc (cxx_eval_vec_init): Only treat {} as value-init for non-aggregate types. (build_vec_init_expr): Also check constancy of explicit initializer elements. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-array28.C: New test. --- gcc/cp/constexpr.cc | 3 ++- gcc/cp/tree.cc | 27 +++++++++++++++---- .../g++.dg/cpp0x/constexpr-array28.C | 21 +++++++++++++++ 3 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-array28.C base-commit: 58586721c79f77224b8571a5dba732620d5546ab diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 9c40b051574..db78b4a6545 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -5008,7 +5008,8 @@ cxx_eval_vec_init (const constexpr_ctx *ctx, tree t, bool value_init = VEC_INIT_EXPR_VALUE_INIT (t); if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init)) ; - else if (CONSTRUCTOR_NELTS (init) == 0) + else if (CONSTRUCTOR_NELTS (init) == 0 + && !CP_AGGREGATE_TYPE_P (strip_array_types (atype))) { /* Handle {} as value-init. */ init = NULL_TREE; diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index 780a8d89165..63164bee638 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -740,7 +740,7 @@ build_cplus_new (tree type, tree init, tsubst_flags_t complain) constructor calls until gimplification time; now we only do it to set VEC_INIT_EXPR_IS_CONSTEXPR. - We assume that init is either NULL_TREE, void_type_node (indicating + We assume that init is either NULL_TREE, {}, void_type_node (indicating value-initialization), or another array to copy. */ static tree @@ -752,7 +752,20 @@ build_vec_init_elt (tree type, tree init, tsubst_flags_t complain) || !CLASS_TYPE_P (inner_type)) /* No interesting initialization to do. */ return integer_zero_node; - else if (init == void_type_node) + if (init && BRACE_ENCLOSED_INITIALIZER_P (init)) + { + /* Even if init has initializers for some array elements, + we're interested in the {}-init of trailing elements. */ + if (CP_AGGREGATE_TYPE_P (inner_type)) + { + tree empty = build_constructor (init_list_type_node, nullptr); + return digest_init (inner_type, empty, complain); + } + else + /* It's equivalent to value-init. */ + init = void_type_node; + } + if (init == void_type_node) return build_value_init (inner_type, complain); releasing_vec argvec; @@ -808,9 +821,13 @@ build_vec_init_expr (tree type, tree init, tsubst_flags_t complain) TREE_SIDE_EFFECTS (init) = true; SET_EXPR_LOCATION (init, input_location); - if (cxx_dialect >= cxx11 - && potential_constant_expression (elt_init)) - VEC_INIT_EXPR_IS_CONSTEXPR (init) = true; + if (cxx_dialect >= cxx11) + { + bool cx = potential_constant_expression (elt_init); + if (BRACE_ENCLOSED_INITIALIZER_P (init)) + cx &= potential_constant_expression (init); + VEC_INIT_EXPR_IS_CONSTEXPR (init) = cx; + } VEC_INIT_EXPR_VALUE_INIT (init) = value_init; return init; diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-array28.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-array28.C new file mode 100644 index 00000000000..d7706b9f0b4 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-array28.C @@ -0,0 +1,21 @@ +// PR c++/105191 +// { dg-do compile { target c++11 } } + +struct A { + const char* message = ""; +}; + +enum class B { }; + +struct C { + A a; + B b; +}; + +struct D { + C cs[1]; +}; + +constexpr D ds[4] = { + D{}, +};