c++: constness of decltype of NTTP object [PR98820]

Message ID 20230915175534.2315315-1-ppalka@redhat.com
State New
Headers
Series c++: constness of decltype of NTTP object [PR98820] |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gcc_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gcc_check--master-aarch64 success Testing passed

Commit Message

Patrick Palka Sept. 15, 2023, 5:55 p.m. UTC
  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
  

Comments

Jason Merrill Sept. 16, 2023, 8:26 p.m. UTC | #1
On 9/15/23 13:55, Patrick Palka wrote:
> 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.

Hmm, seems like dropping the VIEW_CONVERT_EXPR is wrong in this case? 
I'm not sure why I added that.

Jason
  
Patrick Palka Sept. 16, 2023, 10 p.m. UTC | #2
On Sat, 16 Sep 2023, Jason Merrill wrote:

> On 9/15/23 13:55, Patrick Palka wrote:
> > 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.
> 
> Hmm, seems like dropping the VIEW_CONVERT_EXPR is wrong in this case? I'm not
> sure why I added that.

Ah sorry, my commit message was a bit sloppy.

In the non-dependent case we resolve the decltype ahead of time, in
which case finish_decltype_type drops the const VIEW_CONVERT_EXPR
wrapper around the TEMPLATE_PARM_INDEX, and the latter has the
desired non-const type.

In the type-dependent case, tsubst drops the VIEW_CONVERT_EXPR
because the substituted class NTTP is the already const object
created by get_template_parm_object.  So finish_decltype_type
at instantiation time sees the bare const object, which this patch
now adds special handling for.

So we need to continue dropping the VIEW_CONVERT_EXPR to handle the
non-dependent case.

> 
> Jason
> 
>
  
Jason Merrill Sept. 18, 2023, 2:46 a.m. UTC | #3
On 9/16/23 18:00, Patrick Palka wrote:
> On Sat, 16 Sep 2023, Jason Merrill wrote:
> 
>> On 9/15/23 13:55, Patrick Palka wrote:
>>> 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.
>>
>> Hmm, seems like dropping the VIEW_CONVERT_EXPR is wrong in this case? I'm not
>> sure why I added that.
> 
> Ah sorry, my commit message was a bit sloppy.
> 
> In the non-dependent case we resolve the decltype ahead of time, in
> which case finish_decltype_type drops the const VIEW_CONVERT_EXPR
> wrapper around the TEMPLATE_PARM_INDEX, and the latter has the
> desired non-const type.
> 
> In the type-dependent case, tsubst drops the VIEW_CONVERT_EXPR
> because the substituted class NTTP is the already const object
> created by get_template_parm_object.  So finish_decltype_type
> at instantiation time sees the bare const object, which this patch
> now adds special handling for.
> 
> So we need to continue dropping the VIEW_CONVERT_EXPR to handle the
> non-dependent case.

Aha.  The patch is OK, then.

Jason
  

Patch

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{}>();
+}