public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Jason Merrill <jason@redhat.com>
Cc: Jonathan Wakely <jwakely@redhat.com>,
	gcc-patches@gcc.gnu.org, Marek Polacek <polacek@redhat.com>
Subject: Re: [PATCH] c++, v3: Implement C++23 P2647R1 - Permitting static constexpr variables in constexpr functions
Date: Thu, 17 Nov 2022 19:42:40 +0100	[thread overview]
Message-ID: <Y3aAoKUEwPudGAny@tucnak> (raw)
In-Reply-To: <740b5e1e-7143-c291-5594-af937867fbc3@redhat.com>

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


  reply	other threads:[~2022-11-17 18:42 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 [this message]
2022-11-17 20:42                           ` [PATCH] c++, v4: " Jakub Jelinek
2022-11-18  0:15                             ` Marek Polacek
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=Y3aAoKUEwPudGAny@tucnak \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=jwakely@redhat.com \
    --cc=polacek@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).