From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id 22E893858C5F; Sun, 24 Sep 2023 18:42:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 22E893858C5F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1695580964; bh=/0zMhWNaYwp3a+eSPZKyAQErsYouuaUKThEaKPp8EjA=; h=From:To:Subject:Date:From; b=caoSByJYpQOjwJPqbJutLxNFU7UGQ5fnV+Z6os0EWnwQ9+OcuoByFtHC6f4rHS3Su gZCP454Xx2O7PtjL1QQ2Esi/ASDEwDzGa6MbJ2/b+4K4yGA+ERIWcWHPu9sUjKshHI GinOG2hkjTjkZWRyuPUDaJmcG4WaveyHzhJr3DIY= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Patrick Palka To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-7835] c++: constness of decltype of NTTP object [PR99631] X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/releases/gcc-13 X-Git-Oldrev: 9eb2f102d38697011d3069fac759b7c6e149bed4 X-Git-Newrev: b9e02590f7d35f1f8e8e95ab1f2e30f24113f551 Message-Id: <20230924184244.22E893858C5F@sourceware.org> Date: Sun, 24 Sep 2023 18:42:44 +0000 (GMT) List-Id: https://gcc.gnu.org/g:b9e02590f7d35f1f8e8e95ab1f2e30f24113f551 commit r13-7835-gb9e02590f7d35f1f8e8e95ab1f2e30f24113f551 Author: Patrick Palka Date: Tue Sep 19 08:21:05 2023 -0400 c++: constness of decltype of NTTP object [PR99631] This corrects resolving decltype of a (class) NTTP object as per [dcl.type.decltype]/1.2 and [temp.param]/6 in the type-dependent case. Note that 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, at instantiation time tsubst drops the VIEW_CONVERT_EXPR since the substituted NTTP is the already-const object created by get_template_parm_object. So in this case finish_decltype_type sees the const object, which this patch now adds special handling for. PR c++/99631 gcc/cp/ChangeLog: * semantics.cc (finish_decltype_type): For an NTTP object, return its type modulo cv-quals. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/nontype-class60.C: New test. (cherry picked from commit ddd064e3571c4a9e6258c75eba65585a07367712) Diff: --- gcc/cp/semantics.cc | 8 ++++++++ gcc/testsuite/g++.dg/cpp2a/nontype-class60.C | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 45e0b0e81d3..5ba05489cf7 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -11450,6 +11450,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 is the object type modulo 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..9e8030b09bc --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class60.C @@ -0,0 +1,18 @@ +// PR c++/99631 +// { dg-do compile { target c++20 } } + +struct A { }; + +template +void f() { + static_assert(__is_same(decltype(V), A)); +} + +template +void g() { + static_assert(__is_same(decltype(V), A)); +} + +constexpr A a; +template void f(); +template void g();