public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Patrick Palka <ppalka@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-4137] c++: constness of decltype of NTTP object [PR99631] Date: Tue, 19 Sep 2023 12:21:14 +0000 (GMT) [thread overview] Message-ID: <20230919122114.9489C3858D3C@sourceware.org> (raw) https://gcc.gnu.org/g:ddd064e3571c4a9e6258c75eba65585a07367712 commit r14-4137-gddd064e3571c4a9e6258c75eba65585a07367712 Author: Patrick Palka <ppalka@redhat.com> 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. 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 4109ac33654..80ef1364e33 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -11606,6 +11606,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<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)); +} + +constexpr A a; +template void f<a>(); +template void g<A, A{}>();
reply other threads:[~2023-09-19 12:21 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20230919122114.9489C3858D3C@sourceware.org \ --to=ppalka@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).