From patchwork Mon Nov 15 07:45:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 47647 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D22643858029 for ; Mon, 15 Nov 2021 07:46:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D22643858029 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1636962375; bh=n+mQWWaWAarQS2ZDoCdM/DBNpWO9/uHcTDUCMLbeVbg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Z/AIvSfDSqZyVOasigJAxZAdMhBO3FEhRw3ZEO3LiFX+JOxY+Z+L8DxrG/E4zPEUV rwHYuMgtHprdn/PAsArTYbXl9RJxDkb0GPs4pMUQ/dJpmfRekKs/RilGokJaMR09dS MLrxEtKHYvoHdUpV0Ydf2cdYvZIJQ2uSSxnpY7hY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id A716B3858D39 for ; Mon, 15 Nov 2021 07:45:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A716B3858D39 Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-210-daPkBetNO5afPLOE9iETtA-1; Mon, 15 Nov 2021 02:45:44 -0500 X-MC-Unique: daPkBetNO5afPLOE9iETtA-1 Received: by mail-qv1-f72.google.com with SMTP id j9-20020a05621419c900b003b815c01a54so15192313qvc.10 for ; Sun, 14 Nov 2021 23:45:44 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=n+mQWWaWAarQS2ZDoCdM/DBNpWO9/uHcTDUCMLbeVbg=; b=F2T4sxhH3/FtViUo+N3JD6cgPGkGl3EV8DYuwbZncsXVM+6fvMAFE+gH+pEd6XjrsF uUJjJwfpFcNB5OFUPUvcXG890rjhR4P6lSk70JsiG0rKLvSVtIq4sk4S+ViaX7w0WDuc lyJSCo1C+qh7H57909ovnt7MWdYOS31z6cJistd/gNs+VSFfsxvLGHAD+XWj1L08ZnRE NuL4Q8bRZNFzS00NWlXzYd3U16vfXmGY3HlmvI0T9l0DA234/p0TQ74GDX5OfNcXPAWC 0PzBsWu4oZDNAHrsAvTIg8jKzBKBUd3PbI/I9VKIr7XoP1wagcporgxlvaqix4Kn0IUu gEcA== X-Gm-Message-State: AOAM531GtiB1hSEcMJHwxOLE0ewvm1lwIRAr1AM0uEN8+kvr52Kun66P KIZgKEptaqFOqcxsey1aLBhOSQmuU9ihP518c1+CKg9VqjY0r1BJV104f30ZR7m9gLJYYsQXBLX RBSjnQIwU1YKC9i1+W04hFuAsRVqJaPxkgTRjrr9UFmnPLsVGgDnRPcpCIEgLZhGCuw== X-Received: by 2002:ac8:5a01:: with SMTP id n1mr4146414qta.13.1636962343145; Sun, 14 Nov 2021 23:45:43 -0800 (PST) X-Google-Smtp-Source: ABdhPJwFixSHNqzcRQEg3i8dAjT2MReN2UBRVUw5/aoOnrkvm7VaBAOqRtKmQJ6l63U7Hhc8noeD+Q== X-Received: by 2002:ac8:5a01:: with SMTP id n1mr4146367qta.13.1636962342676; Sun, 14 Nov 2021 23:45:42 -0800 (PST) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id h16sm7733942qtx.20.2021.11.14.23.45.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Nov 2021 23:45:41 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: constexpr virtual and vbase thunk Date: Mon, 15 Nov 2021 02:45:39 -0500 Message-Id: <20211115074539.3351927-1-jason@redhat.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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 base-commit: adcfd2c45c3523d74279b5fcac1d7c6c34dd1382 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 "" } +}