From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id CCDE33858298; Tue, 14 Mar 2023 23:18:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CCDE33858298 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678835895; bh=dXQuv6LDaCexX1UEppqR1kYZGof6Uh75h+QJJ1Tl+Tc=; h=From:To:Subject:Date:From; b=nAtrjSXmix/MUgni8HzyqhkBULm+YHV9tq0Qn98Ic3S2BZO8nTssIVjkf05hPI/Wa RIjTlY2OadrmTYWsVeRUi7o7zYW56EkRCZFcFy0SljuVdye+hQA6fLy5fjFAlGJGZ+ Kr+kxTE7zd+Xz/Loje5qT0ZBNZ05lu0ul1n91tBw= 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-6677] c++: redeclaring member of constrained class template [PR96830] X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/master X-Git-Oldrev: ec62dc95c4f8776c9f4eff2a9a06f9aef6a2d98a X-Git-Newrev: cd5baeb4489b6a953abbc7f02fea457fd9ed2f83 Message-Id: <20230314231815.CCDE33858298@sourceware.org> Date: Tue, 14 Mar 2023 23:18:15 +0000 (GMT) List-Id: https://gcc.gnu.org/g:cd5baeb4489b6a953abbc7f02fea457fd9ed2f83 commit r13-6677-gcd5baeb4489b6a953abbc7f02fea457fd9ed2f83 Author: Patrick Palka Date: Tue Mar 14 19:14:29 2023 -0400 c++: redeclaring member of constrained class template [PR96830] An out-of-line definition of a member of a constrained class template needs to repeat the template's constraints, but it turns out we don't verify anywhere that the two sets of constraints match. This patch adds such a check to push_template_decl, nearby a similar consistency check for the template parameter list lengths. PR c++/96830 gcc/cp/ChangeLog: * pt.cc (push_inline_template_parms_recursive): Set TEMPLATE_PARMS_CONSTRAINTS. (push_template_decl): For an out-of-line declaration, verify constraints for each enclosing template scope match those of the original template declaratation. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-class5.C: New test. * g++.dg/cpp2a/concepts-class5a.C: New test. Diff: --- gcc/cp/pt.cc | 26 ++++++++++++++++++ gcc/testsuite/g++.dg/cpp2a/concepts-class5.C | 37 ++++++++++++++++++++++++++ gcc/testsuite/g++.dg/cpp2a/concepts-class5a.C | 38 +++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index eb153adf383..84021c21467 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -471,6 +471,8 @@ push_inline_template_parms_recursive (tree parmlist, int levels) current_template_parms = tree_cons (size_int (current_template_depth + 1), parms, current_template_parms); + TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) + = TEMPLATE_PARMS_CONSTRAINTS (parmlist); TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec, @@ -6134,6 +6136,30 @@ push_template_decl (tree decl, bool is_friend) DECL_INTERFACE_KNOWN (decl) = 1; return error_mark_node; } + + /* Check that the constraints for each enclosing template scope are + consistent with the original declarations. */ + if (flag_concepts) + { + tree decl_parms = DECL_TEMPLATE_PARMS (tmpl); + tree scope_parms = current_template_parms; + if (PRIMARY_TEMPLATE_P (tmpl)) + { + decl_parms = TREE_CHAIN (decl_parms); + scope_parms = TREE_CHAIN (scope_parms); + } + while (decl_parms) + { + if (!template_requirements_equivalent_p (decl_parms, scope_parms)) + { + error ("redeclaration of %qD with different constraints", + TPARMS_PRIMARY_TEMPLATE (TREE_VALUE (decl_parms))); + break; + } + decl_parms = TREE_CHAIN (decl_parms); + scope_parms = TREE_CHAIN (scope_parms); + } + } } gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl); diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-class5.C b/gcc/testsuite/g++.dg/cpp2a/concepts-class5.C new file mode 100644 index 00000000000..5d893d9f6a7 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-class5.C @@ -0,0 +1,37 @@ +// PR c++/96830 +// { dg-do compile { target c++20 } } + +template concept C = true; + +template requires true +struct A { + void f(); + template void g(); + + struct B; + template struct C; + + static int D; + template static int E; +}; + +template +void A::f() { } // { dg-error "different constraints" } + +template requires true +template +void A::g() { } // { dg-error "different constraints" } + +template requires (!!true) +struct A::B { }; // { dg-error "different constraints" } + +template requires true +template +struct A::C { }; // { dg-error "different constraints" } + +template +int A::D = 0; // { dg-error "different constraints" } + +template requires true +template +int A::E = 0; // { dg-error "different constraints" } diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-class5a.C b/gcc/testsuite/g++.dg/cpp2a/concepts-class5a.C new file mode 100644 index 00000000000..a6a3684c258 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-class5a.C @@ -0,0 +1,38 @@ +// PR c++/96830 +// A valid version of concepts-class5.C. +// { dg-do compile { target c++20 } } + +template concept C = true; + +template requires true +struct A { + void f(); + template void g(); + + struct B; + template struct C; + + static int D; + template static int E; +}; + +template requires true +void A::f() { } + +template requires true +template +void A::g() { } + +template requires true +struct A::B { }; + +template requires true +template +struct A::C { }; + +template requires true +int A::D = 0; + +template requires true +template +int A::E = 0;