From patchwork Tue Aug 8 02:40:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 1818374 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.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=W0jlV5dm; dkim-atps=neutral Received: from server2.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 ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RKcqz1Gxmz1yYC for ; Tue, 8 Aug 2023 12:40:46 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 254023858428 for ; Tue, 8 Aug 2023 02:40:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 254023858428 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691462444; bh=FofxZfEcnYFF8qi9IDSPi3eqorZOze3Uxp0gFEzSKTs=; h=Date:To:Cc:Subject:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=W0jlV5dmkaTvkjDQ7S268s07DmOj8SbnlVdTAYe11fxp0RCPPSsGj01LiEwx6AGPj G8lBLIQS2tXF79upr5GFznX2WUoPMGyTri4M9Gcne4DTM8kTM8V5KGS6rjwIUrjvrZ Sk5HWHmtNq/8wX5JDdPN8S7JJih4kajyMIIV/C/o= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by sourceware.org (Postfix) with ESMTPS id 5E5633858C41 for ; Tue, 8 Aug 2023 02:40:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5E5633858C41 Received: by mail-pj1-x102b.google.com with SMTP id 98e67ed59e1d1-267fabc8465so2871552a91.1 for ; Mon, 07 Aug 2023 19:40:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691462423; x=1692067223; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=FofxZfEcnYFF8qi9IDSPi3eqorZOze3Uxp0gFEzSKTs=; b=iP0dpZRTZuHVoffgUW0iGXBGXxr5gV2caH3RYhCFvrXhriTjhvoMJV4PymdadOS3G0 sUt2mMw5iwYlppcAv1FoWJ42q4Q8TTuPn1+6T2rweXoKm0S/72qoTIl8leFII5aSEo3S QbJN7rNcLbGmODXwRxN2kAHX8Il7MQdDEGTrParYBjNyc4hNcsDlWqR8X4F60nTbkF7y gll1K3d2uH/vtF61cqw7Z5HVy7/1UorAh0wpsuglegbhad31W41SxVNSleEdmkWGmW8Y qPEkHGQP/G74voQuCGhGtd1rhn2nr4PrIB9Dow1D7k6mc7IoNsbXd6GzXEfYjg2om2Q1 5coA== X-Gm-Message-State: AOJu0YzkZRzGbUlntlIMRBXWhSR6eHIaozZt7Uq6LsBhld3s8WmFMlkH KgYohG4GI43CGv0Xvd8JF+W9YqKocc0= X-Google-Smtp-Source: AGHT+IFjFl0JN1ELclAjoVV+99h0/ckeESSiBQ0Tt5wLDOowCII/zRmWtoHVxW1XA1IVb264tC0ROg== X-Received: by 2002:a17:90a:688e:b0:268:1d18:a19f with SMTP id a14-20020a17090a688e00b002681d18a19fmr9107076pjd.25.1691462423020; Mon, 07 Aug 2023 19:40:23 -0700 (PDT) Received: from Thaum.localdomain (59-102-120-25.tpgi.com.au. [59.102.120.25]) by smtp.gmail.com with ESMTPSA id i4-20020a17090a64c400b0026307fa0442sm9266288pjm.49.2023.08.07.19.40.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 19:40:22 -0700 (PDT) Date: Tue, 8 Aug 2023 12:40:17 +1000 To: gcc-patches@gcc.gnu.org Cc: Patrick Palka , Jason Merrill Subject: [PATCH] c++: Report invalid id-expression in decltype [PR100482] Message-ID: References: <4cd26c62-2ecf-db54-f180-9fe4be282449@idea> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Nathaniel Shead via Gcc-patches From: Nathaniel Shead Reply-To: Nathaniel Shead Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Ping for https://gcc.gnu.org/pipermail/gcc-patches/2023-June/622681.html. -- 8< -- This patch ensures that any errors raised by finish_id_expression when parsing a decltype expression are properly reported, rather than potentially going ignored and causing invalid code to be accepted. We can also now remove the separate check for templates without args as this is also checked for in finish_id_expression. PR c++/100482 gcc/cp/ChangeLog: * parser.cc (cp_parser_decltype_expr): Report errors raised by finish_id_expression. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/decltype-100482.C: New test. Signed-off-by: Nathaniel Shead --- gcc/cp/parser.cc | 22 ++++++++++---------- gcc/testsuite/g++.dg/cpp0x/decltype-100482.C | 12 +++++++++++ 2 files changed, 23 insertions(+), 11 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/decltype-100482.C diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index dd3665c8ccf..990f2034f54 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -16508,10 +16508,6 @@ cp_parser_decltype_expr (cp_parser *parser, expr = cp_parser_lookup_name_simple (parser, expr, id_expr_start_token->location); - if (expr && TREE_CODE (expr) == TEMPLATE_DECL) - /* A template without args is not a complete id-expression. */ - expr = error_mark_node; - if (expr && expr != error_mark_node && TREE_CODE (expr) != TYPE_DECL @@ -16532,13 +16528,17 @@ cp_parser_decltype_expr (cp_parser *parser, &error_msg, id_expr_start_token->location)); - if (expr == error_mark_node) - /* We found an id-expression, but it was something that we - should not have found. This is an error, not something - we can recover from, so note that we found an - id-expression and we'll recover as gracefully as - possible. */ - id_expression_or_member_access_p = true; + if (error_msg) + { + /* We found an id-expression, but it was something that we + should not have found. This is an error, not something + we can recover from, so report the error we found and + we'll recover as gracefully as possible. */ + cp_parser_parse_definitely (parser); + cp_parser_error (parser, error_msg); + id_expression_or_member_access_p = true; + return error_mark_node; + } } if (expr diff --git a/gcc/testsuite/g++.dg/cpp0x/decltype-100482.C b/gcc/testsuite/g++.dg/cpp0x/decltype-100482.C new file mode 100644 index 00000000000..3df9c8496d7 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/decltype-100482.C @@ -0,0 +1,12 @@ +// PR c++/100482 +// { dg-do compile { target c++11 } } + +namespace N {} +decltype(std) x; // { dg-error "expected primary-expression" } + +struct S {}; +decltype(S) y; // { dg-error "argument to .decltype. must be an expression" } + +template +struct U {}; +decltype(U) z; // { dg-error "missing template arguments" }