From ef8926d23a458560b8e9be1a76cf29ddcb87ec2a Mon Sep 17 00:00:00 2001 From: Patrick Palka Date: Fri, 19 May 2023 09:40:16 -0400 Subject: [PATCH] c++: scoped variable template-id of reference type [PR97340] lookup_and_finish_template_variable calls convert_from_reference, which means for a variable template-id of reference type the function wraps the corresponding VAR_DECL in an INDIRECT_REF. But the downstream logic of two callers, tsubst_qualified_id and finish_class_member_access_expr, expect a DECL_P result and this unexpected INDIRECT_REF leads to an ICE resolving such a (dependently scoped) template-id as in the first testcase. (Note these two callers eventually call convert_from_reference on the result anyway, so calling it earlier seems redundant in this case.) This patch fixes this by pulling out the convert_from_reference call from lookup_and_finish_template_variable and into the callers that actually need it, which turns out to only be tsubst_copy_and_build (if we got rid of the call there we'd mishandle the second testcase). PR c++/97340 gcc/cp/ChangeLog: * pt.cc (lookup_and_finish_template_variable): Don't call convert_from_reference. (tsubst_copy_and_build) : Call convert_from_reference on the result of lookup_and_finish_template_variable. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/var-templ80.C: New test. * g++.dg/cpp1y/var-templ81.C: New test. --- gcc/cp/pt.cc | 3 ++- gcc/testsuite/g++.dg/cpp1y/var-templ80.C | 22 ++++++++++++++++++++++ gcc/testsuite/g++.dg/cpp1y/var-templ81.C | 14 ++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ80.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ81.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 696233e9f07e..7fb3e75bceb1 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -10394,7 +10394,7 @@ lookup_and_finish_template_variable (tree templ, tree targs, complain &= ~tf_partial; var = finish_template_variable (var, complain); mark_used (var); - return convert_from_reference (var); + return var; } /* If the set of template parameters PARMS contains a template parameter @@ -20462,6 +20462,7 @@ tsubst_copy_and_build (tree t, { tree r = lookup_and_finish_template_variable (templ, targs, complain); + r = convert_from_reference (r); r = maybe_wrap_with_location (r, EXPR_LOCATION (t)); RETURN (r); } diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ80.C b/gcc/testsuite/g++.dg/cpp1y/var-templ80.C new file mode 100644 index 000000000000..4439bee82924 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ80.C @@ -0,0 +1,22 @@ +// PR c++/97340 +// { dg-do compile { target c++14 } } + +template +struct A { + template + static constexpr const int& var = 0; +}; + +template +struct B { + static constexpr int x1 = A::template var; + static constexpr int y1 = A{}.template var; + + static constexpr int x2 = A::template var; + static constexpr int y2 = A{}.template var; + + static constexpr int x3 = A::template var; + static constexpr int y3 = A{}.template var; +}; + +template struct B; diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ81.C b/gcc/testsuite/g++.dg/cpp1y/var-templ81.C new file mode 100644 index 000000000000..3efe7797d118 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ81.C @@ -0,0 +1,14 @@ +// Verify we don't ICE on an invalid dereference of a variable +// template-id of reference type. +// { dg-do compile { target c++14 } } + +template +static constexpr const int& var = 0; + +template +struct B { + static constexpr int x = *var; // { dg-error "argument of unary" } + static constexpr const int& y = *var; // { dg-error "argument of unary" } +}; + +template struct B; -- 2.47.2