From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id D3A323857C74; Wed, 6 Oct 2021 14:15:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D3A323857C74 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 r11-9081] c++: constrained variable template issues [PR98486] X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 59c6831682dfa8bec2b5a62bdc85739924970808 X-Git-Newrev: 729cf2ea979f396e706625b1669087d5920b8c2a Message-Id: <20211006141529.D3A323857C74@sourceware.org> Date: Wed, 6 Oct 2021 14:15:29 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 06 Oct 2021 14:15:29 -0000 https://gcc.gnu.org/g:729cf2ea979f396e706625b1669087d5920b8c2a commit r11-9081-g729cf2ea979f396e706625b1669087d5920b8c2a Author: Patrick Palka Date: Thu Sep 16 15:03:55 2021 -0400 c++: constrained variable template issues [PR98486] This fixes some issues with constrained variable templates: - Constraints aren't checked when explicitly specializing a variable template. - Constraints aren't attached to a static data member template at parse time. - Constraints don't get propagated when (partially) instantiating a static data member template, so we need to make sure to look up constraints using the most general template during satisfaction. PR c++/98486 gcc/cp/ChangeLog: * constraint.cc (get_normalized_constraints_from_decl): Always look up constraints using the most general template. * decl.c (grokdeclarator): Set constraints on a static data member template. * pt.c (determine_specialization): Check constraints on a variable template. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-var-templ1.C: New test. * g++.dg/cpp2a/concepts-var-templ1a.C: New test. * g++.dg/cpp2a/concepts-var-templ1b.C: New test. (cherry picked from commit 2e2e65a46d2674bed53afd211493876ee2b79453) Diff: --- gcc/cp/constraint.cc | 8 +++++--- gcc/cp/decl.c | 11 +++++++++++ gcc/cp/pt.c | 3 ++- gcc/testsuite/g++.dg/cpp2a/concepts-var-templ1.C | 9 +++++++++ gcc/testsuite/g++.dg/cpp2a/concepts-var-templ1a.C | 14 ++++++++++++++ gcc/testsuite/g++.dg/cpp2a/concepts-var-templ1b.C | 15 +++++++++++++++ 6 files changed, 56 insertions(+), 4 deletions(-) diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc index 4af7018f1a5..76802964c70 100644 --- a/gcc/cp/constraint.cc +++ b/gcc/cp/constraint.cc @@ -933,13 +933,15 @@ get_normalized_constraints_from_decl (tree d, bool diag = false) tmpl = most_general_template (tmpl); } + d = tmpl ? tmpl : decl; + /* If we're not diagnosing errors, use cached constraints, if any. */ if (!diag) - if (tree *p = hash_map_safe_get (normalized_map, tmpl)) + if (tree *p = hash_map_safe_get (normalized_map, d)) return *p; tree norm = NULL_TREE; - if (tree ci = get_constraints (decl)) + if (tree ci = get_constraints (d)) { push_nested_class_guard pncs (DECL_CONTEXT (d)); @@ -951,7 +953,7 @@ get_normalized_constraints_from_decl (tree d, bool diag = false) } if (!diag) - hash_map_safe_put (normalized_map, tmpl, norm); + hash_map_safe_put (normalized_map, d, norm); return norm; } diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index f21db4d560d..a3c06fdae8e 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -13831,6 +13831,17 @@ grokdeclarator (const cp_declarator *declarator, if (declspecs->gnu_thread_keyword_p) SET_DECL_GNU_TLS_P (decl); } + + /* Set the constraints on the declaration. */ + bool memtmpl = (processing_template_decl + > template_class_depth (current_class_type)); + if (memtmpl) + { + tree tmpl_reqs + = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms); + tree ci = build_constraints (tmpl_reqs, NULL_TREE); + set_constraints (decl, ci); + } } else { diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 46ad819239e..31bd51b7c9d 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -2207,7 +2207,8 @@ determine_specialization (tree template_id, targs = coerce_template_parms (parms, explicit_targs, fns, tf_warning_or_error, /*req_all*/true, /*use_defarg*/true); - if (targs != error_mark_node) + if (targs != error_mark_node + && constraints_satisfied_p (fns, targs)) templates = tree_cons (targs, fns, templates); } else for (lkp_iterator iter (fns); iter; ++iter) diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-var-templ1.C b/gcc/testsuite/g++.dg/cpp2a/concepts-var-templ1.C new file mode 100644 index 00000000000..80b48ba3a3d --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-var-templ1.C @@ -0,0 +1,9 @@ +// PR c++/98486 +// { dg-do compile { target c++20 } } + +template concept C = __is_same(T, U); + +template> int v; + +template<> int v; +template<> int v; // { dg-error "match" } diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-var-templ1a.C b/gcc/testsuite/g++.dg/cpp2a/concepts-var-templ1a.C new file mode 100644 index 00000000000..b12d37d8b7e --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-var-templ1a.C @@ -0,0 +1,14 @@ +// PR c++/98486 +// { dg-do compile { target c++20 } } + +template concept C = __is_same(T, U); + +struct A { + template> static int v; +}; + +template<> int A::v; +template<> int A::v; // { dg-error "match" } + +int x = A::v; +int y = A::v; // { dg-error "invalid" } diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-var-templ1b.C b/gcc/testsuite/g++.dg/cpp2a/concepts-var-templ1b.C new file mode 100644 index 00000000000..37d7f0fc654 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-var-templ1b.C @@ -0,0 +1,15 @@ +// PR c++/98486 +// { dg-do compile { target c++20 } } + +template concept C = __is_same(T, U); + +template +struct A { + template> static int v; +}; + +template<> template<> int A::v; +template<> template<> int A::v; // { dg-error "match" } + +int x = A::v; +int y = A::v; // { dg-error "invalid" }