public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc r11-9326] c++: Don't reject calls through PMF during constant evaluation [PR102786] Date: Mon, 29 Nov 2021 08:49:21 +0000 (GMT) [thread overview] Message-ID: <20211129084921.D68913858438@sourceware.org> (raw) https://gcc.gnu.org/g:62e4f0e8977497e01d2c262a3bd459ab26a5aaca commit r11-9326-g62e4f0e8977497e01d2c262a3bd459ab26a5aaca Author: Jakub Jelinek <jakub@redhat.com> Date: Tue Oct 19 09:24:57 2021 +0200 c++: Don't reject calls through PMF during constant evaluation [PR102786] The following testcase incorrectly rejects the c initializer, while in the s.*a case cxx_eval_* sees .__pfn reads etc., in the s.*&S::foo case get_member_function_from_ptrfunc creates expressions which use INTEGER_CSTs with type of pointer to METHOD_TYPE. And cxx_eval_constant_expression rejects any INTEGER_CSTs with pointer type if they aren't 0. Either we'd need to make sure we defer such folding till cp_fold but the function and pfn_from_ptrmemfunc is used from lots of places, or the following patch just tries to reject only non-zero INTEGER_CSTs with pointer types if they don't point to METHOD_TYPE in the hope that all such INTEGER_CSTs with POINTER_TYPE to METHOD_TYPE are result of folding valid pointer-to-member function expressions. I don't immediately see how one could create such INTEGER_CSTs otherwise, cast of integers to PMF is rejected and would have the PMF RECORD_TYPE anyway, etc. 2021-10-19 Jakub Jelinek <jakub@redhat.com> PR c++/102786 * constexpr.c (cxx_eval_constant_expression): Don't reject INTEGER_CSTs with type POINTER_TYPE to METHOD_TYPE. * g++.dg/cpp2a/constexpr-virtual19.C: New test. (cherry picked from commit f45610a45236e97616726ca042898d6ac46a082e) Diff: --- gcc/cp/constexpr.c | 4 ++++ gcc/testsuite/g++.dg/cpp2a/constexpr-virtual19.C | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 3da7a246167..4f1f454fe1c 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -6101,6 +6101,10 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, if (TREE_CODE (t) == INTEGER_CST && TYPE_PTR_P (TREE_TYPE (t)) + /* INTEGER_CST with pointer-to-method type is only used + for a virtual method in a pointer to member function. + Don't reject those. */ + && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE && !integer_zerop (t)) { if (!ctx->quiet) diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-virtual19.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-virtual19.C new file mode 100644 index 00000000000..cb0d1bed0b1 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-virtual19.C @@ -0,0 +1,11 @@ +// PR c++/102786 +// { dg-do compile { target c++20 } } + +struct S { + virtual constexpr int foo () const { return 42; } +}; + +constexpr S s; +constexpr auto a = &S::foo; +constexpr auto b = (s.*a) (); +constexpr auto c = (s.*&S::foo) ();
reply other threads:[~2021-11-29 8:49 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=20211129084921.D68913858438@sourceware.org \ --to=jakub@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).