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++: constness of decltype of NTTP object [PR98820]
Date: Fri, 15 Sep 2023 13:55:34 -0400	[thread overview]
Message-ID: <20230915175534.2315315-1-ppalka@redhat.com> (raw)

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

-- >8 --

This corrects decltype of a (class) NTTP object as per
[dcl.type.decltype]/1.2 and [temp.param]/6 in the type-dependent case.
In the non-dependent case (nontype-class8.C) we resolve the decltype
ahead of time, and finish_decltype_type already made sure to drop the
const VIEW_CONVERT_EXPR wrapper around the TEMPLATE_PARM_INDEX.

	PR c++/98820

gcc/cp/ChangeLog:

	* semantics.cc (finish_decltype_type): Drop cv-quals from
	the type of an NTTP object.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/nontype-class60.C: New test.
---
 gcc/cp/semantics.cc                          |  8 ++++++++
 gcc/testsuite/g++.dg/cpp2a/nontype-class60.C | 20 ++++++++++++++++++++
 2 files changed, 28 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class60.C

diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 0f7f4e87ae4..a7ae5e78a6c 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -11599,6 +11599,14 @@ finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
         case TEMPLATE_PARM_INDEX:
 	  expr = mark_type_use (expr);
           type = TREE_TYPE (expr);
+	  if (VAR_P (expr) && DECL_NTTP_OBJECT_P (expr))
+	    {
+	      /* decltype of an NTTP object is the type of the template
+		 parameter, which never has cv-quals.  */
+	      int quals = cp_type_quals (type);
+	      gcc_checking_assert (quals & TYPE_QUAL_CONST);
+	      type = cv_unqualified (type);
+	    }
           break;
 
         case ERROR_MARK:
diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class60.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class60.C
new file mode 100644
index 00000000000..fb3a61cfe10
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class60.C
@@ -0,0 +1,20 @@
+// PR c++/98820
+// { dg-do compile { target c++20 } }
+
+struct A { };
+
+template<auto V>
+void f() {
+  static_assert(__is_same(decltype(V), A));
+}
+
+template<class T, T V>
+void g() {
+  static_assert(__is_same(decltype(V), A));
+}
+
+int main() {
+  constexpr A a;
+  f<a>();
+  g<A, A{}>();
+}
-- 
2.42.0.216.gbda494f404


             reply	other threads:[~2023-09-15 17:56 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-15 17:55 Patrick Palka [this message]
2023-09-16 20:26 ` Jason Merrill
2023-09-16 22:00   ` Patrick Palka
2023-09-18  2:46     ` 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=20230915175534.2315315-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).