From patchwork Tue Dec 3 17:25:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Martin X-Patchwork-Id: 102332 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 3EBDB3858C53 for ; Tue, 3 Dec 2024 17:27:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3EBDB3858C53 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=nasilyan.com header.i=@nasilyan.com header.a=rsa-sha256 header.s=tey23rxsjton5kop5bydp3vc5ylkyjkg header.b=l7UdsPJD; dkim=pass (1024-bit key, unprotected) header.d=amazonses.com header.i=@amazonses.com header.a=rsa-sha256 header.s=uku4taia5b5tsbglxyj6zym32efj7xqv header.b=PMUZwT/b X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from a7-19.smtp-out.eu-west-1.amazonses.com (a7-19.smtp-out.eu-west-1.amazonses.com [54.240.7.19]) by sourceware.org (Postfix) with ESMTPS id 561D53858C50 for ; Tue, 3 Dec 2024 17:25:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 561D53858C50 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=nasilyan.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=eu-west-1.amazonses.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 561D53858C50 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=54.240.7.19 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1733246742; cv=none; b=FsynBaLdjpc/ErqX0/lckixWsX3+7VrhF1sXrNjDpsIKuEp+kCuyJSphBgy23zZiC8JEtWIgV+LyOlGJhEbZJtEc0Kxf7KLCevgrFImWkh8UkQ8hcGzeGbr6ZUbea53rPmXqi6A8cjPORrKBQaOhWKZYr+vT+HIe8ToWnDgrITE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1733246742; c=relaxed/simple; bh=bA6RjUR+zACPCsXxFg/KmPa05YS/gxf0hPHPFAWHCCg=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID: MIME-Version; b=iW4JP+0ySIdV95p/nvIRn4aV97LAWtCHrf9CCJJR7fpo5QBtAtVFLGaBpX8BtOECyqRTs1vDKIQjO3PvEkLvL/AxhMh8aTyCqKIDsCvu5f7t2beJwVKXdh1aJUDL9llqrkQ0l6JLQLrexzWfgkl4u+g4jV6KNQNREb8qIlERWJE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 561D53858C50 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=tey23rxsjton5kop5bydp3vc5ylkyjkg; d=nasilyan.com; t=1733246740; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Transfer-Encoding; bh=bA6RjUR+zACPCsXxFg/KmPa05YS/gxf0hPHPFAWHCCg=; b=l7UdsPJD8cj7fLIXkZ4G5YxZenSs+1Oo5VfI2dGQ6OxIolVQx2HxKTWj1aTSHoZq nDKKZZh517/ugSFqSUY7h/RDtcrq+R2QEP0ZWi38M3qa9KBedx8dzvwCef+g9qoERKt 1a6UUWgmKoEouIOvBbRiQRCFRf+bEjsPGhYFBNBM= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=uku4taia5b5tsbglxyj6zym32efj7xqv; d=amazonses.com; t=1733246740; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Transfer-Encoding:Feedback-ID; bh=bA6RjUR+zACPCsXxFg/KmPa05YS/gxf0hPHPFAWHCCg=; b=PMUZwT/bgf2yhywh9ryHDtHmNChO9/5pqAbaMrJPoBEL41hq3fhHDV9M0DA5SzOY yQirRE3Q07+tHWg3rNRsD5KlDjBt0UJyC3m4T3x/lBYkoP/yiWPasN6RJ0g1vxW/OJ2 bAgNaoxzng6I9O+CERi3e3CcfExPKxfc9cshAHvc= From: Simon Martin To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com Subject: [PATCH] c++: Don't reject pointer to virtual method during constant evaluation [PR117615] Date: Tue, 3 Dec 2024 17:25:40 +0000 Message-ID: <010201938d8e6923-df228ae4-b5f9-4568-8ef0-5dcb665cb17d-000000@eu-west-1.amazonses.com> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 Feedback-ID: ::1.eu-west-1.sMDMtMBSIJpOhM8kCarpRYeKFuDfm/WoE5+9WtFxb8w=:AmazonSES X-SES-Outgoing: 2024.12.03-54.240.7.19 X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_PASS, TXREP, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org We currently reject the following valid code: === cut here === struct Base { virtual void doit (int v) const {} }; struct Derived : Base { void doit (int v) const {} }; using fn_t = void (Base::*)(int) const; struct Helper { fn_t mFn; constexpr Helper (auto && fn) : mFn(static_cast(fn)) {} }; void foo () { constexpr Helper h (&Derived::doit); } === cut here === The problem is that since r6-4014-gdcdbc004d531b4, &Derived::doit is represented with an expression with type pointer to method and using an INTEGER_CST (here 1), and that cxx_eval_constant_expression rejects any such expression with a non-null INTEGER_CST. This patch uses the same strategy as r12-4491-gf45610a45236e9 (fix for PR c++/102786), and simply lets such expressions go through. Successfully tested on x86_64-pc-linux-gnu. PR c++/117615 gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_constant_expression): Don't reject INTEGER_CSTs with type POINTER_TYPE to METHOD_TYPE. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/constexpr-virtual22.C: New test. --- gcc/cp/constexpr.cc | 27 ++++++++++++------- .../g++.dg/cpp2a/constexpr-virtual22.C | 22 +++++++++++++++ 2 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/constexpr-virtual22.C diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 5a87fa485c6..d9636bad683 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -8277,15 +8277,24 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, } else { - /* This detects for example: - reinterpret_cast(sizeof 0) - */ - if (!ctx->quiet) - error_at (loc, "%(%E)%> is not " - "a constant expression", - type, op); - *non_constant_p = true; - return t; + if (TYPE_PTR_P (type) + && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE) + /* INTEGER_CST with pointer-to-method type is only used + for a virtual method in a pointer to member function. + Don't reject those. */ + ; + else + { + /* This detects for example: + reinterpret_cast(sizeof 0) + */ + if (!ctx->quiet) + error_at (loc, "%(%E)%> is not " + "a constant expression", + type, op); + *non_constant_p = true; + return t; + } } } diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-virtual22.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-virtual22.C new file mode 100644 index 00000000000..89330bf8620 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-virtual22.C @@ -0,0 +1,22 @@ +// PR c++/117615 +// { dg-do "compile" { target c++20 } } + +struct Base { + virtual void doit (int v) const {} +}; + +struct Derived : Base { + void doit (int v) const {} +}; + +using fn_t = void (Base::*)(int) const; + +struct Helper { + fn_t mFn; + constexpr Helper (auto && fn) : mFn(static_cast(fn)) {} +}; + +void foo () { + constexpr Helper h (&Derived::doit); + constexpr Helper h2 (&Base::doit); +}