public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: jason@redhat.com, Patrick Palka <ppalka@redhat.com>
Subject: [PATCH] c++: scoped variable template-id of reference type [PR97340]
Date: Thu, 18 May 2023 13:59:27 -0400	[thread overview]
Message-ID: <20230518175927.4158045-1-ppalka@redhat.com> (raw)

lookup_and_finish_template_variable calls convert_from_reference, which
means for a variable template-id of reference type the function returns
an INDIRECT_REF instead of the bare VAR_DECL.  But the downstream logic
of two callers, tsubst_qualified_id and finish_class_member_access_expr,
expect a DECL_P result and so we end up crashing when resolving the
template-id's in the first testcase.  (Note that these two callers
eventually call convert_from_reference as appropriate, so this earlier
call seems at best redundant.)

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 be tsubst_copy_and_build (without
it we'd mishandle the second testcase).

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?

	PR c++/97340

gcc/cp/ChangeLog:

	* pt.cc (lookup_and_finish_template_variable): Don't call
	convert_from_reference.
	(tsubst_copy_and_build) <case TEMPLATE_ID_EXPR>: 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 351fc18b600..9e5b29f3099 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 00000000000..4439bee8292
--- /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<class>
+struct A {
+  template<class>
+  static constexpr const int& var = 0;
+};
+
+template<class T>
+struct B {
+  static constexpr int x1 = A<T>::template var<int>;
+  static constexpr int y1 = A<T>{}.template var<int>;
+
+  static constexpr int x2 = A<int>::template var<T>;
+  static constexpr int y2 = A<int>{}.template var<T>;
+
+  static constexpr int x3 = A<int>::template var<int>;
+  static constexpr int y3 = A<int>{}.template var<int>;
+};
+
+template struct B<int>;
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 00000000000..f9d2e6b1eed
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/var-templ81.C
@@ -0,0 +1,14 @@
+// Verify we don't ICE on an invalid use of unary * for a variable
+// template-id of reference type.
+// { dg-do compile { target c++14 } }
+
+template<class>
+static constexpr const int& var = 0;
+
+template<class T>
+struct B {
+  static constexpr int x = *var<T>; // { dg-error "argument of unary" }
+  static constexpr const int& y = *var<T>; // { dg-error "argument of unary" }
+};
+
+template struct B<int>;
-- 
2.41.0.rc0.4.g004e0f790f


             reply	other threads:[~2023-05-18 17:59 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-18 17:59 Patrick Palka [this message]
2023-05-18 18:56 ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230518175927.4158045-1-ppalka@redhat.com \
    --to=ppalka@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).