From patchwork Tue Feb 6 20:37:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 197644 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1810012dyb; Tue, 6 Feb 2024 12:38:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IGfv6gT/k72t3i69ujnU5sZyg1PbHEkzMJRUmPQBl29M4jKWW2d6y9upu98lx6UwzpSADdY X-Received: by 2002:a05:6214:2589:b0:68c:4f40:6661 with SMTP id fq9-20020a056214258900b0068c4f406661mr4714653qvb.11.1707251912470; Tue, 06 Feb 2024 12:38:32 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707251912; cv=pass; d=google.com; s=arc-20160816; b=M8MWyQy75amgtxOOwBj0lXaD6q/DUZ6mgqI4ZIyG9EP045g9TMLGtDf74Mz0n8LnOK Uij4Wokbcg7AJ7LdqiRnvy1nn2ZMG2qsi+r+aLydCBTGa1L3M84fSJhMyfnB5Ltm4LV5 4T+dvkdxD+I/fuMWcxddolHroUGJpI7+aOON5QMdjp0lwPFJLsDqD1wAyeIK+zudi7u2 k4xUDwriLWHx9uQY0JqXtAx3BC3egBFcda/HShF1IfnJlUurlbhHl4qQoE+1zVKJVIwO ye56FMf3cxqi9nG//2vyvpHKyhhZDeAshhnXR7HGgHJupUunUa0X202BnGoD9JrYZgdU u+eg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=vzVeyWPK2Kkv9wGOaoVteMjC0+dcAu+ecYsveIf/tRs=; fh=UJ68uqZuv8jrxHuFZNUZzS06Vvg9CbQmx0xK2Jegjb0=; b=Nqs9Jl9t5LtF38kSWjZW5t9HGsFXuzAoyjJ5CnvDsaTI5AlviVEDTMDc3zkUth7wO8 92519vWP5QcZJ6uHPSZbz9iv4R957Q+NEwVLQ6RDv4XvUkJBIeUx/Rb0wtgftK5PYpCl QlJ5YNAGC9PKV7wTBpRGqeKzrNeFdHSuVwfbpvDMUku5TGpuPe7Us6Rgr9bv268KriPg 3f9C9PGxdvlIMNK8FROhcadamC9pegD4t7QDjyLddSGc2jUgltJ1pg+QhuSBWXCJN0Rv xviBekVMNZjEu+hSYrxCUTZDxtDTakZg3rLr3J1QlbrCgIB1OtFHZUPRmG+H2wYOoKfL +uNA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=drPGF5I2; arc=pass (i=1); 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=redhat.com X-Forwarded-Encrypted: i=1; AJvYcCUt+hsNww3NxcsScWNEn2fB+g25qOVXO0eFHoRvkUb2rZKbNiTTxCQnqrhPrurH/b0TXWyg5IJvD21Viu3XMEaCacEd4Q== Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id eq2-20020ad45962000000b0067f7b1f8f34si3534957qvb.242.2024.02.06.12.38.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 12:38:32 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=drPGF5I2; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2F1FC385840F for ; Tue, 6 Feb 2024 20:38:32 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 305D93858D33 for ; Tue, 6 Feb 2024 20:37:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 305D93858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 305D93858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707251872; cv=none; b=u4XpISeXrjxXoqy7SoATs5PWi2K3EAbNQDdAIhG5kZKPa3tcRopkYHUT5MPlDCLsvKzetEwVGMsZ0B8eGxIzuGez+oOA8zyKpB4KzLSwl0VqpG3/Fuu67aUxTeo0tuh01kvSDydO0G/z/M+HyGYpm2gPw53mpK+x3WjUf4osCuY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707251872; c=relaxed/simple; bh=8HFBQmM/A0uDB7VZImb2o/EV65IS1EbSL/2pJZac4EA=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=Z61tB03ijXA3DQ3aB+M30jQTabZIJTmISKfmFuHDNJRs9HEDeGW5rOwytAZ94tfbtThCKrriXH4O2TZGmdNYmIK0mqodRbLaQv0izNPQAc4YKqrrgrg1Tmhj3rJ5zFUnp624juQwFk+m/6udwRGjpBk8ZvuMb9MZBQokpnsPWQM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707251870; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=vzVeyWPK2Kkv9wGOaoVteMjC0+dcAu+ecYsveIf/tRs=; b=drPGF5I2LB4vPHyzTyNUZmbxlFYWdqJu6+ob+xtOwUWvwpIzhz/V5pIZtV6iP8zBbLfBbp kEq6c/H6UTqkGtuDsHPvvRWBJuVXGJwj/0vfSijoyAW/lqDCNnneraouXwfF0YI68DDDIY spwW4ZTvm5XwyxUYvauwMQ1uOfS9qZ8= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-654-UzHcqZ-LMEaCR0KQPTmooQ-1; Tue, 06 Feb 2024 15:37:49 -0500 X-MC-Unique: UzHcqZ-LMEaCR0KQPTmooQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9AF1E38130AB for ; Tue, 6 Feb 2024 20:37:47 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.70]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 53401C07F51; Tue, 6 Feb 2024 20:37:47 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 416KbiVn1961303 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 6 Feb 2024 21:37:45 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 416Kbipf1961302; Tue, 6 Feb 2024 21:37:44 +0100 Date: Tue, 6 Feb 2024 21:37:44 +0100 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Disallow this specifier except for parameter declarations [PR113788] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790183381554341606 X-GMAIL-MSGID: 1790183381554341606 Hi! The deducing this patchset added parsing of this specifier to cp_parser_decl_specifier_seq unconditionally, but in the C++ grammar this[opt] only appears in the parameter-declaration non-terminal, so rather than checking in all the callers of cp_parser_decl_specifier_seq except for cp_parser_parameter_declaration that this specifier didn't appear I think it is far easier and closer to what the standard says to only parse this specifier when called from cp_parser_parameter_declaration. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-02-06 Jakub Jelinek PR c++/113788 * parser.cc (CP_PARSER_FLAGS_PARAMETER): New enumerator. (cp_parser_decl_specifier_seq): Parse RID_THIS only if CP_PARSER_FLAGS_PARAMETER is set in flags. (cp_parser_parameter_declaration): Or in CP_PARSER_FLAGS_PARAMETER when calling cp_parser_decl_specifier_seq. * g++.dg/parse/pr113788.C: New test. Jakub --- gcc/cp/parser.cc.jj 2024-01-17 10:34:45.337660930 +0100 +++ gcc/cp/parser.cc 2024-02-06 18:31:35.587193903 +0100 @@ -2088,7 +2088,9 @@ enum /* When parsing of the noexcept-specifier should be delayed. */ CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40, /* When parsing a consteval declarator. */ - CP_PARSER_FLAGS_CONSTEVAL = 0x80 + CP_PARSER_FLAGS_CONSTEVAL = 0x80, + /* When parsing a parameter declaration. */ + CP_PARSER_FLAGS_PARAMETER = 0x100 }; /* This type is used for parameters and variables which hold @@ -16342,7 +16344,7 @@ cp_parser_decl_specifier_seq (cp_parser* /* Special case for "this" specifier, indicating a parm is an xobj parm. The "this" specifier must be the first specifier in the declaration, after any attributes. */ - if (token->keyword == RID_THIS) + if (token->keyword == RID_THIS && (flags & CP_PARSER_FLAGS_PARAMETER)) { cp_lexer_consume_token (parser->lexer); if (token != first_specifier) @@ -25607,7 +25609,7 @@ cp_parser_parameter_declaration (cp_pars /* Parse the declaration-specifiers. */ cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer); cp_parser_decl_specifier_seq (parser, - flags, + flags | CP_PARSER_FLAGS_PARAMETER, &decl_specifiers, &declares_class_or_enum); --- gcc/testsuite/g++.dg/parse/pr113788.C.jj 2024-02-06 18:40:29.553791028 +0100 +++ gcc/testsuite/g++.dg/parse/pr113788.C 2024-02-06 18:41:23.326045703 +0100 @@ -0,0 +1,20 @@ +// PR c++/113788 +// { dg-do compile { target c++11 } } + +struct S { int a, b; }; +struct U { + void foo () { this int g = 1; } // { dg-error "expected ';' before 'int'" } +}; +this auto h = 1; // { dg-error "expected unqualified-id before 'this'" } + +int +main () +{ + S s = { 1, 2 }; + short t[3] = { 3, 4, 5 }; + this auto &[a, b] = s; // { dg-error "invalid use of 'this' in non-member function" } + this auto &[c, d, e] = t; // { dg-error "invalid use of 'this' in non-member function" } + this int f = 1; // { dg-error "invalid use of 'this' in non-member function" } + for (this auto &i : t) // { dg-error "invalid use of 'this' in non-member function" } + ; // { dg-error "expected" } +} // { dg-error "expected" }