From patchwork Tue Jul 7 22:40:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1324767 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=yWV/+zB3; 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 4B1cpn4KQTz9sRR for ; Wed, 8 Jul 2020 08:40:49 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A4C383861024; Tue, 7 Jul 2020 22:40:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A4C383861024 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1594161647; bh=I0H6Bd5i2hzSberLa1pyFiejwJl/5HDIR3P3WSRLNSY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=yWV/+zB3j4fkKuzSzC9M9aYNz89McbhBdolzH3bx11SWTvzPJufV31ry15WWUXPUy U9LJdJD5Tihcn83mnZoP0cBxFs3mo+JnYXPNDtfhkS79llb3LM1LclkJ9bHdGBhkjL j+qzAyLadIoy18C59dXBWlWimVTwWYe6Wdg47ruw= 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 [205.139.110.120]) by sourceware.org (Postfix) with ESMTP id 439DE3857010 for ; Tue, 7 Jul 2020 22:40:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 439DE3857010 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-462-1qOSIuruMpGa6SOUGE4lJQ-1; Tue, 07 Jul 2020 18:40:43 -0400 X-MC-Unique: 1qOSIuruMpGa6SOUGE4lJQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5831F804001 for ; Tue, 7 Jul 2020 22:40:42 +0000 (UTC) Received: from pdp-11.redhat.com (ovpn-114-134.rdu2.redhat.com [10.10.114.134]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0223F19D81; Tue, 7 Jul 2020 22:40:41 +0000 (UTC) To: Jason Merrill , GCC Patches Subject: [PATCH] c++: Better diagnostic for decltype(auto) in C++11 [PR96103] Date: Tue, 7 Jul 2020 18:40:39 -0400 Message-Id: <20200707224039.606120-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-15.8 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_H2, 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" If you try to use decltype(auto) in C++11, we emit obscure error: expected primary-expression before 'auto' giving the user no hint as to what's wrong. This patch improves that diagnostic. Since we've been giving an error, I'm also using error(). Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? gcc/cp/ChangeLog: PR c++/96103 * parser.c (cp_parser_decltype): Print error about using decltype(auto) in C++11. Check that the token following "auto" is ")". gcc/testsuite/ChangeLog: PR c++/96103 * g++.dg/cpp0x/decltype77.C: New test. --- gcc/cp/parser.c | 22 +++++++++++++++------- gcc/testsuite/g++.dg/cpp0x/decltype77.C | 10 ++++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/decltype77.C base-commit: 7126583af5d29235584b51b3b05eeaba2adef024 diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index e58d8eb298c..8aaedaefb86 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -14894,11 +14894,11 @@ cp_parser_decltype_expr (cp_parser *parser, return expr; } -/* Parse a `decltype' type. Returns the type. +/* Parse a `decltype' type. Returns the type. - simple-type-specifier: + decltype-specifier: decltype ( expression ) - C++14 proposal: + C++14: decltype ( auto ) */ static tree @@ -14938,10 +14938,18 @@ cp_parser_decltype (cp_parser *parser) tree expr = NULL_TREE; - if (cxx_dialect >= cxx14 - && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)) - /* decltype (auto) */ - cp_lexer_consume_token (parser->lexer); + if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO) + && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)) + { + /* decltype (auto) */ + cp_lexer_consume_token (parser->lexer); + if (cxx_dialect < cxx14) + { + error_at (start_token->location, + "% type specifier is a C++14 extension"); + expr = error_mark_node; + } + } else { /* decltype (expression) */ diff --git a/gcc/testsuite/g++.dg/cpp0x/decltype77.C b/gcc/testsuite/g++.dg/cpp0x/decltype77.C new file mode 100644 index 00000000000..1f987118510 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/decltype77.C @@ -0,0 +1,10 @@ +// PR c++/96103 +// { dg-do compile { target c++11_only } } + +decltype(auto) foo () { return 4; } // { dg-error ".decltype\\(auto\\). type specifier is a C\\+\\+14 extension" } + +void +bar () +{ + decltype(auto) i = 0; // { dg-error ".decltype\\(auto\\). type specifier is a C\\+\\+14 extension" } +}