From patchwork Wed Jun 2 18:39:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1486844 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: 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=OSqtoyBB; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4FwHrS2qqgz9sCD for ; Thu, 3 Jun 2021 04:39:50 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A073C3985441 for ; Wed, 2 Jun 2021 18:39:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A073C3985441 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1622659188; bh=1nKk5+QrMdu3GEPYnZbILvoZwABB6URS/NEHUjDEVAg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=OSqtoyBBg8hnhP7IX0C1iu5Ud8y/p2UhPK0MCz1qgHCs9L7bdGkQMRPk0OQI8UKbn mqZMVvEcwYfL8c/ZWOOFYmA1M5rplnae9+BAMZ1P0qQTdAbEGhX7a1hO+vPMH2ioqM M5DboclIZzyqW1z1IFelQdy6JZ7WQtLw1k8JEKQk= 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 ESMTP id 694C0388C032 for ; Wed, 2 Jun 2021 18:39:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 694C0388C032 Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-342-rhqHgMKSMSuYVkhe_VX26A-1; Wed, 02 Jun 2021 14:39:25 -0400 X-MC-Unique: rhqHgMKSMSuYVkhe_VX26A-1 Received: by mail-qk1-f200.google.com with SMTP id o14-20020a05620a130eb02902ea53a6ef80so2388256qkj.6 for ; Wed, 02 Jun 2021 11:39:25 -0700 (PDT) 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=1nKk5+QrMdu3GEPYnZbILvoZwABB6URS/NEHUjDEVAg=; b=XR8RhWgMjoN/Sl7w+o3tYDpjlypiYnyUxffB4eDmVuifjqHCaTuhyCjLlZFkoSJtQN n4/VAd48pf+QPiAtc/fU/kKIh7i/FRS76XVDa48eWAGL4oHUtFKeidMM0Zj4zjw+JCIy y0OHz6Dt/7NlbsxDPjxQdAXh2H1iNfia7/trBF4n8Nk+oNlQS+HqrTz10DFBDFyFtu7H ZbTQL/+tu9VrDpaNaVwsClZ9aPYWsiFbdKzYJVAcbbtrMf1fXMB4QpHWNcOzfTIxXJjn qAkj7Yt32v7caZig0IODU/1RV5MND/HbhSoj9PzxQzDoaQKdjK5fLJyqbiRm8pIVyVtL LR/Q== X-Gm-Message-State: AOAM530IslqiAp6SHIbPfkNZQS0GuEF+p28RHG17w9fyG09rGHYQ3XMI R6dMztNv0ITnvTfjZdjmaDAcPk/6axxVsMD7mieLnhZhozw/kwkhzSw4kcOJqCg6UIQG1ona+kW vlJXFVqF1XaRfRrOCosBcSGW00Y3f9U5KtEwrWEZjwRdfOgMyUbQQB3319n6sGYIDyIs= X-Received: by 2002:a05:620a:b:: with SMTP id j11mr29174409qki.159.1622659164628; Wed, 02 Jun 2021 11:39:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzUHlsApM3hOx09siSBAHhM2fgt7icc1uDJ9MyYzsfcdEXg0FuT4wP2NhYkTtXGU3buPsHtCA== X-Received: by 2002:a05:620a:b:: with SMTP id j11mr29174373qki.159.1622659164198; Wed, 02 Jun 2021 11:39:24 -0700 (PDT) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id z24sm336203qtm.53.2021.06.02.11.39.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Jun 2021 11:39:23 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: using-enum and access specifiers [PR100862] Date: Wed, 2 Jun 2021 14:39:14 -0400 Message-Id: <20210602183914.958300-1-ppalka@redhat.com> X-Mailer: git-send-email 2.32.0.rc2 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-16.1 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-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" When copying the enumerators imported by a class-scope using-enum declaration, we need to override current_access_specifier so that finish_member_declaration gives them the same access as the using-enum decl. The processing of a using-enum is performed after we've seen the entire definition of the class, so current_access_specifier at this point is otherwise set to the last access specifier within the class. For consistency, this patch makes build_enumerator use set_current_access_from_decl too. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk/11? PR c++/100862 gcc/cp/ChangeLog: * pt.c (set_current_access_from_decl): Move to ... * class.c (set_current_access_from_decl): ... here. (handle_using_decl): Use it to propagate the access of the using-enum decl to the copy of the imported enumerator. * cp-tree.h (set_current_access_from_decl): Declare. * decl.c (build_enumerator): Simplify using make_temp_override and set_current_access_from_decl. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/using-enum-9.C: New test. --- gcc/cp/class.c | 15 ++++++++++++ gcc/cp/cp-tree.h | 1 + gcc/cp/decl.c | 12 ++-------- gcc/cp/pt.c | 14 ------------ gcc/testsuite/g++.dg/cpp2a/using-enum-9.C | 28 +++++++++++++++++++++++ 5 files changed, 46 insertions(+), 24 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/using-enum-9.C diff --git a/gcc/cp/class.c b/gcc/cp/class.c index 354addde773..b53a4dbdd4e 100644 --- a/gcc/cp/class.c +++ b/gcc/cp/class.c @@ -207,6 +207,19 @@ static bool type_maybe_constexpr_default_constructor (tree); static bool type_maybe_constexpr_destructor (tree); static bool field_poverlapping_p (tree); +/* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ + +void +set_current_access_from_decl (tree decl) +{ + if (TREE_PRIVATE (decl)) + current_access_specifier = access_private_node; + else if (TREE_PROTECTED (decl)) + current_access_specifier = access_protected_node; + else + current_access_specifier = access_public_node; +} + /* Return a COND_EXPR that executes TRUE_STMT if this execution of the 'structor is in charge of 'structing virtual bases, or FALSE_STMT otherwise. */ @@ -1359,6 +1372,8 @@ handle_using_decl (tree using_decl, tree t) CONST_DECL_USING_P is true. */ gcc_assert (TREE_CODE (decl) == CONST_DECL); + auto cas = make_temp_override (current_access_specifier); + set_current_access_from_decl (using_decl); tree copy = copy_decl (decl); DECL_CONTEXT (copy) = t; DECL_ARTIFICIAL (copy) = true; diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index c95a820037f..b1b7e615bcc 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -8186,6 +8186,7 @@ struct atom_hasher : default_hash_traits extern bool subsumes (tree, tree); /* In class.c */ +extern void set_current_access_from_decl (tree); extern void cp_finish_injected_record_type (tree); /* in vtable-class-hierarchy.c */ diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index e7268d5ad18..fb21a3a1ae8 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -16333,17 +16333,9 @@ incremented enumerator value is too large for %")); For which case we need to make sure that the access of `S::i' matches the access of `S::E'. */ - tree saved_cas = current_access_specifier; - if (TREE_PRIVATE (TYPE_NAME (enumtype))) - current_access_specifier = access_private_node; - else if (TREE_PROTECTED (TYPE_NAME (enumtype))) - current_access_specifier = access_protected_node; - else - current_access_specifier = access_public_node; - + auto cas = make_temp_override (current_access_specifier); + set_current_access_from_decl (TYPE_NAME (enumtype)); finish_member_declaration (decl); - - current_access_specifier = saved_cas; } else pushdecl (decl); diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 31302803c62..86259e900e9 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -190,7 +190,6 @@ static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree); static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree); static bool check_specialization_scope (void); static tree process_partial_specialization (tree); -static void set_current_access_from_decl (tree); static enum template_base_result get_template_base (tree, tree, tree, tree, bool , tree *); static tree try_class_unification (tree, tree, tree, tree, bool); @@ -26431,19 +26430,6 @@ tsubst_initializer_list (tree t, tree argvec) return inits; } -/* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ - -static void -set_current_access_from_decl (tree decl) -{ - if (TREE_PRIVATE (decl)) - current_access_specifier = access_private_node; - else if (TREE_PROTECTED (decl)) - current_access_specifier = access_protected_node; - else - current_access_specifier = access_public_node; -} - /* Instantiate an enumerated type. TAG is the template type, NEWTAG is the instantiation (which should have been created with start_enum) and ARGS are the template arguments to use. */ diff --git a/gcc/testsuite/g++.dg/cpp2a/using-enum-9.C b/gcc/testsuite/g++.dg/cpp2a/using-enum-9.C new file mode 100644 index 00000000000..3e026057b40 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/using-enum-9.C @@ -0,0 +1,28 @@ +// PR c++/100862 +// { dg-do compile { target c++20 } } + +enum class fruit { orange, apple }; + +struct A { +public: + using enum fruit; +private: +}; + +struct B { +protected: + using enum fruit; +public: +}; + +struct C { +private: + using enum fruit; +public: +}; + +int main() { + A::orange, A::apple; + B::orange, B::apple; // { dg-error "protected" } + C::orange, C::apple; // { dg-error "private" } +}