From patchwork Wed Jan 14 16:28:51 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 429004 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 92D0E1401EB for ; Thu, 15 Jan 2015 03:29:14 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references; q=dns; s= default; b=ZNNOuERqZsljijNMetH2jfNS6rcTXUg11DRAU3cp5i9K6R06Di8Vw MLQfVqST7aoMyriAdfrapYpOYiPHqCvLOyfhypuNHFmNdl9bHdDhKssxL97JrB0d p6JqqM0G32TLBFsjv8kBPmUvAXmvwkPKrj4/DtLzwHvYCsZlFFJB94= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references; s= default; bh=9+gRZZTlp6R7F7yiJr999mSWDzw=; b=GDLnez6nkikNvfSyW2Sc BLOi4Ye2RklSeL3+IYs2c9mXlNMMifbmmHGk/ibQPDsMXkf5nQhuGdFQa7WVhcvq RrkzW2dq7xi4YcrRgDnr2ej+3H8VAvcGO76Czx5XVJeNyOGqfQoJKC4EWC7ARPVT /quIOQdLzMeAjQgJsF9mfpA= Received: (qmail 8878 invoked by alias); 14 Jan 2015 16:29:07 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 8862 invoked by uid 89); 14 Jan 2015 16:29:06 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.5 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 X-HELO: mail-qa0-f53.google.com Received: from mail-qa0-f53.google.com (HELO mail-qa0-f53.google.com) (209.85.216.53) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Wed, 14 Jan 2015 16:29:03 +0000 Received: by mail-qa0-f53.google.com with SMTP id n4so7285200qaq.12 for ; Wed, 14 Jan 2015 08:29:00 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=PzOHmSzecoELahNEK72ugAurC/RxExIjSkd0AV1JH2I=; b=BdL1xbJGMCWAaQjzno3QJ5GqhQg76gYAJGHY7+yANatCHbnF6eeyBJGRd07nhxZQh9 b8HLTNZfXbQPYCoxMMXoyHThjqQyYhEkopFQYllvGtnhHCphLLspUT6ISZfh/AUa8Z5g r66g2Fd7aIScNext++dwgut5n1S33dqO81EjHTLiows+R1gOj1LTCU2IITipbO+mxPqZ g/FcPsDn02Lw/LvM0byweHa2cOi8/D5OpexMQR6+Ldt7mggzW1Vn3Sg27buinq/QckU+ y5zZC87nxVGpMpVeTKOeyTNNXs3V53lFTB2EfCsH5/8SQGZu4PduBWP/ZERnBjhxgskz IW3A== X-Gm-Message-State: ALoCoQlogz/uFunpL6hHz31RfZLM8u0rfT0WJ3wXk94/hck9o8a4Mwk/kKpE6xXlNzERklHEEKzl X-Received: by 10.224.14.84 with SMTP id f20mr7974226qaa.43.1421252940855; Wed, 14 Jan 2015 08:29:00 -0800 (PST) Received: from localhost.localdomain (ool-4353ac94.dyn.optonline.net. [67.83.172.148]) by mx.google.com with ESMTPSA id o1sm18831755qat.47.2015.01.14.08.28.59 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 14 Jan 2015 08:28:59 -0800 (PST) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] Fix PR c++/16160 Date: Wed, 14 Jan 2015 11:28:51 -0500 Message-Id: <1421252931-32684-1-git-send-email-patrick@parcs.ath.cx> In-Reply-To: <1421207682-14372-1-git-send-email-patrick@parcs.ath.cx> References: <1421207682-14372-1-git-send-email-patrick@parcs.ath.cx> Here is version 2 of the patch which only adjusts a couple of testcases in g++.old-deja/ that I missed earlier. I am unsure if the extra dg-error in overload.C is correct. No code changes yet in gcc/cp/ versus the original patch. Successfully bootstrapped and regtested on x86_64-unknown-linux-gnu. I would advise against continuing rather than erroring out in the "an explicit instantiation may not have a definition" case because at that point the frontend thinks that the same declaration is simultaneously an explicit instantiation and a template specialization, which sounds potentially problematic (i.e. further processing may trigger ICEs or infinite loops or something). As for the "an explicit instantiation must be preceded by template" case, if we continue past this error, the declaration in question will be processed as a forward declaration to a template specialization and not as an explicit instantiation. So for instance the mentioned template will not be instantiated anyway. So there is not much potential for further diagnostic reporting. Doesn't sound too problematic, but doesn't sound very useful either. -- >8 -- This patch fixes the above PR where it was reported that the C++ frontend does not reject the malformed class declaration struct X<5>; Instead of rejecting it, the FE treats this declaration as if it were a forward declaration of a template specialization, i.e. as if it were written template<> struct X<5>; First off, the FE should reject the declaration because it is malformed (not 100% sure, though). Second, since the user probably intended to have written an explicit template instantiation (as in the PR), the FE should suggest adding "template" before such a declaration, that is the declaration struct X<5>; // error + suggest adding "template" This patch does both these things along with adding error messages + suggestions for struct X<5> { }; // error + suggest adding "template <>" and template struct X<5> { }; // error + suggest replacing with "template <>" gcc/cp/ChangeLog: PR c++/16160 * parser.c (cp_parser_class_head): Identify and reject malformed template-id declarations and definitions. gcc/testsuite/ChangeLog: PR c++/16160 * g++.dg/template/error55.C: New test. * g++.dg/cpp0x/gen-attrs-9.C: Adjust. * g++.dg/ext/attrib9.C: Adjust. * g++.dg/template/crash54.C: Adjust. * g++.old-deja/g++.jason/overload.C: Adjust. * g++.old-deja/g++.pt/spec24.C: Adjust. --- gcc/cp/parser.c | 53 ++++++++++++++++++------- gcc/testsuite/g++.dg/cpp0x/gen-attrs-9.C | 2 +- gcc/testsuite/g++.dg/ext/attrib9.C | 2 +- gcc/testsuite/g++.dg/template/crash54.C | 2 +- gcc/testsuite/g++.dg/template/error55.C | 11 +++++ gcc/testsuite/g++.old-deja/g++.jason/overload.C | 6 +-- gcc/testsuite/g++.old-deja/g++.pt/spec24.C | 3 +- 7 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 gcc/testsuite/g++.dg/template/error55.C diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 3290dfa..f6dc004 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -20264,6 +20264,34 @@ cp_parser_class_head (cp_parser* parser, } virt_specifiers = cp_parser_virt_specifier_seq_opt (parser); + /* Make sure a top-level template-id declaration or definition is preceded + by "template" or "template <>". */ + if (template_id_p + && at_namespace_scope_p () + && parser->num_template_parameter_lists == 0 + && !processing_explicit_instantiation) + { + if (cp_parser_next_token_starts_class_definition_p (parser)) + { + error_at (type_start_token->location, + "an explicit specialization must be preceded by " + "%