From: Jason Merrill <jason@redhat.com>
To: gcc-patches@gcc.gnu.org
Subject: [pushed] c++: constexpr virtual and vbase thunk
Date: Mon, 15 Nov 2021 02:45:39 -0500 [thread overview]
Message-ID: <20211115074539.3351927-1-jason@redhat.com> (raw)
C++20 allows virtual functions to be constexpr. I don't think that calling
through a pointer to a vbase subobject is supposed to work in a constant
expression, since an object with virtual bases can't be constant, but the
call shouldn't ICE.
Tested x86_64-pc-linux-gnu, applying to trunk.
gcc/cp/ChangeLog:
* constexpr.c (cxx_eval_thunk_call): Error instead of ICE
on vbase thunk to constexpr function.
gcc/testsuite/ChangeLog:
* g++.dg/cpp2a/constexpr-virtual20.C: New test.
---
gcc/cp/constexpr.c | 15 ++++++++-----
.../g++.dg/cpp2a/constexpr-virtual20.C | 22 +++++++++++++++++++
2 files changed, 32 insertions(+), 5 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/cpp2a/constexpr-virtual20.C
diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index 453007c686b..7c27131f506 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -2246,15 +2246,20 @@ cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl,
{
tree function = THUNK_TARGET (thunk_fndecl);
- /* virtual_offset is only set in the presence of virtual bases, which make
- the class non-literal, so we don't need to handle it here. */
if (THUNK_VIRTUAL_OFFSET (thunk_fndecl))
{
- gcc_assert (!DECL_DECLARED_CONSTEXPR_P (function));
if (!ctx->quiet)
{
- error ("call to non-%<constexpr%> function %qD", function);
- explain_invalid_constexpr_fn (function);
+ if (!DECL_DECLARED_CONSTEXPR_P (function))
+ {
+ error ("call to non-%<constexpr%> function %qD", function);
+ explain_invalid_constexpr_fn (function);
+ }
+ else
+ /* virtual_offset is only set for virtual bases, which make the
+ class non-literal, so we don't need to handle it here. */
+ error ("calling constexpr member function %qD through virtual "
+ "base subobject", function);
}
*non_constant_p = true;
return t;
diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-virtual20.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-virtual20.C
new file mode 100644
index 00000000000..3c411fa3fcc
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-virtual20.C
@@ -0,0 +1,22 @@
+// Test for constexpr call through vbase thunk.
+// { dg-do compile { target c++20 } }
+
+class Rep {
+public:
+ constexpr virtual int foo() { return 1; }
+};
+
+class VBase {
+public:
+ constexpr virtual int foo() { return 2; }
+};
+
+class Main : public Rep, virtual public VBase {
+public:
+ constexpr virtual int foo() { return 5; }
+};
+
+int main() {
+ Main m;
+ static_assert(static_cast<VBase*>(&m)->foo() == 5); // { dg-error "" }
+}
base-commit: adcfd2c45c3523d74279b5fcac1d7c6c34dd1382
--
2.27.0
reply other threads:[~2021-11-15 7:45 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=20211115074539.3351927-1-jason@redhat.com \
--to=jason@redhat.com \
--cc=gcc-patches@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: link
Be 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).