From patchwork Tue Jul 2 20:43:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1955733 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=HVwt7edx; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WDFJg4N4Rz1xqb for ; Wed, 3 Jul 2024 06:44:33 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BBEA9388264A for ; Tue, 2 Jul 2024 20:44:31 +0000 (GMT) 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 63741386103B for ; Tue, 2 Jul 2024 20:44:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 63741386103B 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 63741386103B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719953049; cv=none; b=rgKUZGF+Bl95vp/Ksvc/e1FY8H/lBBS4xfnZq6EuE13JiU16jH7u4uzGDnRk3pDN9fGhHBFHA3vg7/gYJSh8Dz60fwGOfbpj7cjX4D9y4biPlRuk6BaVQdbS4hodpJtQ9fHlvGGZgoW3NdCOLJoWXKO5dTqj8dLAwgelp2gS8ig= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719953049; c=relaxed/simple; bh=qU9epV6D2L4nAiBQz44cDAHk9autHHGbmWyYL6nm1co=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=edMAAgk3rZAW5hNlKvPbG+hXWp67zQfZtdlfiU6hUV8fdyODmnQ3KKleuVDZxSs5TrHs/S9U1WpgIUh0xHtSWlN7FP4cnmSOYxnxB08997NSUHd2rOAAD8pBwKz5sOO5/n0fJ8UhB8xsMAv75ZdGkq8HmhCsWrvxtQXGgUQkMvQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719953047; 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=QPVTXiFsLapRcPgu9xSe7Rya7+m2WpuCF3bmcrqS/Rk=; b=HVwt7edxAQFVr4wpzxoWffp6Slp8CCMYkO3aRPgDtqP6fczhzHNX43BVTPgKpXrOVTX3eu ZxBiAVM2Ce0FpTet5Y87T6pqfzAc5XEyeOpGJZbrMNc28ClLip+kBPnV5nE++GDR2rEKbV r1Es4MYddfJUkzzKRooZolelBL/Mln8= Received: from mx-prod-mc-04.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-680-eiv8tZjfNPCptpLDzUbgxw-1; Tue, 02 Jul 2024 16:44:04 -0400 X-MC-Unique: eiv8tZjfNPCptpLDzUbgxw-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A333619560B6 for ; Tue, 2 Jul 2024 20:44:03 +0000 (UTC) Received: from pdp-11.lan (unknown [10.22.9.177]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id AF2351956048; Tue, 2 Jul 2024 20:44:02 +0000 (UTC) From: Marek Polacek To: Jason Merrill , GCC Patches Subject: [PATCH] c++: array new with value-initialization [PR115645] Date: Tue, 2 Jul 2024 16:43:59 -0400 Message-ID: <20240702204359.492020-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 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 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~incoming=patchwork.ozlabs.org@gcc.gnu.org Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/branches? -- >8 -- This extends the r11-5179 fix which doesn't work with multidimensional arrays. In particular, struct S { explicit S() { } }; auto p = new S[1][1](); should not say "converting to S from initializer list would use explicit constructor" because there's no {}. However, since we went into the block where we create a {}, we got confused. We should not have gotten there but we did because array_p was true. This patch refines the check once more. PR c++/115645 gcc/cp/ChangeLog: * init.cc (build_new): Don't do any deduction for arrays with bounds if it's value-initialized. gcc/testsuite/ChangeLog: * g++.dg/expr/anew7.C: New test. --- gcc/cp/init.cc | 12 ++++++++---- gcc/testsuite/g++.dg/expr/anew7.C | 13 +++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/expr/anew7.C base-commit: 1250540a98e0a1dfa4d7834672d88d8543ea70b1 diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc index 826a31c4a84..e9561c146d7 100644 --- a/gcc/cp/init.cc +++ b/gcc/cp/init.cc @@ -4005,10 +4005,14 @@ build_new (location_t loc, vec **placement, tree type, /* P1009: Array size deduction in new-expressions. */ const bool array_p = TREE_CODE (type) == ARRAY_TYPE; if (*init - /* If ARRAY_P, we have to deduce the array bound. For C++20 paren-init, - we have to process the parenthesized-list. But don't do it for (), - which is value-initialization, and INIT should stay empty. */ - && (array_p || (cxx_dialect >= cxx20 && nelts && !(*init)->is_empty ()))) + /* If the array didn't specify its bound, we have to deduce it. */ + && ((array_p && !TYPE_DOMAIN (type)) + /* For C++20 array with parenthesized-init, we have to process + the parenthesized-list. But don't do it for (), which is + value-initialization, and INIT should stay empty. */ + || (cxx_dialect >= cxx20 + && (array_p || nelts) + && !(*init)->is_empty ()))) { /* This means we have 'new T[]()'. */ if ((*init)->is_empty ()) diff --git a/gcc/testsuite/g++.dg/expr/anew7.C b/gcc/testsuite/g++.dg/expr/anew7.C new file mode 100644 index 00000000000..ead5536e109 --- /dev/null +++ b/gcc/testsuite/g++.dg/expr/anew7.C @@ -0,0 +1,13 @@ +// PR c++/115645 +// { dg-do compile { target c++11 } } + +struct S { + explicit S() { } +}; + +auto p = new S[1][1](); +auto q = new S[1][1]{}; // { dg-error "explicit" } +auto r = new S[1](); +auto s = new S[1]{}; // { dg-error "explicit" } +auto t = new S[1][1][1](); +auto u = new S[1][1][1]{}; // { dg-error "explicit" }