From patchwork Mon Oct 18 08:12:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 46321 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 855F93858400 for ; Mon, 18 Oct 2021 08:13:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 855F93858400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1634544815; bh=mjhNafOByoqHYP8mcp+xaj8+dpDzxvd/ftoi8HFMDqs=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=ZFI6NdrzB8JEeaATfE9IULIDJqQTQAGtC0FVQV55sf22wc68TM9mqCBAqim+lVcTr GxuZi7UCxUJQRN2jua5oyWDXgHdGzRAvQFwszlEBhIUNW3CNsDIuGeT9/qCyj9C6IW d5jLuTTVBKbmPelqje2Rpt7Fbg41gDie1nFkOZeY= 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 ESMTP id 578D63858C39 for ; Mon, 18 Oct 2021 08:13:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 578D63858C39 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-524-4b_lvYhYPZ6jWVG_NumP3A-1; Mon, 18 Oct 2021 04:13:04 -0400 X-MC-Unique: 4b_lvYhYPZ6jWVG_NumP3A-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E0A4B1923769 for ; Mon, 18 Oct 2021 08:13:03 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.193.172]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 74ACA19723; Mon, 18 Oct 2021 08:13:01 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 19I8CxoJ1786871 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 18 Oct 2021 10:12:59 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 19I8CwPW1786870; Mon, 18 Oct 2021 10:12:58 +0200 Date: Mon, 18 Oct 2021 10:12:58 +0200 To: Jason Merrill Subject: [PATCH] c++: Diagnose taking address of an immediate member function [PR102753] Message-ID: <20211018081258.GV304296@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-5.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hi! The following testcase ICEs, because while we have in cp_build_addr_expr_1 diagnostics for taking address of an immediate function (and as an exception deal with build_address from immediate invocation), I forgot to diagnose taking address of a member function which is done in a different place. I hope (s.*&S::foo) () is not an immediate invocation like (*&foo) () is not, so this patch just diagnoses taking address of a member function when not in immediate context. Bootstrapped/regtested on x86_64-linux and i686-linux (without go, that seem to have some evrp issue when building libgo on both), ok for trunk? 2021-10-18 Jakub Jelinek PR c++/102753 * typeck.c (cp_build_addr_expr_1): Diagnose taking address of an immediate method. Use t instead of TREE_OPERAND (arg, 1). * g++.dg/cpp2a/consteval20.C: New test. Jakub --- gcc/cp/typeck.c.jj 2021-10-05 09:53:55.382734051 +0200 +++ gcc/cp/typeck.c 2021-10-15 19:28:38.034213437 +0200 @@ -6773,9 +6773,21 @@ cp_build_addr_expr_1 (tree arg, bool str return error_mark_node; } + if (TREE_CODE (t) == FUNCTION_DECL + && DECL_IMMEDIATE_FUNCTION_P (t) + && cp_unevaluated_operand == 0 + && (current_function_decl == NULL_TREE + || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl))) + { + if (complain & tf_error) + error_at (loc, "taking address of an immediate function %qD", + t); + return error_mark_node; + } + type = build_ptrmem_type (context_for_name_lookup (t), TREE_TYPE (t)); - t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1)); + t = make_ptrmem_cst (type, t); return t; } --- gcc/testsuite/g++.dg/cpp2a/consteval20.C.jj 2021-10-15 19:40:38.691900472 +0200 +++ gcc/testsuite/g++.dg/cpp2a/consteval20.C 2021-10-15 19:49:15.281508419 +0200 @@ -0,0 +1,24 @@ +// PR c++/102753 +// { dg-do compile { target c++20 } } + +struct S { + consteval int foo () const { return 42; } +}; + +constexpr S s; + +int +bar () +{ + return (s.*&S::foo) (); // { dg-error "taking address of an immediate function" } +} + +constexpr auto a = &S::foo; // { dg-error "taking address of an immediate function" } + +consteval int +baz () +{ + return (s.*&S::foo) (); +} + +static_assert (baz () == 42);