From patchwork Thu Jan 5 17:20:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 39709 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp408881wrt; Thu, 5 Jan 2023 09:20:59 -0800 (PST) X-Google-Smtp-Source: AMrXdXsl60pRMUi2RjpfiuQbumcWZwgj1E+TOIgaM+iW7iWYejlRFvXR3WJ+jOXcpKK2d5jnq1uP X-Received: by 2002:a17:907:8d03:b0:7c0:d522:decb with SMTP id tc3-20020a1709078d0300b007c0d522decbmr47171596ejc.76.1672939259891; Thu, 05 Jan 2023 09:20:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672939259; cv=none; d=google.com; s=arc-20160816; b=eEsBOY60pfx+buv4ZJrdBn8HhEZJBfQs2X1XwZJXxtiRmBRlM9odpk/i4JGWawpHlj StVPt9x1mEafYL9h28tjAHh7GeaqqLF+eHv9Y/N7GuNivg2aYrKSoDeCO9KLbWFa9NH9 bhf90BzbZaHDjG0izRMespJzJwCyQZV6b6+G0XkjJrAP+rJqGR5pGB4NrqV39l8uso/+ +G7/1IWFD5xWnbCSbwDTL8xPEt4/8HgYTxx19YWRndAjLS/T7YbOKsYiJQSugbKX+ao8 s9mViRlaRpmlBPsciOaPQQd+VmeKRkMZnxb6qRdpnGoAox3MY3BAOgW65OwGgltBpLIv C3vw== 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 :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=seBi63NAd2giMsINF+6LKi4lMMZx0dq/HCcpHbmZWT8=; b=IqFBeU/JgObqLpJ4sp78rmHUPDJu7hxKbMFN0DLEINx5bp3sXNDxfuNLrSFDecXfd2 fhOoY99NBB0NQyvvp8J8WTd7ciPCpDW9EwwSRvimunZ+8jL4TzyjxIi6bay+iUngd3MJ N01qOqjfXkEZIfed3B7y678bZJdkQdsRMAcyUT7B4CMNa7y7f7BUiBUlzhPFZXh0LvFP PGWycsvNWUJJVeRr7Sw2sHwa1X3jskenUwcpdmEtqYA6Q0lNPQtkUyN1wYwQSX2oP8J0 APINw7rPhKHOZmSVxXLxpSXYGkaPLlj0fx8s9VfJsxIVe8i0E/4PXL0H+tWteqT4FDsd jXPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Usktw8wO; 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 sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id wt8-20020a170906ee8800b0078d288ddfc9si34495853ejb.143.2023.01.05.09.20.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Jan 2023 09:20:59 -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=@gcc.gnu.org header.s=default header.b=Usktw8wO; 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 D9B443858414 for ; Thu, 5 Jan 2023 17:20:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D9B443858414 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1672939258; bh=seBi63NAd2giMsINF+6LKi4lMMZx0dq/HCcpHbmZWT8=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Usktw8wOV3LykrNjLYoCdZ58BgM7lVv0PMAcI6BmowOwVHn2A2DXNqzFViBSotehl rtURX8NElHY2zode5PIqqCXDdDuGNsTAKy/z1vi9bnogIHqUFxwgCVyDpDqaGeO/1L qrTNExhisY21JvAWSQyQmAnVfDHB3j7WNcVzSIYY= 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 408763858C3A for ; Thu, 5 Jan 2023 17:20:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 408763858C3A Received: from mail-yb1-f197.google.com (mail-yb1-f197.google.com [209.85.219.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-634-W83Ori2UPs-K7FcV55sjuw-1; Thu, 05 Jan 2023 12:20:14 -0500 X-MC-Unique: W83Ori2UPs-K7FcV55sjuw-1 Received: by mail-yb1-f197.google.com with SMTP id a4-20020a5b0004000000b006fdc6aaec4fso37955787ybp.20 for ; Thu, 05 Jan 2023 09:20:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=seBi63NAd2giMsINF+6LKi4lMMZx0dq/HCcpHbmZWT8=; b=at32qFFy+UT7SZIE9z4G7cnwU8HAKOOW0dSvw5t//K1PWKVlbThAqpkjQCSlXp64hQ CMnN6g6dch7zzlnowXhm2GcG8ljF4Zag5PAC7HV+mgFdcFc7NKXfKxSLEwQXeCOUNNIb zdxmgLOFLyycbncZC1Qq1usF2zwKqAJ3iVJquFAggN4CEbqtr6egECzqAZOWWkfZZJ3X Z/NewSeVyp+t7uED0vIHE9pygkwfvoxopj1Kwaf88ttX/DhMRuS3PNDhZkPFTolvciln t7Gah1sYFs3DXvx/ndlgtBZZ8dqYLOEzAPwRjDXc3mkEpdnEVon3ClJI1f2yZiiNEOxa rfFg== X-Gm-Message-State: AFqh2kofMBJ98f5L+PV4ZXR/qlK7rysjuDzn0RenYm91i4Qdrpo2VEkD cgd0HvRm9774l8GrPB+kXxcdLd7qTssWXb98JPQ+aT/6KG2Ht+nHu/2uCJx6ZsNvycf5kOs1Z0d SJKUuMNijwZ+wngeVKkf4jBwNzA28eaKbULAuHGue4eJHxeZT7AZwgExxIQuBbUSdcGc= X-Received: by 2002:a05:690c:23c2:b0:479:f794:1e15 with SMTP id do2-20020a05690c23c200b00479f7941e15mr35731238ywb.4.1672939213301; Thu, 05 Jan 2023 09:20:13 -0800 (PST) X-Received: by 2002:a05:690c:23c2:b0:479:f794:1e15 with SMTP id do2-20020a05690c23c200b00479f7941e15mr35731213ywb.4.1672939212957; Thu, 05 Jan 2023 09:20:12 -0800 (PST) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id p16-20020a05620a057000b00704c1f4e756sm25269678qkp.14.2023.01.05.09.20.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Jan 2023 09:20:12 -0800 (PST) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: class-head parsing and CPP_TEMPLATE_ID access [PR108275] Date: Thu, 5 Jan 2023 12:20:10 -0500 Message-Id: <20230105172010.3598077-1-ppalka@redhat.com> X-Mailer: git-send-email 2.39.0.189.g4dbebc36b0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.7 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_NONE, 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: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754203957295448863?= X-GMAIL-MSGID: =?utf-8?q?1754203957295448863?= When tentatively parsing what is really an elaborated-type-specifier first as a class-specifier, we may form a CPP_TEMPLATE_ID token that later gets reused in the fallback parse if the tentative parse fails. These special tokens also capture the access checks that have been deferred while parsing the template-id. But here, we form such a token when the access check state is dk_no_check, and so the token captures no access checks. This effectively bypasses access checking for the template-id during the subsequent parse as an elaborated-type-specifier. This patch fixes this by using dk_deferred instead of dk_no_check when parsing the class name. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/108275 gcc/cp/ChangeLog: * parser.cc (cp_parser_class_head): Use dk_deferred instead of dk_no_check when parsing the class name. gcc/testsuite/ChangeLog: * g++.dg/parse/access14.C: New test. --- gcc/cp/parser.cc | 23 +++++++++++++++++------ gcc/testsuite/g++.dg/parse/access14.C | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/g++.dg/parse/access14.C diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index bfd8aeae39f..8b1658decba 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -26559,7 +26559,23 @@ cp_parser_class_head (cp_parser* parser, if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)) qualified_p = true; - push_deferring_access_checks (dk_no_check); + /* It is OK to define an inaccessible class; for example: + + class A { class B; }; + class A::B {}; + + So we want to ignore access when parsing the class name. + However, we might be tentatively parsing what is really an + elaborated-type-specifier naming a template-id, e.g. + + struct C<&D::m> c; + + In this case the tentative parse as a class-head will fail, but not + before cp_parser_template_id splices in a CPP_TEMPLATE_ID token. + Since dk_no_check is sticky, we must instead use dk_deferred so that + any such CPP_TEMPLATE_ID token created during this tentative parse + will correctly capture the access checks imposed by the template-id . */ + push_deferring_access_checks (dk_deferred); /* Determine the name of the class. Begin by looking for an optional nested-name-specifier. */ @@ -26586,11 +26602,6 @@ cp_parser_class_head (cp_parser* parser, The proposed resolution for Core Issue 180 says that wherever you see `class T::X' you should treat `X' as a type-name. - It is OK to define an inaccessible class; for example: - - class A { class B; }; - class A::B {}; - We do not know if we will see a class-name, or a template-name. We look for a class-name first, in case the class-name is a template-id; if we looked for the diff --git a/gcc/testsuite/g++.dg/parse/access14.C b/gcc/testsuite/g++.dg/parse/access14.C new file mode 100644 index 00000000000..bdbc7f6ee2b --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/access14.C @@ -0,0 +1,18 @@ +// PR c++/108275 + +struct A { + int i; +private: + int j; +}; + +template +struct B { + struct C { }; +private: + template struct D { }; +}; + +struct B<&A::j> b; // { dg-error "private" } +struct B<&A::j>::C c; // { dg-error "private" } +struct B<&A::i>::D<0> d; // { dg-error "private" }