From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24355 invoked by alias); 1 Mar 2015 22:09:43 -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 24345 invoked by uid 89); 1 Mar 2015 22:09:43 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=0.8 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,KAM_FROM_URIBL_PCCC,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=no version=3.3.2 X-HELO: mail-lb0-f173.google.com Received: from mail-lb0-f173.google.com (HELO mail-lb0-f173.google.com) (209.85.217.173) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Sun, 01 Mar 2015 22:09:42 +0000 Received: by lbiv13 with SMTP id v13so7902066lbi.1 for ; Sun, 01 Mar 2015 14:09:38 -0800 (PST) X-Received: by 10.112.130.39 with SMTP id ob7mr12193377lbb.32.1425247778548; Sun, 01 Mar 2015 14:09:38 -0800 (PST) Received: from debian-miyuki ([77.41.78.126]) by mx.google.com with ESMTPSA id uf3sm2120555lbc.44.2015.03.01.14.09.38 for (version=SSLv3 cipher=RC4-SHA bits=128/128); Sun, 01 Mar 2015 14:09:38 -0800 (PST) Date: Sun, 01 Mar 2015 22:09:00 -0000 From: Mikhail Maltsev To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix PR c++/65154 (ICE on braced initializer) Message-ID: <20150302010926.27f211e4@debian-miyuki> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2015-03/txt/msg00026.txt.bz2 The following patch is supposed to fix the problem related to initializing an array of aggregates with members, which have user-defined default constructors. Bootstrapped and regtested on x86_64-unknown-linux-gnu. diff --git a/gcc/cp/init.c b/gcc/cp/init.c index 0274663..0f53146 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -3716,11 +3716,9 @@ build_vec_init (tree base, tree maxindex, tree init, { if (cxx_dialect >= cxx11 && AGGREGATE_TYPE_P (type)) { - if (BRACE_ENCLOSED_INITIALIZER_P (init) - && CONSTRUCTOR_NELTS (init) == 0) - /* Reuse it. */; - else - init = build_constructor (init_list_type_node, NULL); + /* We cannot reuse INIT here, because it must not be shared + with member initializers. */ + init = build_constructor (init_list_type_node, NULL); CONSTRUCTOR_IS_DIRECT_INIT (init) = true; } else diff --git a/gcc/testsuite/g++.dg/init/array39.C b/gcc/testsuite/g++.dg/init/array39.C new file mode 100644 index 0000000..2fd8937 --- /dev/null +++ b/gcc/testsuite/g++.dg/init/array39.C @@ -0,0 +1,46 @@ +// PR c++/65154 +// { dg-do run { target c++11 } } + +int cnt1 = 0, + cnt2 = 0; + +struct S_empty +{ + S_empty () { + cnt1++; + }; +}; + +struct C1 +{ + S_empty s; +}; + +struct S_init +{ + S_init () : i(42) + { + cnt2++; + }; + int i; +}; + +struct C2 +{ + S_init a, b; +}; + +int +main () +{ + C1 c1[5]{}; + C2 c2[1]{}; + + if (c2[0].a.i != 42 || c2[0].b.i != 42) + return 1; + + if (cnt1 != 5 || cnt2 != 2) + return 1; + + return 0; +} Changelog record: 2015-03-02 Mikhail Maltsev PR c++/65154 * gcc/cp/init.c (build_vec_init): Fixed initializing aggregates with empty init list. * gcc/testsuite/g++.dg/init/array39.C: New test. N.B.: Consider the test case. The constructors of S_init and S_empty are called from main (this can be seen in GIMPLE dump, right after gimplification), while clang, for example, generates an explicit call to constructors of C1 and C2. GCC does the same, if direct initializers are removed. I don't know, if such behavior is OK or not. -- Regards, Mikhail Maltsev