public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: gcc-patches@gcc.gnu.org
Subject: [pushed] c++: anonymous union member reference [PR105452]
Date: Thu, 30 Mar 2023 18:24:54 -0400	[thread overview]
Message-ID: <20230330222454.793588-1-jason@redhat.com> (raw)

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

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 <typename T>
+struct C {
+  int i = 42;
+  union {
+    T v = i;
+    static_assert(sizeof(v) == sizeof(char), "");
+  };
+};
+
+int main() {
+  C<char> x;
+  return x.v;
+}

base-commit: a23b33a1bdeff7bc2289d9ebb7cb7b7ec0a605f5
prerequisite-patch-id: a099996174aa44b595d914cbadf18d39bfbf2afa
-- 
2.31.1


                 reply	other threads:[~2023-03-30 22:25 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230330222454.793588-1-jason@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).