From patchwork Tue Aug 8 02:48:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 1818376 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=Cmh9YHsa; 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 4RKd1g4Wf5z1yfM for ; Tue, 8 Aug 2023 12:49:11 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7E91638582B7 for ; Tue, 8 Aug 2023 02:49:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7E91638582B7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691462949; bh=KkVynM5hDWOkGkh2ClZ4a1jWSXzJuQjzYMAKOZyrtSw=; 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=Cmh9YHsaaA09vet9XwqPe37gIEYNeVOSunQandByMrZi7VSHm25NfdTNbckqEL1ZS aWJO5TsP2sXAtLcddVeg/HELpkRNHnN2qQ6AxawvQHHEFW/YNxanwMXsko1m6Ze7ET 6Z/o0qzNaLBja6kWKpwL7ertrSB13/8FN1W2J9bs= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by sourceware.org (Postfix) with ESMTPS id F1E5D3858D33 for ; Tue, 8 Aug 2023 02:48:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F1E5D3858D33 Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-2680a031283so2941260a91.3 for ; Mon, 07 Aug 2023 19:48:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691462929; x=1692067729; 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=KkVynM5hDWOkGkh2ClZ4a1jWSXzJuQjzYMAKOZyrtSw=; b=LIYAVpr2YBfbaANHlq5TL9o4lxzEnH+m2mYJAtRs144EU3YCjDZAK5XyRKRhQWeeK7 UMQ9SMgDAZ6KSfPaXrLGrT190JjJghdJJeBznxQ6iPSwW89VZ1g+hlB5QjiO5r59s0hN erymi7H7+Mo8m6jnFQofmA3LhkeETYHHT+HPF8MW/yvpXh3meklXZ4363i9abHa/Hh7r uYv3veFR1mR5gRYJSvSzUF/G9L46271dAjfqyJMAo6zPTFc/6zx9VzlMeqQx5Qoc3/hY Fma2PSPGTV9QL5kNgIbiqr8HbcrWrcT+hGJUJth/zk8D2XWdD01nNzg/J1O3MdcNzwwP u7Kg== X-Gm-Message-State: AOJu0YxvyBz6W0+dYgJU8PTUuXFiwF41dBjwmD/B8JVk2oQp6ZTv44si QVe8P4XPyhoF6J2Py0MCzRxSKGeybh8= X-Google-Smtp-Source: AGHT+IFhk4Z0qNCqkvA/NeeJqLzSLEHXt1KUHpQBwVgkrfEt0ia0EChFth0//lAjz2j09JYbhrCBNA== X-Received: by 2002:a17:90a:9f0a:b0:269:38c2:dbd8 with SMTP id n10-20020a17090a9f0a00b0026938c2dbd8mr7855773pjp.25.1691462928673; Mon, 07 Aug 2023 19:48:48 -0700 (PDT) Received: from Thaum.localdomain (59-102-120-25.tpgi.com.au. [59.102.120.25]) by smtp.gmail.com with ESMTPSA id 9-20020a17090a190900b00256a4d59bfasm10159043pjg.23.2023.08.07.19.48.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 19:48:48 -0700 (PDT) Date: Tue, 8 Aug 2023 12:48:43 +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, 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" Sorry, noticed I provided the wrong version of the test. Here is the correct version (not relying on 'namespace std' being implicitly defined). Bootstrapped + regtested on x86_64-pc-linux-gnu. -- 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 d7ef5b34d42..119a14d03b9 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -16506,10 +16506,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 @@ -16530,13 +16526,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..1df8b162743 --- /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(N) 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" }