From patchwork Thu Mar 19 21:28:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Pan2 via Gcc-patches" X-Patchwork-Id: 1258477 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=8.43.85.97; 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=AmHVvS4l; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (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 48k0Pv2nmlz9sRN for ; Fri, 20 Mar 2020 08:28:19 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9E9AB3945064; Thu, 19 Mar 2020 21:28:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9E9AB3945064 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1584653294; bh=D01ZWVRPQMS08ZFb/k+rUlNyCDQxgaNjubeYa49kp0M=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=AmHVvS4lqtzUmiEFkmvLoYTrT5O0eyzH+3wdTV5jZB966Z5RZcgZyef9Y51zSqstS cvFj9AEXmXzRpLNoJsNXuBYRUAI0YFwT+NG0FYuPf4t2RpboGn0bKveBZXeR9Y5amh J8q7nGjXmpWchwx0ZOyFbb6Sp9w8LfwzNXcrSyEA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [216.205.24.74]) by sourceware.org (Postfix) with ESMTP id 6A084385C017 for ; Thu, 19 Mar 2020 21:28:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 6A084385C017 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-145-4g-3BpkxNWCnNQvIwy1xmA-1; Thu, 19 Mar 2020 17:28:10 -0400 X-MC-Unique: 4g-3BpkxNWCnNQvIwy1xmA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4FF2A107ACC9 for ; Thu, 19 Mar 2020 21:28:09 +0000 (UTC) Received: from pdp-11.redhat.com (ovpn-112-181.rdu2.redhat.com [10.10.112.181]) by smtp.corp.redhat.com (Postfix) with ESMTP id A2C7417B91; Thu, 19 Mar 2020 21:28:08 +0000 (UTC) To: Jason Merrill , GCC Patches Subject: [PATCH] c++: template keyword in a typename-specifier [PR94057] Date: Thu, 19 Mar 2020 17:28:01 -0400 Message-Id: <20200319212801.2726275-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-25.2 required=5.0 tests=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 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: "Li, Pan2 via Gcc-patches" Reply-To: Marek Polacek Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Consider template class A { template class B { void fn(typename A::B); }; }; which is rejected with error: 'typename A::B' names 'template template class A::B', which is not a type whereas clang/icc/msvc accept it. "typename A::B" is a typename-specifier. Sadly, our comments don't mention it anywhere, because the typename-specifier wasn't in C++11; it was only added to the language in N1376. Instead, we handle it as an elaborated-type-specifier (not a problem thus far). So we get to cp_parser_nested_name_specifier_opt which has a loop that breaks if we don't see a < or ::, but that means we can -- tentatively -- parse even B which is not a nested-name-specifier (it doesn't end with a ::). Even though we're parsing B tentatively, we issue an error in cp_parser_class_name -> make_typename_type, but here we should not. In fact, we probably shouldn't have parsed "B" at all. Fixed by the cp_parser_class_name hunk. I think this should compile because [temp.names]/4 says: "In a qualified-id used as the name in a typename-specifier, elaborated-type-specifier, using-declaration, or class-or-decltype, an optional keyword template appearing at the top level is ignored.", added in DR 1710. Also see DR 1812. This issue on its own is not a significant problem or a regression. However, in C++20, the typename here becomes optional, and so this test is rejected in C++20, but accepted in C++17: template class A { template class B { void fn(A::B); }; }; Here we morph A::B into a typename-specifier, but that happens in cp_parser_simple_type_specifier and we never handle it as above. To fake the template keyword I'm afraid we need to use cp_parser_template_id with template_keyword_p=true as in the patch below. The tricky thing is to avoid breaking concepts. Does this approach make sense? Should these tests be accepted because of DR 1710 or am I off base here? Apologies for the verbosity, but I felt it necessary. Bootstrapped/regtested on x86_64-linux, built Boost/cmcstl2, ok for trunk? PR c++/94057 - template keyword in a typename-specifier. * parser.c (cp_parser_simple_type_specifier): Assume that a < following a qualified-id in a typename-specifier begins a template argument list. (cp_parser_class_name): Complain only if not parsing tentatively. * g++.dg/template/dependent-name5.C: Update dg-error. * g++.dg/template/dependent-name7.C: New test. * g++.dg/template/dependent-name8.C: New test. * g++.dg/template/dependent-name9.C: New test. --- gcc/cp/parser.c | 32 +++++++++++++++++-- .../g++.dg/template/dependent-name5.C | 2 -- .../g++.dg/template/dependent-name7.C | 9 ++++++ .../g++.dg/template/dependent-name8.C | 9 ++++++ .../g++.dg/template/dependent-name9.C | 9 ++++++ 5 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/template/dependent-name7.C create mode 100644 gcc/testsuite/g++.dg/template/dependent-name8.C create mode 100644 gcc/testsuite/g++.dg/template/dependent-name9.C base-commit: f22712bd8a2ed57d3cc7e6fa92730bd5852e27b3 diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index cbd5510a8fb..f4175955992 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -18113,6 +18113,33 @@ cp_parser_simple_type_specifier (cp_parser* parser, } } } + /* DR 1812: A < following a qualified-id in a typename-specifier + could safely be assumed to begin a template argument list, so + the template keyword should be optional. */ + else if (parser->scope + && qualified_p + && typename_p + && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)) + { + cp_parser_parse_tentatively (parser); + + type = cp_parser_template_id (parser, + /*template_keyword_p=*/true, + /*check_dependency_p=*/true, + none_type, + /*is_declaration=*/false); + /* This is handled below, so back off. */ + if (type && concept_check_p (type)) + cp_parser_simulate_error (parser); + + if (!cp_parser_parse_definitely (parser)) + type = NULL_TREE; + else if (TREE_CODE (type) == TEMPLATE_ID_EXPR) + type = make_typename_type (parser->scope, type, typename_type, + /*complain=*/tf_error); + else if (TREE_CODE (type) != TYPE_DECL) + type = NULL_TREE; + } /* Otherwise, look for a type-name. */ if (!type) @@ -23636,8 +23663,9 @@ cp_parser_class_name (cp_parser *parser, && decl != error_mark_node && !is_overloaded_fn (decl)) { - decl = make_typename_type (scope, decl, typename_type, - /*complain=*/tf_error); + tsubst_flags_t complain = (cp_parser_parsing_tentatively (parser) + ? tf_none : tf_error); + decl = make_typename_type (scope, decl, typename_type, complain); if (decl != error_mark_node) decl = TYPE_NAME (decl); } diff --git a/gcc/testsuite/g++.dg/template/dependent-name5.C b/gcc/testsuite/g++.dg/template/dependent-name5.C index fc78983324b..15c1acb0347 100644 --- a/gcc/testsuite/g++.dg/template/dependent-name5.C +++ b/gcc/testsuite/g++.dg/template/dependent-name5.C @@ -22,9 +22,7 @@ struct A typedef N type6; typedef A::N type7; -// { dg-error "" "" { target c++2a } .-1 } typedef A::N type8; -// { dg-error "" "" { target c++2a } .-1 } typedef A::template N type9; // { dg-error "" "" { target c++17_down } } typedef typename A::template N type10; diff --git a/gcc/testsuite/g++.dg/template/dependent-name7.C b/gcc/testsuite/g++.dg/template/dependent-name7.C new file mode 100644 index 00000000000..3dfa42d2df0 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/dependent-name7.C @@ -0,0 +1,9 @@ +// PR c++/94057 - template keyword in a typename-specifier. +// { dg-do compile } + +template struct A { + template struct B { + B(A::B&); + void fn(A::B); + }; +}; diff --git a/gcc/testsuite/g++.dg/template/dependent-name8.C b/gcc/testsuite/g++.dg/template/dependent-name8.C new file mode 100644 index 00000000000..ad9e44f9b85 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/dependent-name8.C @@ -0,0 +1,9 @@ +// PR c++/94057 - template keyword in a typename-specifier. +// { dg-do compile } + +template struct A { + template struct B { + B(typename A::B&); + void fn(typename A::B); + }; +}; diff --git a/gcc/testsuite/g++.dg/template/dependent-name9.C b/gcc/testsuite/g++.dg/template/dependent-name9.C new file mode 100644 index 00000000000..6dfdbc176c1 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/dependent-name9.C @@ -0,0 +1,9 @@ +// PR c++/94057 - template keyword in a typename-specifier. +// { dg-do compile } + +template struct A { + template struct B { + B(typename A::template B&); + void fn(typename A::template B); + }; +};