From patchwork Tue Sep 1 16:10:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1355242 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=none (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=Ewk80th4; 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 4BgsXK5frQz9sXZ for ; Wed, 2 Sep 2020 02:10:47 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4104E38618BD; Tue, 1 Sep 2020 16:10:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4104E38618BD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1598976644; bh=lJqoDMv/V+P7EoJ+TVtYWOmm6ftVPKk0FpAOX9glHbs=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Ewk80th4qM7zE1ZTq9PLIthQ4N2pky9Qv9AgPLSzweVwABSnHnThS1klL+m2PqfFR GJZehM/yCgT847M/PyOYtjEv3ikFJSip4ab6YbNwn/U5KuwbYQeOwnqaP5E5DMAK6h bW/lVYRjbh75iJEgJ7/0FESxEfhi60ECAWsrH1VM= 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 [63.128.21.124]) by sourceware.org (Postfix) with ESMTP id 1346F385782F for ; Tue, 1 Sep 2020 16:10:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 1346F385782F 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-334-okQMgQMmPDWv1QR556jYFw-1; Tue, 01 Sep 2020 12:10:39 -0400 X-MC-Unique: okQMgQMmPDWv1QR556jYFw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 576FC1019634 for ; Tue, 1 Sep 2020 16:10:38 +0000 (UTC) Received: from pdp-11.redhat.com (ovpn-116-121.rdu2.redhat.com [10.10.116.121]) by smtp.corp.redhat.com (Postfix) with ESMTP id 015E35D9CC; Tue, 1 Sep 2020 16:10:37 +0000 (UTC) To: GCC Patches , Jason Merrill Subject: [PATCH] c++: Allow new char[4]{"foo"} [PR77841] Date: Tue, 1 Sep 2020 12:10:32 -0400 Message-Id: <20200901161032.4095949-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0.001 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_H5, 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" Currently, we allow new char[]{"foo"}, but not new char[4]{"foo"}. We should accept the latter too: [dcl.init.list]p3.3 says to treat this as [dcl.init.string]. We were rejecting this code because we never called reshape_init before the digest_init in build_new_1. reshape_init handles [dcl.init.string] by unwrapping the STRING_CST from its enclosing { }, and digest_init assumes that reshape_init has been called for aggregates anyway, and an array is an aggregate. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? gcc/cp/ChangeLog: PR c++/77841 * init.c (build_new_1): Call reshape_init. gcc/testsuite/ChangeLog: PR c++/77841 * g++.dg/cpp0x/initlist-new4.C: New test. --- gcc/cp/init.c | 6 ++++++ gcc/testsuite/g++.dg/cpp0x/initlist-new4.C | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp0x/initlist-new4.C base-commit: a292e31dac72c20cda3478b866ccf6e07dfad1a4 diff --git a/gcc/cp/init.c b/gcc/cp/init.c index 360ab8c0b52..d4540db3605 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -3575,6 +3575,12 @@ build_new_1 (vec **placement, tree type, tree nelts, /* We'll check the length at runtime. */ domain = NULL_TREE; arraytype = build_cplus_array_type (type, domain); + /* If we have new char[4]{"foo"}, we have to reshape + so that the STRING_CST isn't wrapped in { }. */ + vecinit = reshape_init (arraytype, vecinit, complain); + /* The middle end doesn't cope with the location wrapper + around a STRING_CST. */ + STRIP_ANY_LOCATION_WRAPPER (vecinit); vecinit = digest_init (arraytype, vecinit, complain); } } diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-new4.C b/gcc/testsuite/g++.dg/cpp0x/initlist-new4.C new file mode 100644 index 00000000000..d7b418426bb --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/initlist-new4.C @@ -0,0 +1,6 @@ +// PR c++/77841 +// { dg-do compile { target c++11 } } + +char *p1 = new char[4]{"foo"}; +char *p2 = new char[5]{"foo"}; +char *p3 = new char[3]{"foo"}; // { dg-error "too long" }