From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by sourceware.org (Postfix) with ESMTP id 334FA3857C70 for ; Mon, 18 Jan 2021 05:31:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 334FA3857C70 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-414-9WfAlLXhOBOQIEcDwoER8Q-1; Mon, 18 Jan 2021 00:31:14 -0500 X-MC-Unique: 9WfAlLXhOBOQIEcDwoER8Q-1 Received: by mail-qv1-f69.google.com with SMTP id c2so15441924qvs.12 for ; Sun, 17 Jan 2021 21:31:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ktnxseJ5Oir8b76Wxvb60QVTHG99Pem7a7PbXrcZxZw=; b=qD7c7YqaAxK2IgFiHZgKu1Ernz6dvp7x0Xa4v9MUmPDVtpF9e2rXS/cnJcLuzr8pS0 KLZUUhH2B0NrRsbZEzo2YZlouUFSrTj8H/xj3AGvSPcwpOnmImVReLhZyfuwxXVcsnCO 0ZSQefI2ssKiwBRUQuLeGjDW7makmY56qXxAqyFsD+dk+9PbYU2llcXtpAAxSL03haUF RkNYeEb0+ajpTW8ic8otCxYnabYVrCHlae4JpGZqKT/1cXK/grcTmvzzZmm94cCZEHHd /BxMLAbbgW2t2dUS0dlLqSmhqgYqapwD77DEelg4lGLsulLg7bePhEphVU6W1W+rWix+ kajg== X-Gm-Message-State: AOAM533nsJC9R1MXHlaiRldJHmUQ7PXocBWwtrwEhLwmlWWNFexIzzfY 6ez5R6Drjp/YQl7zo8G4q+T9ThydU5NO7GbHv342LSZdvUMkRyIjR/aSy8FlJpDYRzXpjVvSpvY UVUPMNo1peJ7QMfsmwA60y+tc4PEihdtThyc+UfvIT/HWc5WevLD+Faep0AB/eNWw/30= X-Received: by 2002:a0c:83e1:: with SMTP id k88mr23057309qva.60.1610947873436; Sun, 17 Jan 2021 21:31:13 -0800 (PST) X-Google-Smtp-Source: ABdhPJxl8rE+0QPNpM5eZ4Ztp352qSBJD6pdXfd7cZyXASX+S1zC49kIIgW1UPGDWP9AnqCOgLgiXw== X-Received: by 2002:a0c:83e1:: with SMTP id k88mr23057293qva.60.1610947873107; Sun, 17 Jan 2021 21:31:13 -0800 (PST) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id a22sm10014817qkl.121.2021.01.17.21.31.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Jan 2021 21:31:12 -0800 (PST) From: Patrick Palka To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Defer access checking when processing bases [PR82613] Date: Mon, 18 Jan 2021 00:31:10 -0500 Message-Id: <20210118053110.2306043-1-ppalka@redhat.com> X-Mailer: git-send-email 2.30.0.155.g66e871b664 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" X-Spam-Status: No, score=-16.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_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-List-Received-Date: Mon, 18 Jan 2021 05:31:17 -0000 When parsing the base-clause of a class declaration, we need to defer access checking until the entire base-clause has been seen, so that access can be properly checked relative to the scope of the class with all its bases attached. This allows us to accept the declaration of struct D from Example 2 of [class.access.general] (access12.C below). Similarly when substituting into the base-clause of a class template, which is the subject of PR82613. Bootstrapped and regtested on x86_64-pc-linxu-gnu, does this look OK for trunk? gcc/cp/ChangeLog: PR c++/82613 * parser.c (cp_parser_class_head): Defer access checking when parsing the base-clause until all bases are seen and attached to the class type. * pt.c (instantiate_class_template): Likewise when substituting into dependent bases. gcc/testsuite/ChangeLog: PR c++/82613 * g++.dg/parse/access12.C: New test. * g++.dg/template/access35.C: New test. --- gcc/cp/parser.c | 30 ++++++++++++++---------- gcc/cp/pt.c | 16 ++++++------- gcc/testsuite/g++.dg/parse/access12.C | 24 +++++++++++++++++++ gcc/testsuite/g++.dg/template/access35.C | 26 ++++++++++++++++++++ 4 files changed, 75 insertions(+), 21 deletions(-) create mode 100644 gcc/testsuite/g++.dg/parse/access12.C create mode 100644 gcc/testsuite/g++.dg/template/access35.C diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 88c6e2648cb..57843cd65c6 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -25578,19 +25578,11 @@ cp_parser_class_head (cp_parser* parser, is valid. */ - /* Get the list of base-classes, if there is one. */ + /* Get the list of base-classes, if there is one. Defer access checking + until the entire list has been seen, as per [class.access.general]. */ + push_deferring_access_checks (dk_deferred); if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) - { - /* PR59482: enter the class scope so that base-specifiers are looked - up correctly. */ - if (type) - pushclass (type); - bases = cp_parser_base_clause (parser); - /* PR59482: get out of the previously pushed class scope so that the - subsequent pops pop the right thing. */ - if (type) - popclass (); - } + bases = cp_parser_base_clause (parser); else bases = NULL_TREE; @@ -25599,6 +25591,20 @@ cp_parser_class_head (cp_parser* parser, if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) xref_basetypes (type, bases); + /* Now that all bases have been seen and attached to the class, check + accessibility of the types named in the base-clause. This must be + done relative to the class scope, so that we accept e.g. + + class A { protected: struct B {}; }; + struct C : A::B, A {}; // OK: A::B is accessible from C + + as per [class.access.general]. */ + if (type) + pushclass (type); + pop_to_parent_deferring_access_checks (); + if (type) + popclass (); + done: /* Leave the scope given by the nested-name-specifier. We will enter the class scope itself while processing the members. */ diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index a82324d23be..d5d3d2fd040 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -11825,17 +11825,14 @@ instantiate_class_template_1 (tree type) || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type))); base_list = NULL_TREE; + /* Defer access checking while we substitute into the types named in + the base-clause. */ + push_deferring_access_checks (dk_deferred); if (BINFO_N_BASE_BINFOS (pbinfo)) { tree pbase_binfo; - tree pushed_scope; int i; - /* We must enter the scope containing the type, as that is where - the accessibility of types named in dependent bases are - looked up from. */ - pushed_scope = push_scope (CP_TYPE_CONTEXT (type)); - /* Substitute into each of the bases to determine the actual basetypes. */ for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++) @@ -11877,9 +11874,6 @@ instantiate_class_template_1 (tree type) /* The list is now in reverse order; correct that. */ base_list = nreverse (base_list); - - if (pushed_scope) - pop_scope (pushed_scope); } /* Now call xref_basetypes to set up all the base-class information. */ @@ -11897,6 +11891,10 @@ instantiate_class_template_1 (tree type) class, except we also need to push the enclosing classes. */ push_nested_class (type); + /* Now check accessibility of the types named in its base-clause, + relative to the scope of the class. */ + pop_to_parent_deferring_access_checks (); + /* Now members are processed in the order of declaration. */ for (member = CLASSTYPE_DECL_LIST (pattern); member; member = TREE_CHAIN (member)) diff --git a/gcc/testsuite/g++.dg/parse/access12.C b/gcc/testsuite/g++.dg/parse/access12.C new file mode 100644 index 00000000000..6963e0eb089 --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/access12.C @@ -0,0 +1,24 @@ +// Example 2 of [class.access.general] +// { dg-do compile } + +class A { + typedef int I; // private member + I f(); + friend I g(I); + static I x; + template struct Q; + template friend struct R; +protected: + struct B { }; +}; + +A::I A::f() { return 0; } +A::I g(A::I p = A::x); +A::I g(A::I p) { return 0; } +A::I A::x = 0; +// FIXME: We reject these two declarations because access checking of A::I +// is not performed in the scope of the class being declared. +// template struct A::Q { }; +// template struct R { }; + +struct D: A::B, A { }; diff --git a/gcc/testsuite/g++.dg/template/access35.C b/gcc/testsuite/g++.dg/template/access35.C new file mode 100644 index 00000000000..dcd2c229b35 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/access35.C @@ -0,0 +1,26 @@ +// PR c++/82613 +// { dg-do compile } + +struct Empty {}; + +template +struct B; + +struct A +{ + friend struct B; + +private: + typedef Empty BaseForB; +}; + +template +struct B : T::BaseForB +{ +}; + +int main() +{ + B test; + return 0; +} -- 2.30.0.155.g66e871b664