public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Patrick Palka <ppalka@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r15-57] c++/modules: imported spec befriending class tmpl [PR114889]
Date: Tue, 30 Apr 2024 01:28:31 +0000 (GMT)	[thread overview]
Message-ID: <20240430012831.B17F23858D35@sourceware.org> (raw)

https://gcc.gnu.org/g:22b20ac6c6aead2d3f36c413a77dd0b80adfec39

commit r15-57-g22b20ac6c6aead2d3f36c413a77dd0b80adfec39
Author: Patrick Palka <ppalka@redhat.com>
Date:   Mon Apr 29 21:27:59 2024 -0400

    c++/modules: imported spec befriending class tmpl [PR114889]
    
    When adding to CLASSTYPE_BEFRIENDING_CLASSES as part of installing an
    imported class definition, we need to look through TEMPLATE_DECL like
    make_friend_class does.
    
    Otherwise in the below testcase we won't add _Hashtable<int, int> to
    CLASSTYPE_BEFRIENDING_CLASSES of _Map_base, which leads to a bogus
    access check failure for _M_hash_code.
    
            PR c++/114889
    
    gcc/cp/ChangeLog:
    
            * module.cc (trees_in::read_class_def): Look through
            TEMPLATE_DECL when adding to CLASSTYPE_BEFRIENDING_CLASSES.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/modules/friend-8_a.H: New test.
            * g++.dg/modules/friend-8_b.C: New test.
    
    Reviewed-by: Jason Merrill <jason@redhat.com>

Diff:
---
 gcc/cp/module.cc                          |  2 ++
 gcc/testsuite/g++.dg/modules/friend-8_a.H | 23 +++++++++++++++++++++++
 gcc/testsuite/g++.dg/modules/friend-8_b.C |  9 +++++++++
 3 files changed, 34 insertions(+)

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index c35e70b8cb8..3bf863e15d4 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -12498,6 +12498,8 @@ trees_in::read_class_def (tree defn, tree maybe_template)
 	  for (; friend_classes; friend_classes = TREE_CHAIN (friend_classes))
 	    {
 	      tree f = TREE_VALUE (friend_classes);
+	      if (TREE_CODE (f) == TEMPLATE_DECL)
+		f = TREE_TYPE (f);
 
 	      if (CLASS_TYPE_P (f))
 		{
diff --git a/gcc/testsuite/g++.dg/modules/friend-8_a.H b/gcc/testsuite/g++.dg/modules/friend-8_a.H
new file mode 100644
index 00000000000..b07ea25adfb
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/friend-8_a.H
@@ -0,0 +1,23 @@
+// PR c++/114889
+// { dg-additional-options "-fmodule-header" }
+// { dg-module-cmi {} }
+
+template<class, class>
+struct _Hashtable;
+
+template<class _Key, class _Val>
+struct _Map_base {
+  void f() {
+    _Hashtable<_Key, _Val> __h;
+    __h._M_hash_code(0);
+  }
+};
+
+template<class _Key, class _Value>
+struct _Hashtable {
+  template<class, class> friend struct _Map_base;
+protected:
+  void _M_hash_code(int);
+};
+
+inline _Hashtable<int, int> m;
diff --git a/gcc/testsuite/g++.dg/modules/friend-8_b.C b/gcc/testsuite/g++.dg/modules/friend-8_b.C
new file mode 100644
index 00000000000..b04280bc91a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/friend-8_b.C
@@ -0,0 +1,9 @@
+// PR c++/114889
+// { dg-additional-options "-fmodules-ts" }
+
+import "friend-8_a.H";
+
+int main() {
+  _Map_base<int, int> m;
+  m.f();
+}

                 reply	other threads:[~2024-04-30  1:28 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=20240430012831.B17F23858D35@sourceware.org \
    --to=ppalka@gcc.gnu.org \
    --cc=gcc-cvs@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).