From patchwork Tue Oct 19 13:24:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 46395 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 14B3B385843F for ; Tue, 19 Oct 2021 13:25:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 14B3B385843F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1634649926; bh=ippvVSHbkhT6OpvaOijid4v5tVTlyhy9ajM5YnC/evs=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=DcKq54OQi3o1aSMPsS/ygysrVIa00uFW/oyScdgqPnrQ6tHOgLRX8+7EzVOt+731s +AEedIhStEh6NCau5zY+hK0EPZqfdXywjIw2lad8c2UXcXOwFi4iuodby7yuf3bFjR L6nfZNe6lv6yASniGQuhKWVeKoCPRofv2dGhqVHg= 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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTPS id 3D070385801A for ; Tue, 19 Oct 2021 13:24:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3D070385801A 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-391-zEirxImBMwSC9_OqveZd7Q-1; Tue, 19 Oct 2021 09:24:20 -0400 X-MC-Unique: zEirxImBMwSC9_OqveZd7Q-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DDF9419253C0 for ; Tue, 19 Oct 2021 13:24:19 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.193.172]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 79B0860CA1; Tue, 19 Oct 2021 13:24:13 +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 19JDOAGo2927812 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 19 Oct 2021 15:24:11 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 19JDOAvT2927809; Tue, 19 Oct 2021 15:24:10 +0200 Date: Tue, 19 Oct 2021 15:24:10 +0200 To: Jason Merrill Subject: [PATCH] c++: Reject addresses of immediate functions in constexpr vars inside of immediate functions or consteval if [PR102753] Message-ID: <20211019132410.GM304296@tucnak> References: <20211018081258.GV304296@tucnak> <40b79f8d-dd22-a2f4-f63d-802d67395444@redhat.com> <20211019120021.GL304296@tucnak> MIME-Version: 1.0 In-Reply-To: <20211019120021.GL304296@tucnak> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-5.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SHORT, 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" On Tue, Oct 19, 2021 at 02:00:21PM +0200, Jakub Jelinek via Gcc-patches wrote: > And another thing isn't in a patch, but I'm wondering whether we don't > handle it incorrectly. constexpr.c has: > /* Check that immediate invocation does not return an expression referencing > any immediate function decls. They need to be allowed while parsing > immediate functions, but can't leak outside of them. */ > if (is_consteval > && t != r > && (current_function_decl == NULL_TREE > || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl))) > as condition for the discovery of embedded immediate FUNCTION_DECLs > (or now PTRMEM_CSTs). If I remove the && (current... ..._decl)) > then g++.dg/cpp2a/consteval7.C's > struct S { int b; int (*c) (); }; > consteval S baz () { return { 5, foo }; } > consteval int qux () { S s = baz (); return s.b + s.c (); } > consteval int quux () { constexpr S s = baz (); return s.b + s.c (); } > quux line fails, but based on > http://eel.is/c++draft/expr.const#11 > I wonder if it shouldn't fail (clang++ -std=c++20 rejects it), > and be only accepted without the constexpr keyword before S s. Here is an incremental patch that implements that. 2021-10-19 Jakub Jelinek PR c++/102753 * constexpr.c (cxx_eval_outermost_constant_expr): Perform find_immediate_fndecl discovery if is_consteval or in_immediate_context () rather than if is_consteval, t != r and not in immediate function's body. * g++.dg/cpp2a/consteval7.C: Expect diagnostics on quux. * g++.dg/cpp2a/consteval24.C: New test. * g++.dg/cpp23/consteval-if12.C: New test. Jakub --- gcc/cp/constexpr.c.jj 2021-10-19 12:22:35.583964001 +0200 +++ gcc/cp/constexpr.c 2021-10-19 13:58:22.545182032 +0200 @@ -7472,12 +7472,8 @@ cxx_eval_outermost_constant_expr (tree t } /* Check that immediate invocation does not return an expression referencing - any immediate function decls. They need to be allowed while parsing - immediate functions, but can't leak outside of them. */ - if (is_consteval - && t != r - && (current_function_decl == NULL_TREE - || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl))) + any immediate function decls. */ + if (is_consteval || in_immediate_context ()) if (tree immediate_fndecl = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl, NULL)) --- gcc/testsuite/g++.dg/cpp2a/consteval7.C.jj 2020-01-12 11:54:37.140402440 +0100 +++ gcc/testsuite/g++.dg/cpp2a/consteval7.C 2021-10-19 13:59:54.033897061 +0200 @@ -7,7 +7,7 @@ constexpr auto a = bar (); // { dg-error struct S { int b; int (*c) (); }; consteval S baz () { return { 5, foo }; } consteval int qux () { S s = baz (); return s.b + s.c (); } -consteval int quux () { constexpr S s = baz (); return s.b + s.c (); } +consteval int quux () { constexpr S s = baz (); return s.b + s.c (); } // { dg-error "immediate evaluation returns address of immediate function 'consteval int foo\\(\\)'" } constexpr auto d = baz (); // { dg-error "immediate evaluation returns address of immediate function 'consteval int foo\\(\\)'" } constexpr auto e = qux (); constexpr auto f = quux (); --- gcc/testsuite/g++.dg/cpp2a/consteval24.C.jj 2021-10-19 14:32:51.858019368 +0200 +++ gcc/testsuite/g++.dg/cpp2a/consteval24.C 2021-10-19 14:49:11.618177303 +0200 @@ -0,0 +1,30 @@ +// PR c++/102753 +// { dg-do compile { target c++20 } } + +struct S { + constexpr S () : s (0) {} + consteval int foo () { return 1; } + virtual consteval int bar () { return 2; } + int s; +}; + +consteval int foo () { return 42; } +consteval auto baz () { return foo; } +consteval auto qux () { return &S::foo; } +consteval auto corge () { return &S::bar; } + +consteval int +bar () +{ + S s; + constexpr auto fn1 = foo; // { dg-error "immediate evaluation returns address of immediate function" } + constexpr auto fn2 = &foo; // { dg-error "immediate evaluation returns address of immediate function" } + constexpr auto fn3 = &S::foo; // { dg-error "immediate evaluation returns address of immediate function" } + constexpr auto fn4 = &S::bar; // { dg-error "immediate evaluation returns address of immediate function" } + constexpr auto fn5 = baz (); // { dg-error "immediate evaluation returns address of immediate function" } + constexpr auto fn6 = qux (); // { dg-error "immediate evaluation returns address of immediate function" } + constexpr auto fn7 = corge (); // { dg-error "immediate evaluation returns address of immediate function" } + return fn1 () + fn2 () + (s.*fn3) () + (s.*fn4) () + fn5 () + (s.*fn6) () + (s.*fn7) (); +} + +auto a = bar (); --- gcc/testsuite/g++.dg/cpp23/consteval-if12.C.jj 2021-10-19 14:54:05.123023731 +0200 +++ gcc/testsuite/g++.dg/cpp23/consteval-if12.C 2021-10-19 14:55:29.026844039 +0200 @@ -0,0 +1,34 @@ +// PR c++/102753 +// { dg-do compile { target c++20 } } +// { dg-options "" } + +struct S { + constexpr S () : s (0) {} + consteval int foo () { return 1; } + virtual consteval int bar () { return 2; } + int s; +}; + +consteval int foo () { return 42; } +consteval auto baz () { return foo; } +consteval auto qux () { return &S::foo; } +consteval auto corge () { return &S::bar; } + +constexpr int +bar () +{ + S s; + if consteval { // { dg-warning "'if consteval' only available with" "" { target c++20_only } } + constexpr auto fn1 = foo; // { dg-error "immediate evaluation returns address of immediate function" } + constexpr auto fn2 = &foo; // { dg-error "immediate evaluation returns address of immediate function" } + constexpr auto fn3 = &S::foo; // { dg-error "immediate evaluation returns address of immediate function" } + constexpr auto fn4 = &S::bar; // { dg-error "immediate evaluation returns address of immediate function" } + constexpr auto fn5 = baz (); // { dg-error "immediate evaluation returns address of immediate function" } + constexpr auto fn6 = qux (); // { dg-error "immediate evaluation returns address of immediate function" } + constexpr auto fn7 = corge (); // { dg-error "immediate evaluation returns address of immediate function" } + return fn1 () + fn2 () + (s.*fn3) () + (s.*fn4) () + fn5 () + (s.*fn6) () + (s.*fn7) (); + } + return 0; +} + +auto a = bar ();