From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id BC089384D14D; Thu, 20 Oct 2022 18:13:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BC089384D14D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666289621; bh=NDdxqlJnxR/IcCjkyTAZZdE08TNGdVFzErTd1CovW3E=; h=From:To:Subject:Date:From; b=PotTNBvsQ6nQIy1dV/tANcV/T1RhGLJB+AgvvB0h8YYgLNVZsnibHWXOytQhS4I7h RlCDzq9QK0lc4V1BmkSUkMHAb0Ps98vnOd8HRfQfjob0Vdi2pwQc9aBUkgLwQerT61 4kvWavxaXgSyxGqGz1V8MwI4M6NxhQTMFdZiYVag= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Patrick Palka To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-3417] c++: constraint matching, TEMPLATE_ID_EXPR, current inst X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/master X-Git-Oldrev: 1d561e1851c466a4952081caef17747781609b00 X-Git-Newrev: 14272aec22dcacbb3b937d6c5b86794faaa24620 Message-Id: <20221020181341.BC089384D14D@sourceware.org> Date: Thu, 20 Oct 2022 18:13:41 +0000 (GMT) List-Id: https://gcc.gnu.org/g:14272aec22dcacbb3b937d6c5b86794faaa24620 commit r13-3417-g14272aec22dcacbb3b937d6c5b86794faaa24620 Author: Patrick Palka Date: Thu Oct 20 14:13:10 2022 -0400 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: --- gcc/cp/pt.cc | 14 ++++++++------ gcc/testsuite/g++.dg/cpp2a/concepts-friend10.C | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 5eddad900ea..1289aabec75 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 00000000000..fc07120e112 --- /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{}); +}