From patchwork Thu Mar 30 22:24:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1763527 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.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=fgb7rE2h; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4PndKJ1dnbz1yYr for ; Fri, 31 Mar 2023 09:25:22 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7E1B03858404 for ; Thu, 30 Mar 2023 22:25:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7E1B03858404 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680215120; bh=0Ii1dmJPNvKaGvMdu1iabN7Sb4DVyniHvSN+2F/sW6s=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=fgb7rE2haG79r6qbaZ3BBG9En6W+hJE3ZjI0Wo/rSaM7p1fxlO+cTLTdh87RmcVck G7oq1T+wMMp2Z2dghzS4eZHqm9C4FV4XkfyhH/dRP3r15417Av2VTNlp76eKKbmZts d5usBxu9Qocl3V8i9iguiRfDuutYWDlOFAzQnvzY= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 63A363858404 for ; Thu, 30 Mar 2023 22:25:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 63A363858404 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-627-5xwmuJGtMnqx3ZKDVGS90A-1; Thu, 30 Mar 2023 18:24:59 -0400 X-MC-Unique: 5xwmuJGtMnqx3ZKDVGS90A-1 Received: by mail-qv1-f69.google.com with SMTP id oo15-20020a056214450f00b005a228adfcefso9044177qvb.2 for ; Thu, 30 Mar 2023 15:24:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680215098; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=0Ii1dmJPNvKaGvMdu1iabN7Sb4DVyniHvSN+2F/sW6s=; b=FdCJYJHQBf4jZgUf+wGxFRViFXObFLW3vnOdHMx94KUok7id5MgeOPjGRdcDpd0MLD 3DU5Zo3Veop6MjuxZS5g9e/2VzE3D9s5mlNVit1p3ILH8EY3ZZeOOoYAmSHHXcQthwM2 z6WWDX+30FYSM1PtxcoUI7um6jWwKdWUaNGjTxcqpV71Lk/2o1/YhFQX9BOufe153X9i uJLRYHgXDPKJCr07nWxlij5G+0MFuElfDU9eCBpnotECcK+tzYnDnczoyVRrcMi7DVQ7 3m9W4SezjZgoenTipXCtVOxiGiWvgyjKWfvs6obQW6AJSO9EUzjEyVdLAb+6a6nLe1u7 imsw== X-Gm-Message-State: AAQBX9daFyH3pbEKkXyTf9aj2viToRFC2GXNJOjtr/QQLSxGSmNvNmvp idftSn/YSplaY+0Awi68WyOanURICiXTCjLTdmemlBVuYO59Ff5Xwqz43/jbzw2J5rn+lRz7Wib IpzP9JItfIgsiluHnWUXAbgiKzKaqi7JxdGtReRVRIYOEW5636h3VoKoZKudIpSD+ljCVf2CYsA == X-Received: by 2002:a05:6214:234b:b0:535:5492:b427 with SMTP id hu11-20020a056214234b00b005355492b427mr39034057qvb.28.1680215098555; Thu, 30 Mar 2023 15:24:58 -0700 (PDT) X-Google-Smtp-Source: AKy350ZsZUCsjFDFZ1tMLqqtLqdD2JudgI20ddHJgMTImMk4RMQN0IjslCWmLZU7z4HmGg7w7DrUzw== X-Received: by 2002:a05:6214:234b:b0:535:5492:b427 with SMTP id hu11-20020a056214234b00b005355492b427mr39034025qvb.28.1680215098046; Thu, 30 Mar 2023 15:24:58 -0700 (PDT) Received: from jason.cygnus.com (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id s11-20020a05621412cb00b005dd8b9345efsm138615qvv.135.2023.03.30.15.24.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 15:24:57 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: anonymous union member reference [PR105452] Date: Thu, 30 Mar 2023 18:24:54 -0400 Message-Id: <20230330222454.793588-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.4 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- While parsing the anonymous union, we don't yet know that it's an anonymous union, so we build the reference to 'v' in the static_assert relative to the union type. But at instantiation time we know it's an anonymous union, so we need to avoid trying to check access for 'v' in the union again; the simplest approach seemed to be to make accessible_p step out to the containing class. While looking at this I also noticed that we were having trouble with DMI in an anonymous union referring to members of the containing class; there we just need to give current_class_ptr the right type. PR c++/105452 gcc/cp/ChangeLog: * search.cc (type_context_for_name_lookup): New. (accessible_p): Handle anonymous union. * init.cc (maybe_instantiate_nsdmi_init): Use type_context_for_name_lookup. * parser.cc (cp_parser_class_specifier): Likewise. * cp-tree.h (type_context_for_name_lookup): Declare. gcc/testsuite/ChangeLog: * g++.dg/lookup/anon8.C: New test. --- gcc/cp/cp-tree.h | 1 + gcc/cp/init.cc | 2 +- gcc/cp/parser.cc | 5 +++-- gcc/cp/search.cc | 23 +++++++++++++++++++++++ gcc/testsuite/g++.dg/lookup/anon8.C | 16 ++++++++++++++++ 5 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/lookup/anon8.C base-commit: a23b33a1bdeff7bc2289d9ebb7cb7b7ec0a605f5 prerequisite-patch-id: a099996174aa44b595d914cbadf18d39bfbf2afa diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 0e37d4043d0..d450b3d5b78 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7534,6 +7534,7 @@ extern int at_function_scope_p (void); extern bool at_class_scope_p (void); extern bool at_namespace_scope_p (void); extern tree context_for_name_lookup (tree); +extern tree type_context_for_name_lookup (tree); extern tree lookup_conversions (tree); extern tree binfo_from_vbase (tree); extern tree binfo_for_vbase (tree, tree); diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc index c5a55dae563..9571d18170e 100644 --- a/gcc/cp/init.cc +++ b/gcc/cp/init.cc @@ -597,7 +597,7 @@ maybe_instantiate_nsdmi_init (tree member, tsubst_flags_t complain) DECL_INSTANTIATING_NSDMI_P (member) = 1; bool pushed = false; - tree ctx = DECL_CONTEXT (member); + tree ctx = type_context_for_name_lookup (member); processing_template_decl_sentinel ptds (/*reset*/false); if (!currently_open_class (ctx)) diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index e8efc32f2c2..a6341b98af2 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -26439,11 +26439,12 @@ cp_parser_class_specifier (cp_parser* parser) /* Now parse any NSDMIs. */ FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl) { - if (class_type != DECL_CONTEXT (decl)) + tree ctx = type_context_for_name_lookup (decl); + if (class_type != ctx) { if (pushed_scope) pop_scope (pushed_scope); - class_type = DECL_CONTEXT (decl); + class_type = ctx; pushed_scope = push_scope (class_type); } inject_this_parameter (class_type, TYPE_UNQUALIFIED); diff --git a/gcc/cp/search.cc b/gcc/cp/search.cc index e472a97679d..3f521b3bd72 100644 --- a/gcc/cp/search.cc +++ b/gcc/cp/search.cc @@ -485,6 +485,25 @@ context_for_name_lookup (tree decl) return context; } +/* Like the above, but always return a type, because it's simpler for member + handling to refer to the anonymous aggr rather than a function. */ + +tree +type_context_for_name_lookup (tree decl) +{ + tree context = DECL_P (decl) ? DECL_CONTEXT (decl) : decl; + gcc_checking_assert (CLASS_TYPE_P (context)); + + while (context && TYPE_P (context) && ANON_AGGR_TYPE_P (context)) + { + tree next = TYPE_CONTEXT (context); + if (!TYPE_P (next)) + break; + context = next; + } + return context; +} + /* Returns true iff DECL is declared in TYPE. */ static bool @@ -881,6 +900,10 @@ accessible_p (tree type, tree decl, bool consider_local_p) else otype = type; + /* Anonymous unions don't have their own access. */ + if (ANON_AGGR_TYPE_P (type)) + type = type_context_for_name_lookup (type); + /* [class.access.base] A member m is accessible when named in class N if diff --git a/gcc/testsuite/g++.dg/lookup/anon8.C b/gcc/testsuite/g++.dg/lookup/anon8.C new file mode 100644 index 00000000000..80124caba63 --- /dev/null +++ b/gcc/testsuite/g++.dg/lookup/anon8.C @@ -0,0 +1,16 @@ +// PR c++/105452 +// { dg-do compile { target c++11 } } + +template +struct C { + int i = 42; + union { + T v = i; + static_assert(sizeof(v) == sizeof(char), ""); + }; +}; + +int main() { + C x; + return x.v; +}