diff options
author | Jakub Jelinek <jakub@redhat.com> | 2021-10-27 09:08:19 +0200 |
---|---|---|
committer | Jakub Jelinek <jakub@redhat.com> | 2021-10-27 09:08:19 +0200 |
commit | 7473b8a90490e1dcd8fd5f7a92307d79fd2a5f8e (patch) | |
tree | 2df8bc219d3e32884f26f5de91193259d9cf8d8f /gcc/cp/semantics.c | |
parent | 4b2fda8bea3fdcc9421726e5a21e537f745cad0b (diff) | |
download | gcc-7473b8a90490e1dcd8fd5f7a92307d79fd2a5f8e.zip gcc-7473b8a90490e1dcd8fd5f7a92307d79fd2a5f8e.tar.gz gcc-7473b8a90490e1dcd8fd5f7a92307d79fd2a5f8e.tar.bz2 |
c++: Reject addresses of immediate functions in constexpr vars inside of immediate functions or consteval if [PR102753]
Another thing that wasn't in the previous 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.
Also wonder about e.g.
consteval int foo () { return 42; }
consteval int
bar ()
{
auto fn1 = foo; // This must be ok
constexpr auto fn2 = foo; // Isn't this an error?
return fn1 () + fn2 ();
}
constexpr int
baz ()
{
if consteval {
auto fn1 = foo; // This must be ok
constexpr auto fn2 = foo; // Isn't this an error?
return fn1 () + fn2 ();
}
return 0;
}
auto a = bar ();
static_assert (bar () == 84);
static_assert (baz () == 84);
(again, clang++ -std=c++20 rejects the fn2 = foo; case,
but doesn't implement consteval if, so can't test the other one).
For taking address of an immediate function or method if it is taken
outside of immediate function context we already have diagnostics
about it, but shouldn't the immediate FUNCTION_DECL discovery in
cxx_eval_outermost_constant_expression be instead guarded with something
like
if (is_consteval || in_immediate_context ())
and be done regardless of whether t != r?
2021-10-27 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.
Diffstat (limited to 'gcc/cp/semantics.c')
0 files changed, 0 insertions, 0 deletions