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 r12-2221] c++: find_template_parameters and TEMPLATE_DECLs [PR101247]
Date: Fri,  9 Jul 2021 14:22:50 +0000 (GMT)	[thread overview]
Message-ID: <20210709142250.0E1F6398CC23@sourceware.org> (raw)

https://gcc.gnu.org/g:f53e66019df819f55d424cc56f8b0ea81c074b55

commit r12-2221-gf53e66019df819f55d424cc56f8b0ea81c074b55
Author: Patrick Palka <ppalka@redhat.com>
Date:   Fri Jul 9 10:20:22 2021 -0400

    c++: find_template_parameters and TEMPLATE_DECLs [PR101247]
    
    r12-1989 fixed the testcase in the PR, but unfortunately the fix is
    buggy: it breaks the case where the common template between the
    TEMPLATE_DECL t and ctx_parms is the innermost template (as in
    concepts-memtmpl5.C below).  This can be fixed by instead passing the
    TREE_TYPE of ctmpl to common_enclosing_class when ctmpl is a class
    template.
    
    But even after that's fixed, the analogous case where the innermost
    template is a partial specialization is still broken (as in
    concepts-memtmpl5a.C below), because ctmpl is always a primary template.
    
    So this patch instead takes a diferent approach that doesn't rely on
    ctx_parms at all: when looking for the template parameters of a
    TEMPLATE_DECL that are shared with the current template context, just
    walk its DECL_CONTEXT.  As long as the template is not overly general
    (e.g. we didn't pass it through most_general_template), this should give
    us exactly what we want, since if a TEMPLATE_DECL can be referred to
    from some template context then the template parameters it uses must all
    be in-scope and contained in its DECL_CONTEXT.  This effectively makes
    us treat TEMPLATE_DECLs more similarly to other _DECLs (whose DECL_CONTEXT
    we also walk).
    
            PR c++/101247
    
    gcc/cp/ChangeLog:
    
            * pt.c (any_template_parm_r) <case TEMPLATE_DECL>: Just walk the
            DECL_CONTEXT.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp2a/concepts-memtmpl4.C: Uncomment the commented out
            example, which we now handle correctly.
            * g++.dg/cpp2a/concepts-memtmpl5.C: New test.
            * g++.dg/cpp2a/concepts-memtmpl5a.C: New test.

Diff:
---
 gcc/cp/pt.c                                     | 14 +++++---------
 gcc/testsuite/g++.dg/cpp2a/concepts-memtmpl4.C  |  2 +-
 gcc/testsuite/g++.dg/cpp2a/concepts-memtmpl5.C  | 11 +++++++++++
 gcc/testsuite/g++.dg/cpp2a/concepts-memtmpl5a.C | 15 +++++++++++++++
 4 files changed, 32 insertions(+), 10 deletions(-)

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 7e56ccfc45f..dc0f0b7b58e 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -10728,15 +10728,11 @@ any_template_parm_r (tree t, void *data)
       break;
 
     case TEMPLATE_DECL:
-      {
-	/* If T is a member template that shares template parameters with
-	   ctx_parms, we need to mark all those parameters for mapping.  */
-	if (tree ctmpl = TREE_TYPE (INNERMOST_TEMPLATE_PARMS (ftpi->ctx_parms)))
-	  if (tree com = common_enclosing_class (DECL_CONTEXT (t),
-						 DECL_CONTEXT (ctmpl)))
-	    if (tree ti = CLASSTYPE_TEMPLATE_INFO (com))
-	      WALK_SUBTREE (TI_ARGS (ti));
-      }
+      /* If T is a member template that shares template parameters with
+	 ctx_parms, we need to mark all those parameters for mapping.
+	 To that end, it should suffice to just walk the DECL_CONTEXT of
+	 the template (assuming the template is not overly general).  */
+      WALK_SUBTREE (DECL_CONTEXT (t));
       break;
 
     case LAMBDA_EXPR:
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-memtmpl4.C b/gcc/testsuite/g++.dg/cpp2a/concepts-memtmpl4.C
index 625149e5025..f990ae17859 100644
--- a/gcc/testsuite/g++.dg/cpp2a/concepts-memtmpl4.C
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-memtmpl4.C
@@ -24,5 +24,5 @@ int main()
 {
   A<void>::B::f(0);
   A<void>::C<int>::f(0);
-  // A<void>::C<int>::g();
+  A<void>::C<int>::g();
 }
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-memtmpl5.C b/gcc/testsuite/g++.dg/cpp2a/concepts-memtmpl5.C
new file mode 100644
index 00000000000..3c83bb88485
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-memtmpl5.C
@@ -0,0 +1,11 @@
+// PR c++/101247
+// { dg-do compile { target concepts } }
+
+template<class T, class U> struct A {
+  template<class> static constexpr bool d = true;
+  static void g() requires d<U>;
+};
+
+int main() {
+  A<int, char>::g();
+}
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-memtmpl5a.C b/gcc/testsuite/g++.dg/cpp2a/concepts-memtmpl5a.C
new file mode 100644
index 00000000000..458f1cdf856
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-memtmpl5a.C
@@ -0,0 +1,15 @@
+// PR c++/101247
+// { dg-do compile { target concepts } }
+// A variant of concepts-memtmpl5.C that uses a partial specialization
+// of A instead of the primary template.
+
+template<class, class> struct A;
+
+template<class T, class U> requires true struct A<T, U> {
+  template<class V> static constexpr bool d = true;
+  static void g() requires d<U>;
+};
+
+int main() {
+  A<int, char>::g();
+}


                 reply	other threads:[~2021-07-09 14:22 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=20210709142250.0E1F6398CC23@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).