From: Patrick Palka Date: Thu, 20 Oct 2022 18:13:10 +0000 (-0400) Subject: c++: constraint matching, TEMPLATE_ID_EXPR, current inst X-Git-Tag: basepoints/gcc-14~3790 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=14272aec22dcacbb3b937d6c5b86794faaa24620;p=thirdparty%2Fgcc.git c++: constraint matching, TEMPLATE_ID_EXPR, current inst Here we're crashing during constraint matching for the instantiated hidden friends due to two issues with dependent substitution into a TEMPLATE_ID_EXPR that names a template from the current instantiation (as for C<1> with T=T from maybe_substitute_reqs_for): * tsubst_copy substitutes into such a TEMPLATE_DECL by looking it up from the substituted class scope. But for this lookup to work when the args are dependent, we need to substitute the class scope with entering_scope=true so that we obtain the primary template type A (which has TYPE_BINFO) instead of the implicit instantiation A (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::C). gcc/cp/ChangeLog: * pt.cc (lookup_and_finish_template_variable): Don't instantiate if the template's scope is dependent. (tsubst_copy) : Pass entering_scope=true when substituting the class scope. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-friend10.C: New test. --- diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 5eddad900eae..1289aabec758 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -10398,14 +10398,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 @@ -17229,7 +17230,8 @@ tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl) TEMPLATE_DECL with `D' as its DECL_CONTEXT. Now we have to substitute this with one having context `D'. */ - 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 000000000000..fc07120e112a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-friend10.C @@ -0,0 +1,24 @@ +// Verify we don't crash when matching constraints containing a +// TEMPLATE_ID_EXPR that names a template from the current instantiation. +// { dg-do compile { target c++20 } } + +template static constexpr bool False = false; + +template +struct A { + template static constexpr bool C = sizeof(T) > N; + friend constexpr void f(A) requires C<1> { } + friend constexpr void f(A) requires C<1> && False { } +}; + +template +struct A { + template static constexpr bool D = sizeof(T) > N; + friend constexpr void g(A) requires D<1> { } + friend constexpr void g(A) requires D<1> && False { } +}; + +int main() { + f(A{}); + g(A{}); +}