From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2122) id E3D493858D39; Mon, 15 Nov 2021 07:47:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E3D493858D39 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jason Merrill To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-5252] c++: constexpr virtual and vbase thunk X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/master X-Git-Oldrev: adcfd2c45c3523d74279b5fcac1d7c6c34dd1382 X-Git-Newrev: 267318a2857a42922c3ca033dac4690172b17683 Message-Id: <20211115074740.E3D493858D39@sourceware.org> Date: Mon, 15 Nov 2021 07:47:40 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 15 Nov 2021 07:47:41 -0000 https://gcc.gnu.org/g:267318a2857a42922c3ca033dac4690172b17683 commit r12-5252-g267318a2857a42922c3ca033dac4690172b17683 Author: Jason Merrill Date: Wed Nov 10 16:42:04 2021 -0500 c++: constexpr virtual and vbase thunk 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. Diff: --- gcc/cp/constexpr.c | 15 ++++++++++----- gcc/testsuite/g++.dg/cpp2a/constexpr-virtual20.C | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) 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-% function %qD", function); - explain_invalid_constexpr_fn (function); + if (!DECL_DECLARED_CONSTEXPR_P (function)) + { + error ("call to non-% 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(&m)->foo() == 5); // { dg-error "" } +}