From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id 87B023858D1E; Tue, 29 Nov 2022 14:55:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 87B023858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669733729; bh=Zt9hhtWiPGr2cBYAufq57T9GV0cgToJOC7ZDzXesMxU=; h=From:To:Subject:Date:From; b=Gy/4o+nAxLkqZdJltkE3yEkMV5Iw8LyCYk/Mw80I7qHBWjGr46a5Ic1xOZvJg9cGW 2Vp/KddnSxH8p8OmHSToIROr5TfWOb8rotTNP7wSSdJt2Csh9aZwqFvbcsnmcOYzQp xuk87St7qt8zpS65orQEATbdzwNp3ixT2luM57PA= 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-4390] c++: explicit specialization and trailing requirements [PR107864] X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/master X-Git-Oldrev: fd8dd6c0384969170e594be34da278a072d5eb76 X-Git-Newrev: 36cabc257dfb7dd4f7625896891f6c5b195a0241 Message-Id: <20221129145529.87B023858D1E@sourceware.org> Date: Tue, 29 Nov 2022 14:55:29 +0000 (GMT) List-Id: https://gcc.gnu.org/g:36cabc257dfb7dd4f7625896891f6c5b195a0241 commit r13-4390-g36cabc257dfb7dd4f7625896891f6c5b195a0241 Author: Patrick Palka Date: Tue Nov 29 09:55:21 2022 -0500 c++: explicit specialization and trailing requirements [PR107864] Here we're crashing when using the explicit specialization of the function template g with trailing requirements ultimately because earlier decls_match (called indirectly from register_specialization) for for the explicit specialization returned false since the template has trailing requirements whereas the specialization doesn't. In r12-2230-gddd25bd1a7c8f4, we fixed a similar issue concerning template requirements instead of trailing requirements. We could extend that fix to ignore trailing requirement mismatches for explicit specializations as well, but it seems cleaner to just propagate constraints from the specialized template to the specialization when declaring an explicit specialization so that decls_match will naturally return true in this case. And it looks like determine_specialization already does this, albeit inconsistently (only when specializing a non-template member function of a class template as in cpp2a/concepts-explicit-spec4.C). So this patch makes determine_specialization consistently propagate constraints from the specialized template to the specialization, which in turn lets us get rid of the function_requirements_equivalent_p special case added by r12-2230. PR c++/107864 gcc/cp/ChangeLog: * decl.cc (function_requirements_equivalent_p): Don't check DECL_TEMPLATE_SPECIALIZATION. * pt.cc (determine_specialization): Propagate constraints when specializing a function template too. Simplify by using add_outermost_template_args. gcc/testsuite/ChangeLog: * g++.dg/concepts/explicit-spec1a.C: New test. Diff: --- gcc/cp/decl.cc | 4 +--- gcc/cp/pt.cc | 19 ++++++++----------- gcc/testsuite/g++.dg/concepts/explicit-spec1a.C | 11 +++++++++++ 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 544efdc9914..238e72f90da 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -956,9 +956,7 @@ static bool function_requirements_equivalent_p (tree newfn, tree oldfn) { /* In the concepts TS, the combined constraints are compared. */ - if (cxx_dialect < cxx20 - && (DECL_TEMPLATE_SPECIALIZATION (newfn) - <= DECL_TEMPLATE_SPECIALIZATION (oldfn))) + if (cxx_dialect < cxx20) { tree ci1 = get_constraints (oldfn); tree ci2 = get_constraints (newfn); diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index fbf498ad16a..2d8e4fdd4b5 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -2482,17 +2482,14 @@ determine_specialization (tree template_id, } /* It was a specialization of a template. */ - targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates))); - if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) - { - *targs_out = copy_node (targs); - SET_TMPL_ARGS_LEVEL (*targs_out, - TMPL_ARGS_DEPTH (*targs_out), - TREE_PURPOSE (templates)); - } - else - *targs_out = TREE_PURPOSE (templates); - return TREE_VALUE (templates); + tree tmpl = TREE_VALUE (templates); + *targs_out = add_outermost_template_args (tmpl, TREE_PURPOSE (templates)); + + /* Propagate the template's constraints to the declaration. */ + if (tsk != tsk_template) + set_constraints (decl, get_constraints (tmpl)); + + return tmpl; } /* Returns a chain of parameter types, exactly like the SPEC_TYPES, diff --git a/gcc/testsuite/g++.dg/concepts/explicit-spec1a.C b/gcc/testsuite/g++.dg/concepts/explicit-spec1a.C new file mode 100644 index 00000000000..ec678740cb8 --- /dev/null +++ b/gcc/testsuite/g++.dg/concepts/explicit-spec1a.C @@ -0,0 +1,11 @@ +// A version of explicit-spec1.C where the template g has trailing instead of +// template requirements. +// PR c++/107864 +// { dg-do compile { target concepts } } + +template concept C = __is_class(T); +struct Y { int n; } y; +template void g(T) requires C { } +int called; +template<> void g(Y) { called = 3; } +int main() { g(y); }