From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1734) id 8E1CB3858C33; Sat, 4 Mar 2023 18:14:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8E1CB3858C33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677953695; bh=HiCIR2lZNnYqKbWZ4T9RlS9aypDdXeXNPyCs9PsZA6U=; h=From:To:Subject:Date:From; b=HZ8OU7NaGlehZERWdNzDeZ6cBcmyxEWjRc/fkFts1LlIW0EJwBUjDIS9SROfPBTmU xi+BkYdQavP7flB1VKjnZBgq3p7llSNLlcNaqmoqtG5wzKGfrNWKbbmn5qidlZbufa rSccMfhseUE6Lglqa6qF2lleHvHbZ6CgXXa/BMiA= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Marek Polacek To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-9219] c++: variable template and targ deduction [PR108550] X-Act-Checkin: gcc X-Git-Author: Marek Polacek X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 6ab4c8759754f34f5285924652238c829960cd4c X-Git-Newrev: d6419c18056ae27741e961cd2dab9d120bfe746a Message-Id: <20230304181455.8E1CB3858C33@sourceware.org> Date: Sat, 4 Mar 2023 18:14:55 +0000 (GMT) List-Id: https://gcc.gnu.org/g:d6419c18056ae27741e961cd2dab9d120bfe746a commit r12-9219-gd6419c18056ae27741e961cd2dab9d120bfe746a Author: Marek Polacek Date: Sat Mar 4 13:14:01 2023 -0500 c++: variable template and targ deduction [PR108550] In this test, we get a bogus error because we failed to deduce the auto in constexpr auto is_pointer_v = is_pointer::value; to bool. Then ensure_literal_type_for_constexpr_object thinks the object isn't literal and an error is reported. This is another case of the interaction between tf_partial and 'auto', where the auto was not reduced so the deduction failed. In more detail: we have Wrap1() in the code and we need to perform OR -> fn_type_unification. The targ list is incomplete, so we do tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type; fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE); where TREE_TYPE (fn) is struct integral_constant (void). Then we substitute the return type, which results in tsubsting is_pointer_v. is_pointer_v is a variable template with a placeholder type: template constexpr auto is_pointer_v = is_pointer::value; so we find ourselves in lookup_and_finish_template_variable. tf_partial is still set, so finish_template_variable -> instantiate_template -> tsubst won't reduce the level of auto. But then we do mark_used which eventually calls do_auto_deduction which clears tf_partial, because we want to replace the auto now. But we hadn't reduced auto's level so this fails. And since we're not in an immediate context, we emit a hard error. I suppose that when we reach lookup_and_finish_template_variable it's probably time to clear tf_partial. (I added an assert and our testsuite doesn't have a test whereby we get to lookup_and_finish_template_variable while tf_partial is still active.) PR c++/108550 gcc/cp/ChangeLog: * pt.cc (lookup_and_finish_template_variable): Clear tf_partial. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/var-templ70.C: New test. * g++.dg/cpp1y/var-templ71.C: New test. * g++.dg/cpp1y/var-templ72.C: New test. Diff: --- gcc/cp/pt.cc | 6 ++++++ gcc/testsuite/g++.dg/cpp1y/var-templ70.C | 25 +++++++++++++++++++++++++ gcc/testsuite/g++.dg/cpp1y/var-templ71.C | 26 ++++++++++++++++++++++++++ gcc/testsuite/g++.dg/cpp1y/var-templ72.C | 27 +++++++++++++++++++++++++++ 4 files changed, 84 insertions(+) diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 10aa88f5518..19dd181c62a 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -10420,6 +10420,12 @@ lookup_and_finish_template_variable (tree templ, tree targs, templ = lookup_template_variable (templ, targs); if (!any_dependent_template_arguments_p (targs)) { + /* We may be called while doing a partial substitution, but the + type of the variable template may be auto, in which case we + will call do_auto_deduction in mark_used (which clears tf_partial) + and the auto must be properly reduced at that time for the + deduction to work. */ + complain &= ~tf_partial; templ = finish_template_variable (templ, complain); mark_used (templ); } diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ70.C b/gcc/testsuite/g++.dg/cpp1y/var-templ70.C new file mode 100644 index 00000000000..1d35c38c7cc --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ70.C @@ -0,0 +1,25 @@ +// PR c++/108550 +// { dg-do compile { target c++14 } } + +template +struct is_pointer +{ + static constexpr bool value = false; +}; + +template +struct integral_constant +{ + static constexpr T value = T1; +}; + + +template +constexpr auto is_pointer_v = is_pointer::value; + +template +integral_constant> Wrap1(); + +int main() { + static_assert(!decltype(Wrap1())::value, ""); +} diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ71.C b/gcc/testsuite/g++.dg/cpp1y/var-templ71.C new file mode 100644 index 00000000000..e0cf55230d9 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ71.C @@ -0,0 +1,26 @@ +// PR c++/108550 +// { dg-do compile { target c++14 } } + +template +struct integral_constant +{ + static constexpr T value = T1; +}; + +template +struct S { + template + static constexpr void foo(V) { } + + constexpr bool bar () const { foo(10); return false; } +}; + +template +constexpr auto is_pointer_v = S{}.bar(); + +template +integral_constant> Wrap1(); + +int main() { + static_assert(!decltype(Wrap1())::value, ""); +} diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ72.C b/gcc/testsuite/g++.dg/cpp1y/var-templ72.C new file mode 100644 index 00000000000..7426bad4a6c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ72.C @@ -0,0 +1,27 @@ +// PR c++/108550 +// { dg-do compile { target c++14 } } + +template +struct is_pointer +{ + static constexpr bool value = false; +}; + +template +struct integral_constant +{ + static constexpr T value = T1; +}; + +template +using PTR_P = is_pointer; + +template +constexpr auto is_pointer_v = PTR_P::value; + +template +integral_constant> Wrap1(); + +int main() { + static_assert(!decltype(Wrap1())::value, ""); +}