From patchwork Tue Jan 14 16:25:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 104773 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 D6A84385B519 for ; Tue, 14 Jan 2025 16:26:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D6A84385B519 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=QXY9fNko 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 ESMTP id 629893858D33 for ; Tue, 14 Jan 2025 16:26:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 629893858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 629893858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736871968; cv=none; b=ej1yO0vfWsmFnwK9u/6Fos6SdR2QUs2M/jLglYZNU0v9AlMixntvY8xkIFNalbiHIDbFmS/nh3iq+cHnJGO528HBM5dRd5SFsNTtilUKSlbhmonPqT6D6nRol0YU5Rq788gKJMf0PcPJCuyOp+QW5avnGq176xXeh7ybAO3cFgM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736871968; c=relaxed/simple; bh=BEk+Lm8RUrHYVzmD5R+lutyvUfg6u1Nh77pEiFbbv70=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=KFcodPjjpPlbf6TbQJEfKRwxWvZ4wvYu4lBnsmiBlnq5eV0f45ZpaA1UzOmXNzyfUS/eFXEc5kG8oSAxrYFR5lmZAchKbtGVHt+3hsQ0+heGP9L71Vp7dc66w/gm66S735LUygExpzqVzvVevGoJOIurPpVK5UKzbPG1zyrwggs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 629893858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736871968; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=K9QqDeykc8VJfHlthBducuMHYTGVt9G5QScDg8gwGDg=; b=QXY9fNkoI6NJOUBMxt6W44by7UIblkQ5XJwqHKb/zaV8QG4w99UD2Qo995pPgE32h96Krv AX76PIim/+rxeseEYJKNmhabxztKP98EIBTlyPwt9lzjUtwI1PkVZ15CDh8SmSQ1auX+IU z7PWAOgHLboHnOZWeOlW2+WngmYlNKo= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-605-gA_1SifbPa-j_qoXruLP8Q-1; Tue, 14 Jan 2025 11:26:06 -0500 X-MC-Unique: gA_1SifbPa-j_qoXruLP8Q-1 X-Mimecast-MFC-AGG-ID: gA_1SifbPa-j_qoXruLP8Q Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 10EA0195608A for ; Tue, 14 Jan 2025 16:26:05 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.81.140]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 71A2F19560AA; Tue, 14 Jan 2025 16:26:03 +0000 (UTC) From: Marek Polacek To: GCC Patches , Jason Merrill Subject: [PATCH] c++: re-enable NSDMI CONSTRUCTOR folding [PR118355] Date: Tue, 14 Jan 2025 11:25:58 -0500 Message-ID: <20250114162558.15438-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: fsFN-XDUCYOsWRh9DqSuLfdeQgEYZ7njDmv-peLeP84_1736871965 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.1 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, SPF_HELO_NONE, SPF_NONE, TXREP 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/branches? -- >8 -- In c++/102990 we had a problem where massage_init_elt got {}, digest_nsdmi_init turned that {} into { .value = (int) 1.0e+0 }, and we crashed in the call to fold_non_dependent_init because a FIX_TRUNC_EXPR/FLOAT_EXPR got into tsubst*. So we avoided calling fold_non_dependent_init for a CONSTRUCTOR. But that broke the following test, where we no longer fold the CONST_DECL in { .type = ZERO } to { .type = 0 } and then process_init_constructor_array does: if (next != error_mark_node && (initializer_constant_valid_p (next, TREE_TYPE (next)) != null_pointer_node)) { /* Use VEC_INIT_EXPR for non-constant initialization of trailing elements with no explicit initializers. */ picflags |= PICFLAG_VEC_INIT; because { .type = ZERO } isn't initializer_constant_valid_p. Then we create a VEC_INIT_EXPR and say we can't convert the argument. So we have to fold the elements of the CONSTRUCTOR. We just can't instantiate the elements in a template. This also fixes c++/118047. PR c++/118047 PR c++/118355 gcc/cp/ChangeLog: * typeck2.cc (massage_init_elt): Call fold_non_dependent_init unless for a CONSTRUCTOR in a template. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/nsdmi-list10.C: New test. * g++.dg/cpp0x/nsdmi-list9.C: New test. --- gcc/cp/typeck2.cc | 8 +++--- gcc/testsuite/g++.dg/cpp0x/nsdmi-list10.C | 35 +++++++++++++++++++++++ gcc/testsuite/g++.dg/cpp0x/nsdmi-list9.C | 34 ++++++++++++++++++++++ 3 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/nsdmi-list10.C create mode 100644 gcc/testsuite/g++.dg/cpp0x/nsdmi-list9.C base-commit: 22fe3c05d86b52c35850918bfb21e1f597e1b5c7 diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc index 5dae7fccf19..e2ab255a7d5 100644 --- a/gcc/cp/typeck2.cc +++ b/gcc/cp/typeck2.cc @@ -1568,10 +1568,10 @@ massage_init_elt (tree type, tree init, int nested, int flags, new_flags |= LOOKUP_AGGREGATE_PAREN_INIT; init = digest_init_r (type, init, nested ? 2 : 1, new_flags, complain); /* When we defer constant folding within a statement, we may want to - defer this folding as well. Don't call this on CONSTRUCTORs because - their elements have already been folded, and we must avoid folding - the result of get_nsdmi. */ - if (TREE_CODE (init) != CONSTRUCTOR) + defer this folding as well. Don't call this on CONSTRUCTORs in + a template because their elements have already been folded, and + we must avoid folding the result of get_nsdmi. */ + if (!(processing_template_decl && TREE_CODE (init) == CONSTRUCTOR)) { tree t = fold_non_dependent_init (init, complain); if (TREE_CONSTANT (t)) diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-list10.C b/gcc/testsuite/g++.dg/cpp0x/nsdmi-list10.C new file mode 100644 index 00000000000..36b74749cbf --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-list10.C @@ -0,0 +1,35 @@ +// PR c++/118047 +// { dg-do compile { target c++11 } } + +typedef decltype(sizeof(char)) size_t; + +namespace std { +template +struct initializer_list { + const T *_M_array; + size_t _M_len; + constexpr size_t size() const { return _M_len; } +}; +} + +enum E { + One +}; + +struct A { + E e = One; +}; + +struct B { + A as[1] {}; +}; + +struct V +{ + constexpr V(const std::initializer_list &a) : size(a.size()){} + int size; +}; + +constexpr V a{{}}; + +static_assert(a.size == 1, ""); diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-list9.C b/gcc/testsuite/g++.dg/cpp0x/nsdmi-list9.C new file mode 100644 index 00000000000..ae69ba0810d --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-list9.C @@ -0,0 +1,34 @@ +// PR c++/118355 +// { dg-do compile { target c++11 } } + +enum MY_ENUM +{ + ZERO, +}; + +struct FOO +{ + MY_ENUM type = ZERO; +}; + +struct ARR +{ + FOO array[1] = {}; +}; + +template +struct ARR2 +{ + FOO array[1] = {}; +}; + +void +g () +{ + + ARR arr; + arr = {}; + + ARR2 arr2; + arr2 = {}; +}