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: 132489 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1840520vqr; Mon, 7 Aug 2023 19:49:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGbmQLJWCWpWpymIALTxfmRh+6PsXaw806P63rJBKIAcuk2kz3WgfO94bjuQh70MB4LvKHL X-Received: by 2002:a05:6402:1243:b0:523:fa4:6530 with SMTP id l3-20020a056402124300b005230fa46530mr9620046edw.10.1691462973698; Mon, 07 Aug 2023 19:49:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691462973; cv=none; d=google.com; s=arc-20160816; b=znpGbydFmFJtwTMyRVEWXUd20b07jYULgT2zTHnCtziXyGXPNFK08REYyOK9pFqWHS 480M+zM1t1jPCIcLOeU1gKsYePahwsvQsAtgFV1M2lrM5OLM7GppWNTYT0Pv2zJqyIMi NMKdZrYysOtcjbrctdg0Cra874Di8KEvjcfcvo/lk5xVC4At+yBd4RjUviWApE6kcG6G MU92LoT5xOPhgwZ04GTu+tqQ7KL5dMt4OxTt4h7JVpaKGk5pR2i4BmMk55Hycd7y6ydQ xU4MTWyCvcNP5+dnRXHKZAc3834b8Iqit+s4hlJuTthtaB7GboDniJedq/0G9FUSvZ7f dHlQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:date:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=KkVynM5hDWOkGkh2ClZ4a1jWSXzJuQjzYMAKOZyrtSw=; fh=dKYAqBRQc5As17VcTBVmpEqV2XcO6vNkRJVM8xaHGUE=; b=SWqN706S9dTRlzR7xnCie96iqBv5rnA9YlNtBDBRV4Z77rmHYC26YvHs6+zGL0VkkE DLduemeyBuaYfVg5o4zGr/t+d+G6IWk+Fo7LzwOKGp9woxwPBDN0uJdBK3XNkfilwjdX C88FOE3rc4hdzk16NR3HkUZJchxxkPRUvBsl5WEuO/WplrEFh90bZcwgtG6BgD+aMTER lYvzNQoYZIu4/t8EcurUWPm+5dJbqSY+y5F6z9sXJp1Nc1UwInq5tMjFqU0N4osFlPES 53MhDDZC2U9zkWkiLpPccflUINExtn8Hhz0vaB4kBbi1DUAZVZGNRw6Nai6Cvp7408rr TIbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=M6nkNghY; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id t2-20020aa7d702000000b00522def3c64csi5681778edq.615.2023.08.07.19.49.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 19:49:33 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=M6nkNghY; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A1F623858439 for ; Tue, 8 Aug 2023 02:49:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A1F623858439 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691462972; 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=M6nkNghYWcEjEUDxH/ppwQreVYnebNDPcxJCajucvOr9Jz/pUJs/wfRskCuDThK1b JO1DjrvgRCzkbtlPDTxfYbaNxd/XeK1QFIrHYqblUlvt23erNM93aVsGap8z58md+q mx9BsrAR2ZjLQ+vaxLhoH69/mDnGQ6GV2hR/TUhI= 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-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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1764564797380242481 X-GMAIL-MSGID: 1773627479282817489 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" }