From: Marek Polacek <polacek@redhat.com>
To: Jakub Jelinek <jakub@redhat.com>
Cc: Jason Merrill <jason@redhat.com>,
Jonathan Wakely <jwakely@redhat.com>,
gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] c++, v4: Implement C++23 P2647R1 - Permitting static constexpr variables in constexpr functions
Date: Thu, 17 Nov 2022 19:15:05 -0500 [thread overview]
Message-ID: <Y3bOiUJM9FXBdUWO@redhat.com> (raw)
In-Reply-To: <Y3acqbULfy3PULmc@tucnak>
On Thu, Nov 17, 2022 at 09:42:17PM +0100, Jakub Jelinek wrote:
> On Thu, Nov 17, 2022 at 07:42:40PM +0100, Jakub Jelinek via Gcc-patches wrote:
> > I thought for older C++ this is to catch
> > void
> > foo ()
> > {
> > constexpr int a = ({ static constexpr int b = 2; b; });
> > }
> > and for C++23 the only 3 spots that diagnose those.
> > But perhaps for C++20 or older we can check if the var has a context
> > of a constexpr function (then assume cp_finish_decl errored or pedwarned
> > already) and only error or pedwarn otherwise.
>
> So, here is an updated patch, which in constexpr.cc will accept
> DECL_EXPR of decl_*constant_var_p static/thread_local non-extern vars
> for C++23 or if they are not declared in constexpr/consteval function.
> So, the statement expression case will remain hard error for C++ <= 20 rather than
> pedwarn, because due to the ctx->quiet vs. !ctx->quiet case I don't see
> what else we could do, either something is a constant expression, or
> it is not, but whether it is or is not shouldn't depend on
> -Wpedantic/-Wno-pedantic/-Werror=pedantic.
>
> 2022-11-17 Jakub Jelinek <jakub@redhat.com>
>
> gcc/c-family/
> * c-cppbuiltin.cc (c_cpp_builtins): Bump __cpp_constexpr
> value from 202207L to 202211L.
> gcc/cp/
> * constexpr.cc (cxx_eval_constant_expression): Implement C++23
> P2647R1 - Permitting static constexpr variables in constexpr functions.
> Allow decl_constant_var_p static or thread_local vars for
> C++23 and later or if they are declared inside of constexpr or
> consteval function.
> (potential_constant_expression_1): Similarly, except use
> decl_maybe_constant_var_p instead of decl_constant_var_p if
> processing_template_decl.
> * decl.cc (diagnose_static_in_constexpr): New function.
> (start_decl): Remove diagnostics of static or thread_local
> vars in constexpr or consteval functions.
> (cp_finish_decl): Call diagnose_static_in_constexpr.
> gcc/testsuite/
> * g++.dg/cpp23/constexpr-nonlit17.C: New test.
> * g++.dg/cpp23/constexpr-nonlit18.C: New test.
> * g++.dg/cpp23/constexpr-nonlit19.C: New test.
> * g++.dg/cpp23/constexpr-nonlit20.C: New test.
> * g++.dg/cpp23/feat-cxx2b.C: Adjust expected __cpp_constexpr
> value.
> * g++.dg/ext/stmtexpr19.C: Don't expect an error for C++20 or later.
>
> --- gcc/c-family/c-cppbuiltin.cc.jj 2022-11-17 09:00:42.106249011 +0100
> +++ gcc/c-family/c-cppbuiltin.cc 2022-11-17 09:01:49.286320527 +0100
> @@ -1074,7 +1074,7 @@ c_cpp_builtins (cpp_reader *pfile)
> /* Set feature test macros for C++23. */
> cpp_define (pfile, "__cpp_size_t_suffix=202011L");
> cpp_define (pfile, "__cpp_if_consteval=202106L");
> - cpp_define (pfile, "__cpp_constexpr=202207L");
> + cpp_define (pfile, "__cpp_constexpr=202211L");
> cpp_define (pfile, "__cpp_multidimensional_subscript=202211L");
> cpp_define (pfile, "__cpp_named_character_escapes=202207L");
> cpp_define (pfile, "__cpp_static_call_operator=202207L");
> --- gcc/cp/constexpr.cc.jj 2022-11-17 08:48:30.530357181 +0100
> +++ gcc/cp/constexpr.cc 2022-11-17 20:53:15.432408015 +0100
> @@ -7100,17 +7100,35 @@ cxx_eval_constant_expression (const cons
> /* Allow __FUNCTION__ etc. */
> && !DECL_ARTIFICIAL (r))
> {
> - if (!ctx->quiet)
> + bool ok = decl_constant_var_p (r);
> + /* Since P2647R1 control can pass through definitions of static
> + or thread_local vars usable in constant expressions.
> + In C++20 or older, if such vars are declared inside of
> + constexpr or consteval function, diagnose_static_in_constexpr
> + should have already pedwarned on those. Otherwise they could
> + be e.g. in a statement expression, reject those before
> + C++23. */
> + if (ok && cxx_dialect < cxx23)
> {
> - if (CP_DECL_THREAD_LOCAL_P (r))
> - error_at (loc, "control passes through definition of %qD "
> - "with thread storage duration", r);
> - else
> - error_at (loc, "control passes through definition of %qD "
> - "with static storage duration", r);
> + tree fnctx = decl_function_context (r);
> + if (fnctx == NULL_TREE
> + || !DECL_DECLARED_CONSTEXPR_P (fnctx))
> + ok = false;
FWIW, I couldn't find a way to trigger this code.
> + }
> + if (!ok)
> + {
> + if (!ctx->quiet)
> + {
> + if (CP_DECL_THREAD_LOCAL_P (r))
> + error_at (loc, "control passes through definition of "
> + "%qD with thread storage duration", r);
> + else
> + error_at (loc, "control passes through definition of "
> + "%qD with static storage duration", r);
> + }
> + *non_constant_p = true;
> + break;
> }
> - *non_constant_p = true;
> - break;
> }
>
> if (AGGREGATE_TYPE_P (TREE_TYPE (r))
> @@ -9588,21 +9606,41 @@ potential_constant_expression_1 (tree t,
> tmp = DECL_EXPR_DECL (t);
> if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp))
> {
> - if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
> - {
> - if (flags & tf_error)
> - constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
> - "%qD defined %<thread_local%> in "
> - "%<constexpr%> context", tmp);
> - return false;
> - }
> - else if (TREE_STATIC (tmp))
> + if (TREE_STATIC (tmp)
> + || (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp)))
> {
> - if (flags & tf_error)
> - constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
> - "%qD defined %<static%> in %<constexpr%> "
> - "context", tmp);
> - return false;
> + bool ok = (processing_template_decl
> + ? decl_maybe_constant_var_p (tmp)
> + : decl_constant_var_p (tmp));
> + /* Since P2647R1 control can pass through definitions of static
> + or thread_local vars usable in constant expressions.
> + In C++20 or older, if such vars are declared inside of
> + constexpr or consteval function, diagnose_static_in_constexpr
> + should have already pedwarned on those. Otherwise they could
> + be e.g. in a statement expression, reject those before
> + C++23. */
> + if (ok && cxx_dialect < cxx23)
> + {
> + tree fnctx = decl_function_context (tmp);
> + if (fnctx == NULL_TREE
> + || !DECL_DECLARED_CONSTEXPR_P (fnctx))
> + ok = false;
> + }
> + if (!ok)
> + {
> + if (flags & tf_error)
> + {
> + if (CP_DECL_THREAD_LOCAL_P (tmp))
> + constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
> + "%qD defined %<thread_local%> in "
> + "%<constexpr%> context", tmp);
> + else
> + constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
> + "%qD defined %<static%> in "
> + "%<constexpr%> context", tmp);
> + }
> + return false;
> + }
> }
> else if (!check_for_uninitialized_const_var
> (tmp, /*constexpr_context_p=*/true, flags))
> --- gcc/cp/decl.cc.jj 2022-11-16 14:44:43.692339668 +0100
> +++ gcc/cp/decl.cc 2022-11-17 20:53:44.102011594 +0100
> @@ -5600,6 +5600,57 @@ groktypename (cp_decl_specifier_seq *typ
> return type;
> }
>
> +/* For C++17 and older diagnose static or thread_local decls in constexpr
> + or consteval functions. For C++20 similarly, except if they are
In C++17 we don't support consteval so I guess drop the "or consteval "?
BTW, I notice that the patch breaks
g++.dg/cpp1y/lambda-generic-func1.C
g++.dg/cpp1z/constexpr-lambda16.C
Maybe they just need dg- tweaks.
Marek
next prev parent reply other threads:[~2022-11-18 0:15 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-11 17:07 [PATCH] c++: " Jakub Jelinek
2022-11-13 11:45 ` [PATCH] c++, v2: " Jakub Jelinek
2022-11-15 23:36 ` Jason Merrill
2022-11-15 23:50 ` Jakub Jelinek
2022-11-16 0:27 ` Jonathan Wakely
2022-11-16 6:19 ` Jakub Jelinek
2022-11-16 13:20 ` Jason Merrill
2022-11-16 14:08 ` Jakub Jelinek
2022-11-16 14:33 ` Jason Merrill
2022-11-16 14:46 ` Jakub Jelinek
2022-11-16 20:26 ` Jason Merrill
2022-11-17 9:13 ` [PATCH] c++, v3: " Jakub Jelinek
2022-11-17 14:42 ` Jason Merrill
2022-11-17 18:42 ` Jakub Jelinek
2022-11-17 20:42 ` [PATCH] c++, v4: " Jakub Jelinek
2022-11-18 0:15 ` Marek Polacek [this message]
2022-11-18 7:48 ` Jakub Jelinek
2022-11-18 15:03 ` Marek Polacek
2022-11-18 15:14 ` Jakub Jelinek
2022-11-18 16:24 ` Jason Merrill
2022-11-18 16:34 ` Jakub Jelinek
2022-11-18 16:52 ` Jason Merrill
2022-11-18 0:28 ` Jason Merrill
2022-11-18 9:10 ` [PATCH] c++, v5: " Jakub Jelinek
2022-11-16 0:26 ` [PATCH] c++, v2: " Jonathan Wakely
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=Y3bOiUJM9FXBdUWO@redhat.com \
--to=polacek@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jakub@redhat.com \
--cc=jason@redhat.com \
--cc=jwakely@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).