From: Jakub Jelinek <jakub@redhat.com>
To: Jason Merrill <jason@redhat.com>
Cc: gcc-patches@gcc.gnu.org
Subject: [PATCH] c++: Reject addresses of immediate functions in constexpr vars inside of immediate functions or consteval if [PR102753]
Date: Tue, 19 Oct 2021 15:24:10 +0200 [thread overview]
Message-ID: <20211019132410.GM304296@tucnak> (raw)
In-Reply-To: <20211019120021.GL304296@tucnak>
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 <jakub@redhat.com>
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.
--- 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 ();
Jakub
next prev parent reply other threads:[~2021-10-19 13:24 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-18 8:12 [PATCH] c++: Diagnose taking address of an immediate member function [PR102753] Jakub Jelinek
2021-10-18 16:42 ` Jason Merrill
2021-10-19 12:00 ` [PATCH, v2] " Jakub Jelinek
2021-10-19 13:24 ` Jakub Jelinek [this message]
2021-10-20 23:26 ` [PATCH] c++: Reject addresses of immediate functions in constexpr vars inside of immediate functions or consteval if [PR102753] Jason Merrill
2021-10-20 23:16 ` [PATCH, v2] c++: Diagnose taking address of an immediate member function [PR102753] Jason Merrill
2021-10-21 11:17 ` Jakub Jelinek
2021-10-26 20:58 ` Jason Merrill
2021-10-29 15:24 ` Jakub Jelinek
2021-11-23 20:45 ` Jason Merrill
2021-11-24 16:02 ` Jakub Jelinek
2021-11-24 18:02 ` [PATCH] c++: Fix up diagnostics about " Jakub Jelinek
2021-11-24 22:15 ` Jason Merrill
2021-11-24 22:42 ` [PATCH] c++, v2: " Jakub Jelinek
2021-11-25 2:07 ` Jason Merrill
2021-11-25 14:38 ` [PATCH] c++, v3: " Jakub Jelinek
2021-11-25 15:49 ` Jason Merrill
2021-11-27 8:52 ` [PATCH] c++: Small incremental tweak to source_location::current() folding Jakub Jelinek
2021-11-29 22:45 ` Jason Merrill
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211019132410.GM304296@tucnak \
--to=jakub@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jason@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).