From patchwork Thu Nov 19 16:11:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1403121 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=TL085mV8; dkim-atps=neutral Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CcPnY6CGjz9sTv for ; Fri, 20 Nov 2020 03:11:45 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0320A3846034; Thu, 19 Nov 2020 16:11:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0320A3846034 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1605802303; bh=j1k1mUSFxX77cz6jOTp3dS3GbQjJH9yNQYNguSOJjfc=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=TL085mV8XlkpyRbX3pxhRO4fiaUVxfBTeRNRovUKFJ4yBDslK714g+djzS68/ijp8 bMTCn9patO5ZPnALVRdVKcmiDivhqGsIjiSP7iz0eSJToOsD5CJkhHCu/dg8wrsFje XnIj16kwKP386Eu8954w15qWiR51fpUrUukB4HI8= 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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 42CFE3857812 for ; Thu, 19 Nov 2020 16:11:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 42CFE3857812 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-407-ZvN1SnNoOuKnj0KzwaUxQg-1; Thu, 19 Nov 2020 11:11:37 -0500 X-MC-Unique: ZvN1SnNoOuKnj0KzwaUxQg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 46BFF184215E for ; Thu, 19 Nov 2020 16:11:36 +0000 (UTC) Received: from pdp-11.hsd1.ma.comcast.net (ovpn-119-42.rdu2.redhat.com [10.10.119.42]) by smtp.corp.redhat.com (Postfix) with ESMTP id D413860843; Thu, 19 Nov 2020 16:11:35 +0000 (UTC) To: GCC Patches , Jason Merrill Subject: [PATCH] c++: Fix array new with value-initialization [PR97523] Date: Thu, 19 Nov 2020 11:11:30 -0500 Message-Id: <20201119161130.3747041-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.2 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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Since my r11-3092 the following is rejected with -std=c++20: struct T { explicit T(); }; void fn(int n) { new T[1](); } with "would use explicit constructor 'T::T()'". It is because since that change we go into the P1009 block in build_new (array_p is false, but nelts is non-null and we're in C++20). Since we only have (), we build a {} and continue to build_new_1, which then calls build_vec_init and then we error because the {} isn't CONSTRUCTOR_IS_DIRECT_INIT. For (), which is value-initializing, we want to do what we were doing before: pass empty init and let build_value_init take care of it. For various reasons I wanted to dig a little bit deeper into this, and as a result, I'm adding a test for [expr.new]/24 (and checked that out current behavior matches clang++). Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? gcc/cp/ChangeLog: PR c++/97523 * init.c (build_new): When value-initializing an array new, leave the INIT as an empty vector. gcc/testsuite/ChangeLog: PR c++/97523 * g++.dg/expr/anew5.C: New test. * g++.dg/expr/anew6.C: New test. --- gcc/cp/init.c | 6 +++++- gcc/testsuite/g++.dg/expr/anew5.C | 26 ++++++++++++++++++++++++ gcc/testsuite/g++.dg/expr/anew6.C | 33 +++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/expr/anew5.C create mode 100644 gcc/testsuite/g++.dg/expr/anew6.C base-commit: 2729378d0905a04e476a8bdcaaf0288f417810ec diff --git a/gcc/cp/init.c b/gcc/cp/init.c index ffb84ea5b09..0b98f338feb 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -3766,7 +3766,11 @@ 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 && (array_p || (nelts && cxx_dialect >= cxx20))) + 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 ()))) { /* This means we have 'new T[]()'. */ if ((*init)->is_empty ()) diff --git a/gcc/testsuite/g++.dg/expr/anew5.C b/gcc/testsuite/g++.dg/expr/anew5.C new file mode 100644 index 00000000000..d597caf5483 --- /dev/null +++ b/gcc/testsuite/g++.dg/expr/anew5.C @@ -0,0 +1,26 @@ +// PR c++/97523 +// { dg-do compile } +// We were turning the () into {} which made it seem like +// aggregate-initialization (we are dealing with arrays here), which +// performs copy-initialization, which only accepts converting constructors. + +struct T { + explicit T(); + T(int); +}; + +void +fn (int n) +{ + new T[1](); + new T[2](); + new T[3](); + new T[n](); +#if __cpp_aggregate_paren_init + new T[](); + new T[2](1, 2); + // T[2] is initialized via copy-initialization, so we can't call + // explicit T(). + new T[3](1, 2); // { dg-error "explicit constructor" "" { target c++20 } } +#endif +} diff --git a/gcc/testsuite/g++.dg/expr/anew6.C b/gcc/testsuite/g++.dg/expr/anew6.C new file mode 100644 index 00000000000..0542daac275 --- /dev/null +++ b/gcc/testsuite/g++.dg/expr/anew6.C @@ -0,0 +1,33 @@ +// PR c++/97523 +// { dg-do compile { target c++11 } } + +// [expr.new]/24: If the new-expression creates an object or an array of +// objects of class type, access and ambiguity control are done for the +// [...] constructor selected for the initialization (if any). +// NB: We only check for a default constructor if the array has a non-constant +// bound, or there are insufficient initializers. Since an array is an +// aggregate, we perform aggregate-initialization, which performs +// copy-initialization, so we only accept converting constructors. + +struct T { + explicit T(); + T(int); +}; + +struct S { + S(int); +}; + +void +fn (int n) +{ + new T[1]{}; // { dg-error "explicit constructor" } + new T[2]{1, 2}; + new T[3]{1, 2}; // { dg-error "explicit constructor" } + new T[n]{}; // { dg-error "explicit constructor" } + + new S[1]{}; // { dg-error "could not convert" } + new S[2]{1, 2}; + new S[3]{1, 2}; // { dg-error "could not convert" } + new S[n]{}; // { dg-error "could not convert" } +}