From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 D3B0E3857812 for ; Thu, 19 Nov 2020 16:12:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D3B0E3857812 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-336-jTUod5YiNzig2wiHev2ihA-1; Thu, 19 Nov 2020 11:12:00 -0500 X-MC-Unique: jTUod5YiNzig2wiHev2ihA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D5536E75A for ; Thu, 19 Nov 2020 16:11:59 +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 7DA1E1E5; Thu, 19 Nov 2020 16:11:59 +0000 (UTC) From: Marek Polacek To: GCC Patches , Jason Merrill Subject: [PATCH] c++: Fix crash with broken deduction from {} [PR97895] Date: Thu, 19 Nov 2020 11:11:54 -0500 Message-Id: <20201119161154.3747116-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" 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_H4, 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-List-Received-Date: Thu, 19 Nov 2020 16:12:05 -0000 Unfortunately, the otherwise beautiful for (constructor_elt &elt : *CONSTRUCTOR_ELTS (init)) is not immune to an empty constructor, so we have to check CONSTRUCTOR_ELTS first. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? gcc/cp/ChangeLog: PR c++/97895 * pt.c (do_auto_deduction): Don't crash when the constructor has zero elements. gcc/testsuite/ChangeLog: PR c++/97895 * g++.dg/cpp0x/auto54.C: New test. --- gcc/cp/pt.c | 11 +++++++---- gcc/testsuite/g++.dg/cpp0x/auto54.C | 10 ++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/auto54.C diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 1babf833d32..a1b6631d691 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -29250,10 +29250,13 @@ do_auto_deduction (tree type, tree init, tree auto_node, return error_mark_node; if (BRACE_ENCLOSED_INITIALIZER_P (init)) - /* We don't recurse here because we can't deduce from a nested - initializer_list. */ - for (constructor_elt &elt : *CONSTRUCTOR_ELTS (init)) - elt.value = resolve_nondeduced_context (elt.value, complain); + { + /* We don't recurse here because we can't deduce from a nested + initializer_list. */ + if (CONSTRUCTOR_ELTS (init)) + for (constructor_elt &elt : *CONSTRUCTOR_ELTS (init)) + elt.value = resolve_nondeduced_context (elt.value, complain); + } else init = resolve_nondeduced_context (init, complain); diff --git a/gcc/testsuite/g++.dg/cpp0x/auto54.C b/gcc/testsuite/g++.dg/cpp0x/auto54.C new file mode 100644 index 00000000000..0c1815a99bc --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/auto54.C @@ -0,0 +1,10 @@ +// PR c++/97895 +// { dg-do compile { target c++11 } } + +namespace std { + template struct initializer_list { + const T *ptr; + decltype(sizeof 0) n; + }; + auto a = {}; // { dg-error "unable to deduce" } +} base-commit: 25bb75f841c552cfd27a4344b7487efbe35b4481 -- 2.28.0