On Thu, Nov 17, 2022 at 09:42:18AM -0500, Jason Merrill wrote:
> > --- gcc/cp/constexpr.cc.jj 2022-11-17 08:48:30.530357181 +0100
> > +++ gcc/cp/constexpr.cc 2022-11-17 09:56:50.479522863 +0100
> > @@ -7098,7 +7098,8 @@ cxx_eval_constant_expression (const cons
> > && (TREE_STATIC (r)
> > || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
> > /* Allow __FUNCTION__ etc. */
> > - && !DECL_ARTIFICIAL (r))
> > + && !DECL_ARTIFICIAL (r)
> > + && (cxx_dialect < cxx20 || !decl_constant_var_p (r)))
>
> I don't think we need to check cxx_dialect here since
> diagnose_static_in_constexpr will have already complained.
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.
>
> > {
> > if (!ctx->quiet)
> > {
> > @@ -9588,7 +9589,12 @@ 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 (CP_DECL_THREAD_LOCAL_P (tmp)
> > + && !DECL_REALLY_EXTERN (tmp)
> > + && (cxx_dialect < cxx20
> > + || (processing_template_decl
> > + ? !decl_maybe_constant_var_p (tmp)
> > + : !decl_constant_var_p (tmp))))
>
> Or here.
>
> > {
> > if (flags & tf_error)
> > constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
> > @@ -9596,7 +9602,11 @@ potential_constant_expression_1 (tree t,
> > "%<constexpr%> context", tmp);
> > return false;
> > }
> > - else if (TREE_STATIC (tmp))
> > + else if (TREE_STATIC (tmp)
> > + && (cxx_dialect < cxx20
> > + || (processing_template_decl
> > + ? !decl_maybe_constant_var_p (tmp)
> > + : !decl_constant_var_p (tmp))))
> > {
> > if (flags & tf_error)
> > constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
And these too.
> > +static void
> > +diagnose_static_in_constexpr (tree decl)
> > +{
> > + if (current_function_decl && VAR_P (decl)
> > + && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
> > + && cxx_dialect < cxx23
> > + && (cxx_dialect < cxx20
> > + || (processing_template_decl
> > + ? !decl_maybe_constant_var_p (decl)
> > + : !decl_constant_var_p (decl))))
>
> For (maybe) constant variables let's make this error a pedwarn in C++20 and
> below.
Ok.
>
> > + {
> > + bool ok = false;
> > + if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
> > + error_at (DECL_SOURCE_LOCATION (decl),
> > + "%qD defined %<thread_local%> in %qs function only "
> > + "available with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
> > + DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
> > + ? "consteval" : "constexpr");
> > + else if (TREE_STATIC (decl))
> > + error_at (DECL_SOURCE_LOCATION (decl),
> > + "%qD defined %<static%> in %qs function only available "
> > + "with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
> > + DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
> > + ? "consteval" : "constexpr");
> > + else
> > + ok = true;
> > + if (!ok)
> > + cp_function_chain->invalid_constexpr = true;
> > + }
> > +}
> > +
> > /* Process a DECLARATOR for a function-scope or namespace-scope
> > variable or function declaration.
> > (Function definitions go through start_function; class member
> > @@ -5860,28 +5895,8 @@ start_decl (const cp_declarator *declara
> > DECL_THIS_STATIC (decl) = 1;
> > }
> > - if (current_function_decl && VAR_P (decl)
> > - && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
> > - && cxx_dialect < cxx23)
> > - {
> > - bool ok = false;
> > - if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
> > - error_at (DECL_SOURCE_LOCATION (decl),
> > - "%qD defined %<thread_local%> in %qs function only "
> > - "available with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
> > - DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
> > - ? "consteval" : "constexpr");
> > - else if (TREE_STATIC (decl))
> > - error_at (DECL_SOURCE_LOCATION (decl),
> > - "%qD defined %<static%> in %qs function only available "
> > - "with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
> > - DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
> > - ? "consteval" : "constexpr");
> > - else
> > - ok = true;
> > - if (!ok)
> > - cp_function_chain->invalid_constexpr = true;
> > - }
> > + if (cxx_dialect < cxx20)
> > + diagnose_static_in_constexpr (decl);
>
> Can we drop this call (and make the ones in cp_finish_decl unconditional)?
Yes, will do it.
Jakub