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
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
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
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
>
>
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
@@ -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:
new file mode 100644
@@ -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{}>();
+}