From patchwork Tue Dec 10 19:13:23 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1207220 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=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-515637-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="ARUcIslh"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="V0ZZ/zEC"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47XV8g2c2Tz9sP3 for ; Wed, 11 Dec 2019 06:13:39 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type :content-transfer-encoding; q=dns; s=default; b=DPvYHdIii7c5KZHv moYbR0FoHItH1bKCorv9PY6yc27wTATumHVQmeMVpfKe9Oa5m+qfRirhmGPkIf9R jbavQQWtsJ/GG2UneZ38XHe8ocThJc4JK3IQjBs0bHVXl79hTfr56XmK7SS//Efu AD9+9kvur/7A2F7CUuQ91pdmmNE= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type :content-transfer-encoding; s=default; bh=e3PjJCAa9oIAurtNBTJxnF IWirQ=; b=ARUcIslhU/UJdLpVY8rx1llrwtgDfbp81z7bY2qfBkAPUmU2dsapm5 4s1BG4ehWShBK+dP40uWJeGc4mN63jIgHd++CU6WGaQBYCJ5w8k9BPINB5k1I9Id bEWAAq62HB6Zu6HitR33vraQYBpOmjbi3BmVVK0DuEJQYxv5yGgzk= Received: (qmail 19416 invoked by alias); 10 Dec 2019 19:13:31 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 19405 invoked by uid 89); 10 Dec 2019 19:13:31 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3 autolearn=ham version=3.3.1 spammy=sk:polacek, polacekredhatcom, polacek@redhat.com, U*polacek X-HELO: us-smtp-1.mimecast.com Received: from us-smtp-delivery-1.mimecast.com (HELO us-smtp-1.mimecast.com) (205.139.110.120) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 10 Dec 2019 19:13:30 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1576005208; 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=qEQ7fFx+PC2+klJYzBvjtBpFNNXgFTw54aoSuKCIsZI=; b=V0ZZ/zEC3eNrAyX67idRfUqcIp2FID14XlQisuyZstrl9/bZW8YZrByLrazSedARftLuIM WGtin4XxKfn/wHhF6ncgbgeMVlEsZ2jM5E5QdGADcqP2AqQNpOG3Kmb9HsCKuub6174LLP Ci5Jxi1KnyQ+WEFSCWkuvTaNDRxmltk= 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-254-2YsQ7dXGMEWU-RH5S8rH9Q-1; Tue, 10 Dec 2019 14:13:26 -0500 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 546278017DF for ; Tue, 10 Dec 2019 19:13:25 +0000 (UTC) Received: from redhat.com (unknown [10.20.4.137]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 00BFD6B8E7; Tue, 10 Dec 2019 19:13:24 +0000 (UTC) Date: Tue, 10 Dec 2019 14:13:23 -0500 From: Marek Polacek To: GCC Patches , Jason Merrill Subject: [C++ PATCH] c++/92878 - Parenthesized init of aggregates in new-expression. Message-ID: <20191210191323.GF1118066@redhat.com> MIME-Version: 1.0 User-Agent: Mutt/1.12.1 (2019-06-15) X-Mimecast-Spam-Score: 0 Content-Disposition: inline Ville pointed out that our paren init of aggregates doesn't work for auto a = new A(1, 2, 3); and I think it should: A new-expression that creates an object of type T initializes that object as follows: ... -- Otherwise, the new-initializer is interpreted according to the initialization rules of [dcl.init] for direct-initialization. so I think it follows that we should perform dcl.init#17.6.2.2. This doesn't work with new[]; we have: error ("parenthesized initializer in array new"); Bootstrapped/regtested on x86_64-linux, ok for trunk? 2019-12-10 Marek Polacek PR c++/92878 - Parenthesized init of aggregates in new-expression. * init.c (build_new_1): Handle parenthesized initialization of aggregates in new-expression. * g++.dg/cpp2a/paren-init20.C: New test. diff --git gcc/cp/init.c gcc/cp/init.c index e40afe27e1a..b0331b8ba53 100644 --- gcc/cp/init.c +++ gcc/cp/init.c @@ -3608,10 +3608,22 @@ build_new_1 (vec **placement, tree type, tree nelts, tree ie; /* We are processing something like `new int (10)', which - means allocate an int, and initialize it with 10. */ + means allocate an int, and initialize it with 10. - ie = build_x_compound_expr_from_vec (*init, "new initializer", - complain); + In C++20, also handle `new A(1, 2)'. */ + if (cxx_dialect >= cxx2a + && AGGREGATE_TYPE_P (type) + && (*init)->length () > 1) + { + ie = build_tree_list_vec (*init); + ie = build_constructor_from_list (init_list_type_node, ie); + CONSTRUCTOR_IS_DIRECT_INIT (ie) = true; + CONSTRUCTOR_IS_PAREN_INIT (ie) = true; + ie = digest_init (type, ie, complain); + } + else + ie = build_x_compound_expr_from_vec (*init, "new initializer", + complain); init_expr = cp_build_modify_expr (input_location, init_expr, INIT_EXPR, ie, complain); } diff --git gcc/testsuite/g++.dg/cpp2a/paren-init20.C gcc/testsuite/g++.dg/cpp2a/paren-init20.C new file mode 100644 index 00000000000..05da7604686 --- /dev/null +++ gcc/testsuite/g++.dg/cpp2a/paren-init20.C @@ -0,0 +1,54 @@ +// PR c++/92878 - Parenthesized init of aggregates in new-expression. +// { dg-do compile { target c++2a } } +// Test new TYPE(...). + +int f (); + +struct A +{ + int a; + int b; +}; + +void +fn_A () +{ + int i = 0; + auto y = new A(1, 2); + auto x = new A(++i, ++i); + auto z = new A(1, { ++i }); + auto u = new A(1, f()); +} + +struct B +{ + int a; + int b; + int c = 42; +}; + +void +fn_B () +{ + int i = 0; + auto y = new B(1, 2); + auto x = new B(++i, ++i); + auto z = new B(1, { ++i }); + auto u = new B(1, f()); +} + +struct C +{ + int a; + int b = 10; +}; + +void +fn_C () +{ + int i = 0; + auto y = new C(1); + auto x = new C(++i); + auto z = new C({ ++i }); + auto u = new C(f()); +}