From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 113208 invoked by alias); 25 Feb 2020 18:55:35 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 113200 invoked by uid 89); 25 Feb 2020 18:55:35 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.9 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,KAM_SHORT,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy= X-HELO: us-smtp-1.mimecast.com Received: from us-smtp-1.mimecast.com (HELO us-smtp-1.mimecast.com) (207.211.31.81) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 25 Feb 2020 18:55:33 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582656932; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SzNpkWktLEn+PV2VmNOxTOLycoyUz7hOHW032Gq8j1s=; b=J+nKPQaTWEfNqR8MeHX/Qhrk+78+/DqsRsjkwUBJaArkxTreN/7uVjYYuYFKKoUGxS/E5k g9PypITEjTNAikOqZSCj5yLfeCIDal1nWPExeeCawgT64tZaz04eu4MeNEvVuBxBq4vpid gqyXVAxxjEEoJjSF6DDxHRZgBrv3vnA= 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-13-OcI53FSSPA64bd2CEVlEZA-1; Tue, 25 Feb 2020 13:55:27 -0500 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 E073980268C for ; Tue, 25 Feb 2020 18:55:26 +0000 (UTC) Received: from redhat.com (unknown [10.20.4.137]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 64B435DA60; Tue, 25 Feb 2020 18:55:26 +0000 (UTC) Date: Tue, 25 Feb 2020 18:55:00 -0000 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: Re: [PATCH v5] c++: Fix value-init crash in template [PR93676] Message-ID: <20200225185524.GM3554@redhat.com> References: <20200211195459.358172-1-polacek@redhat.com> <6eb452b2-c291-8a14-9e33-303a789db065@redhat.com> <20200219211523.GC3559@redhat.com> <1e9fd58e-f6d2-cc4a-185f-0bbbf0c0096e@redhat.com> <20200220165224.GA3554@redhat.com> <03345539-e36f-66c8-4e57-f40eb8c377c1@redhat.com> <20200225175256.GI3554@redhat.com> <36806f8f-7705-bc48-064b-385d7bd83782@redhat.com> MIME-Version: 1.0 In-Reply-To: <36806f8f-7705-bc48-064b-385d7bd83782@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-SW-Source: 2020-02/txt/msg01416.txt.bz2 On Tue, Feb 25, 2020 at 01:27:12PM -0500, Jason Merrill wrote: > On 2/25/20 12:52 PM, Marek Polacek wrote: > > On Mon, Feb 24, 2020 at 05:15:45PM -0500, Jason Merrill wrote: > > > On 2/20/20 11:52 AM, Marek Polacek wrote: > > > > On Thu, Feb 20, 2020 at 12:13:07AM +0000, Jason Merrill wrote: > > > > > On 2/19/20 10:15 PM, Marek Polacek wrote: > > > > > > On Fri, Feb 14, 2020 at 12:24:30AM +0100, Jason Merrill wrote: > > > > > > > On 2/11/20 8:54 PM, Marek Polacek wrote: > > > > > > > > Since we > > > > > > > > attempt to value-initialize in build_vec_init even when the= re's no > > > > > > > > initializer but the type has a constexpr default constructo= r. But > > > > > > > > build_value_init doesn't work in templates, so I think let'= s avoid > > > > > > > > this scenario; we'll go to the normal build_aggr_init path = then. > > > > > > > >=20 > > > > > > > > Bootstrapped/regtested on x86_64-linux, ok for trunk and br= anches? > > > > > > > >=20 > > > > > > > > PR c++/93676 - value-init crash in template. > > > > > > > > * init.c (build_vec_init): Don't perform value-init in a t= emplate. > > > > > > >=20 > > > > > > > Hmm, we really shouldn't even be calling build_vec_init in a = template, that > > > > > > > builds up a lot of garbage that we'll throw away at the end o= f build_new. > > > > > >=20 > > > > > > Ah, it's true that build_new will just creates a NEW_EXPR in a = template and > > > > > > doesn't use the result of build_new_1. Unfortunately I can't j= ust call > > > > > > build_special_member_call like we do in build_new_1 since that = crashes for > > > > > > array types. > > > > >=20 > > > > > We should call it for strip_array_types (type). > > > >=20 > > > > Since build_special_member_call takes an expression we'd have to mo= dify > > > > its type which I think is not pretty, but it works. Is this along = the > > > > lines you had in mind? > > >=20 > > >=20 > > >=20 > > > > I think I still like the v1 patch best but if you're fine with the > > > > following, then am I. > > > >=20 > > > > -- >8 -- > > > > Since we > > > > attempt to value-initialize in build_vec_init even when there's no > > > > initializer but the type has a constexpr default constructor. But > > > > build_value_init doesn't work in templates, and build_vec_init > > > > creates a lot of garbage that would not be used anyway, so don't > > > > call it in a template. > > > >=20 > > > > PR c++/93676 - value-init crash in template. > > > > * init.c (build_new_1): Don't call build_vec_init in a template. > > > >=20 > > > > * g++.dg/cpp0x/nsdmi-template19.C: New test. > > > > --- > > > > gcc/cp/init.c | 6 +++++- > > > > gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C | 13 +++++++++++++ > > > > 2 files changed, 18 insertions(+), 1 deletion(-) > > > > create mode 100644 gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C > > > >=20 > > > > diff --git a/gcc/cp/init.c b/gcc/cp/init.c > > > > index d480660445e..c60f332313a 100644 > > > > --- a/gcc/cp/init.c > > > > +++ b/gcc/cp/init.c > > > > @@ -3511,13 +3511,17 @@ build_new_1 (vec **placement, = tree type, tree nelts, > > > > explicit_value_init_p =3D true; > > > > } > > > > - if (processing_template_decl && explicit_value_init_p) > > > > + if (processing_template_decl) > > > > { > > > > /* build_value_init doesn't work in templates, and we don't n= eed > > > > the initializer anyway since we're going to throw it away = and > > > > rebuild it at instantiation time, so just build up a single > > > > constructor call to get any appropriate diagnostics. */ > > > > init_expr =3D cp_build_fold_indirect_ref (data_addr); > > > > + /* Avoid an ICE when converting to a base in build_simple_base_= path. > > > > + We'll throw this all away anyway, and build_new will create > > > > + a NEW_EXPR. */ > > > > + TREE_TYPE (init_expr) =3D strip_array_types (TREE_TYPE (init_ex= pr)); > > >=20 > > > instead of this, how about casting data_addr to elt_type* before > > > cp_build_fold_indirect_ref? Gotcha. I'm testing the following, OK for 8/9/10 if it passes? -- >8 -- Since we attempt to value-initialize in build_vec_init even when there's no initializer but the type has a constexpr default constructor. But build_value_init doesn't work in templates, and build_vec_init creates a lot of garbage that would not be used anyway, so don't call it in a template. PR c++/93676 - value-init crash in template. * init.c (build_new_1): Don't call build_vec_init in a template. * g++.dg/cpp0x/nsdmi-template19.C: New test. --- gcc/cp/init.c | 8 ++++++-- gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C | 13 +++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C diff --git a/gcc/cp/init.c b/gcc/cp/init.c index d480660445e..61ed3aa7e93 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -3511,13 +3511,17 @@ build_new_1 (vec **placement, tree typ= e, tree nelts, explicit_value_init_p =3D true; } =20 - if (processing_template_decl && explicit_value_init_p) + if (processing_template_decl) { + /* Avoid an ICE when converting to a base in build_simple_base_path. + We'll throw this all away anyway, and build_new will create + a NEW_EXPR. */ + tree t =3D fold_convert (build_pointer_type (elt_type), data_addr); /* build_value_init doesn't work in templates, and we don't need the initializer anyway since we're going to throw it away and rebuild it at instantiation time, so just build up a single constructor call to get any appropriate diagnostics. */ - init_expr =3D cp_build_fold_indirect_ref (data_addr); + init_expr =3D cp_build_fold_indirect_ref (t); if (type_build_ctor_call (elt_type)) init_expr =3D build_special_member_call (init_expr, complete_ctor_identifier, diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C b/gcc/testsuite/= g++.dg/cpp0x/nsdmi-template19.C new file mode 100644 index 00000000000..f3e2cb87fd6 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template19.C @@ -0,0 +1,13 @@ +// PR c++/93676 - value-init crash in template. +// { dg-do compile { target c++11 } } + +struct P { + int x =3D 0; +}; + +template +struct S { + S() { new P[2][2]; } +}; + +S s; base-commit: a71f2193d0df71a86c4743aab22891bb0003112e --=20 Marek Polacek =E2=80=A2 Red Hat, Inc. =E2=80=A2 300 A St, Boston, MA