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 via email to