* [PATCH] c++: constness of decltype of NTTP object [PR98820]
@ 2023-09-15 17:55 Patrick Palka
2023-09-16 20:26 ` Jason Merrill
0 siblings, 1 reply; 4+ messages in thread
From: Patrick Palka @ 2023-09-15 17:55 UTC (permalink / raw)
To: gcc-patches; +Cc: jason, Patrick Palka
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] c++: constness of decltype of NTTP object [PR98820]
2023-09-15 17:55 [PATCH] c++: constness of decltype of NTTP object [PR98820] Patrick Palka
@ 2023-09-16 20:26 ` Jason Merrill
2023-09-16 22:00 ` Patrick Palka
0 siblings, 1 reply; 4+ messages in thread
From: Jason Merrill @ 2023-09-16 20:26 UTC (permalink / raw)
To: Patrick Palka, gcc-patches
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] c++: constness of decltype of NTTP object [PR98820]
2023-09-16 20:26 ` Jason Merrill
@ 2023-09-16 22:00 ` Patrick Palka
2023-09-18 2:46 ` Jason Merrill
0 siblings, 1 reply; 4+ messages in thread
From: Patrick Palka @ 2023-09-16 22:00 UTC (permalink / raw)
To: Jason Merrill; +Cc: Patrick Palka, gcc-patches
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
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] c++: constness of decltype of NTTP object [PR98820]
2023-09-16 22:00 ` Patrick Palka
@ 2023-09-18 2:46 ` Jason Merrill
0 siblings, 0 replies; 4+ messages in thread
From: Jason Merrill @ 2023-09-18 2:46 UTC (permalink / raw)
To: Patrick Palka; +Cc: gcc-patches
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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-09-18 2:46 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-15 17:55 [PATCH] c++: constness of decltype of NTTP object [PR98820] Patrick Palka
2023-09-16 20:26 ` Jason Merrill
2023-09-16 22:00 ` Patrick Palka
2023-09-18 2:46 ` Jason Merrill
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).