]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: constexpr virtual and vbase thunk
authorJason Merrill <jason@redhat.com>
Wed, 10 Nov 2021 21:42:04 +0000 (16:42 -0500)
committerJason Merrill <jason@redhat.com>
Mon, 15 Nov 2021 07:30:26 +0000 (02:30 -0500)
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.

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
gcc/testsuite/g++.dg/cpp2a/constexpr-virtual20.C [new file with mode: 0644]

index 453007c686bf0400cec2f3abe396c5d56b33ecea..7c27131f5063d5ec53c9191ebaca0547532eca93 100644 (file)
@@ -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 (file)
index 0000000..3c411fa
--- /dev/null
@@ -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 "" }
+}