From patchwork Sat Apr 18 14:49:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1272613 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=ytPdQnMq; 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 494G8J04Pcz9sWv for ; Sun, 19 Apr 2020 00:49:44 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 80769385BF81; Sat, 18 Apr 2020 14:49:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 80769385BF81 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1587221375; bh=v9SK5to0MxUQbMHg60awXvCLmvvPdyLi0R2T1oUYR1U=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=ytPdQnMqTTKNLOqf/Gv+ofhAJCQdTziYgbkNecFuIG46O4va6tMtgpy7N80v3P4i5 f198ESw3wt1yumLAt9mxS7l/HOKSvirGvDZB7pzXYeHzhQELBQn484dqJs2h40+dvZ xt3Yyv5bOoMy0PV1/mP69nip9vNwHKsuFcevizZ0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by sourceware.org (Postfix) with ESMTP id A3392385B835 for ; Sat, 18 Apr 2020 14:49:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A3392385B835 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-111-R_pCNb0yNlmXAbIKeRmcOQ-1; Sat, 18 Apr 2020 10:49:29 -0400 X-MC-Unique: R_pCNb0yNlmXAbIKeRmcOQ-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 96272107ACC7 for ; Sat, 18 Apr 2020 14:49:28 +0000 (UTC) Received: from pdp-11.redhat.com (ovpn-113-252.rdu2.redhat.com [10.10.113.252]) by smtp.corp.redhat.com (Postfix) with ESMTP id 24F565D9C5; Sat, 18 Apr 2020 14:49:28 +0000 (UTC) To: Jason Merrill , GCC Patches Subject: [PATCH] c++: Fix crash with template spec in different namespace [PR94255] Date: Sat, 18 Apr 2020 10:49:20 -0400 Message-Id: <20200418144920.1479035-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-29.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, 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" This is an ICE on invalid, because we're specializing S::foo in the wrong namespace. cp_parser_class_specifier_1 parses S::foo in M and then it tries to push the nested-name-specifier of foo, which is S. By that, we're breaking the assumption of push_inner_scope that the pushed scope must be a scope nested inside current scope: current scope is M, but the namespace context of S is N, and N is not nested in M, so we fell into an infinite loop in push_inner_scope_r. (cp_parser_class_head called check_specialization_namespace which already gave a permerror.) Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR c++/94255 * parser.c (cp_parser_class_specifier_1): Check that the scope is nested inside current scope before pushing it. * g++.dg/template/spec41.C: New test. --- gcc/cp/parser.c | 7 ++++++- gcc/testsuite/g++.dg/template/spec41.C | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/template/spec41.C base-commit: b57e1621eb76ba80c949ad098829aa8171a8c4ab diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index d2f3f853cb4..d4482ef5a90 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -23858,7 +23858,12 @@ cp_parser_class_specifier_1 (cp_parser* parser) if (nested_name_specifier_p) { scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)); - old_scope = push_inner_scope (scope); + /* SCOPE must be a scope nested inside current scope. */ + if (is_nested_namespace (current_namespace, + decl_namespace_context (scope))) + old_scope = push_inner_scope (scope); + else + nested_name_specifier_p = false; } type = begin_class_definition (type); diff --git a/gcc/testsuite/g++.dg/template/spec41.C b/gcc/testsuite/g++.dg/template/spec41.C new file mode 100644 index 00000000000..249fde74c3a --- /dev/null +++ b/gcc/testsuite/g++.dg/template/spec41.C @@ -0,0 +1,17 @@ +// PR c++/94255 - crash with template spec in different namespace. +// { dg-do compile { target c++11 } } + +namespace N { + class S { + template struct foo; + }; + namespace M { + using S = ::N::S; + } +} + +namespace N { + namespace M { + template <> struct S::foo {}; // { dg-error "specialization of" } + } +}