public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: constraint matching, TEMPLATE_ID_EXPR, current inst
@ 2022-09-15 15:58 Patrick Palka
  2022-09-16 12:08 ` Jason Merrill
  0 siblings, 1 reply; 7+ messages in thread
From: Patrick Palka @ 2022-09-15 15:58 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, Patrick Palka

Here we're crashing during constraint matching for the instantiated
hidden friends due to two issues with dependent substitution into a
TEMPLATE_ID_EXPR naming a template from the current instantiation
(as performed from maybe_substitute_reqs_for for C<3> with T=T):

  * tsubst_copy substitutes into such a TEMPLATE_DECL by looking it
    up from the substituted class scope.  But for this to not fail when
    the args are dependent, we need to pass entering_scope=true for the
    class scope substitution so that we obtain the primary template type
    A<T> (which has TYPE_BINFO) instead of the implicit instantiation
    A<T> (which doesn't).
  * lookup_and_finish_template_variable shouldn't instantiate a
    TEMPLATE_ID_EXPR that names a TEMPLATE_DECL which has more than
    one level of (unsubstituted) parameters (such as A<T>::C).

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?

gcc/cp/ChangeLog:

	* pt.cc (lookup_and_finish_template_variable): Don't
	instantiate if the template's scope is dependent.
	(tsubst_copy) <case TEMPLATE_DECL>: Pass entering_scope=true
	when substituting the class scope.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/concepts-friend10.C: New test.
---
 gcc/cp/pt.cc                                  | 14 +++++++------
 .../g++.dg/cpp2a/concepts-friend10.C          | 21 +++++++++++++++++++
 2 files changed, 29 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-friend10.C

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index db4e808adec..bfcbe0b8670 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -10475,14 +10475,15 @@ tree
 lookup_and_finish_template_variable (tree templ, tree targs,
 				     tsubst_flags_t complain)
 {
-  templ = lookup_template_variable (templ, targs);
-  if (!any_dependent_template_arguments_p (targs))
+  tree var = lookup_template_variable (templ, targs);
+  if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (templ)) == 1
+      && !any_dependent_template_arguments_p (targs))
     {
-      templ = finish_template_variable (templ, complain);
-      mark_used (templ);
+      var = finish_template_variable (var, complain);
+      mark_used (var);
     }
 
-  return convert_from_reference (templ);
+  return convert_from_reference (var);
 }
 
 /* If the set of template parameters PARMS contains a template parameter
@@ -17282,7 +17283,8 @@ tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 	     TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT.  Now we
 	     have to substitute this with one having context `D<int>'.  */
 
-	  tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
+	  tree context = tsubst_aggr_type (DECL_CONTEXT (t), args, complain,
+					   in_decl, /*entering_scope=*/true);
 	  return lookup_field (context, DECL_NAME(t), 0, false);
 	}
       else
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-friend10.C b/gcc/testsuite/g++.dg/cpp2a/concepts-friend10.C
new file mode 100644
index 00000000000..4b21a379f59
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-friend10.C
@@ -0,0 +1,21 @@
+// { dg-do compile { target c++20 } }
+// Verify we don't crash during constraint matching containing
+// a TEMPLATE_ID_EXPR referring to a template from the current
+// instantiation.
+
+template<class T>
+struct A {
+  template<int N> static constexpr bool C = sizeof(T) > N;
+  friend constexpr void f(A) requires C<3> { }
+  friend constexpr void f(A) requires C<3> || true { }
+};
+
+template<class T>
+struct A<T*> {
+  template<int N> static constexpr bool C = sizeof(T) > N;
+  friend constexpr void g(A) requires C<3> { }
+  friend constexpr void g(A) requires C<3> || true { }
+};
+
+template struct A<int>;
+template struct A<int*>;
-- 
2.37.3.662.g36f8e7ed7d


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-10-20 16:21 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-15 15:58 [PATCH] c++: constraint matching, TEMPLATE_ID_EXPR, current inst Patrick Palka
2022-09-16 12:08 ` Jason Merrill
2022-09-16 14:59   ` Patrick Palka
2022-09-16 15:05     ` Patrick Palka
2022-09-16 23:30     ` Jason Merrill
2022-09-17 14:31       ` Patrick Palka
2022-10-20 16:21         ` Jason Merrill

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).