Re: [PATCH v2] c++: wrong error with static constexpr var in tmpl [PR109876]

2023-07-14 Thread Jason Merrill via Gcc-patches

On 7/13/23 14:54, Marek Polacek wrote:

On Fri, May 26, 2023 at 09:47:10PM -0400, Jason Merrill wrote:

On 5/26/23 19:18, Marek Polacek wrote:

The is_really_empty_class check is sort of non-obvious but the
comment should explain why I added it.

+  /* When there's nothing to initialize, we'll never mark the
+ VAR_DECL TREE_CONSTANT, therefore it would remain
+ value-dependent and we wouldn't instantiate.  */
  
Sorry it's taken so long to get back to this.



Interesting.  Can we change that (i.e. mark it TREE_CONSTANT) rather than
work around it here?


I think we can.  Maybe as in the below:

-- >8 --
Since r8-509, we'll no longer create a static temporary var for
the initializer '{ 1, 2 }' for num in the attached test because
the code in finish_compound_literal is now guarded by
'&& fcl_context == fcl_c99' but it's fcl_functional here.  This
causes us to reject num as non-constant when evaluating it in
a template.

Jason's idea was to treat num as value-dependent even though it
actually isn't.  This patch implements that suggestion.

We weren't marking objects whose type is an empty class type
constant.  This patch changes that so that v_d_e_p doesn't need
to check is_really_empty_class.

Co-authored-by: Jason Merrill 

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?


OK, thanks.

Incidentally, I prefer to put the "ok?" line above the scissors line 
since it isn't intended to be part of the commit message.



PR c++/109876

gcc/cp/ChangeLog:

* decl.cc (cp_finish_decl): Set TREE_CONSTANT when initializing
an object of empty class type.
* pt.cc (value_dependent_expression_p) : Treat a
constexpr-declared non-constant variable as value-dependent.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/constexpr-template12.C: New test.
* g++.dg/cpp1z/constexpr-template1.C: New test.
* g++.dg/cpp1z/constexpr-template2.C: New test.
---
  gcc/cp/decl.cc| 13 +--
  gcc/cp/pt.cc  |  7 
  .../g++.dg/cpp0x/constexpr-template12.C   | 38 +++
  .../g++.dg/cpp1z/constexpr-template1.C| 25 
  .../g++.dg/cpp1z/constexpr-template2.C| 25 
  5 files changed, 105 insertions(+), 3 deletions(-)
  create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-template12.C
  create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-template1.C
  create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-template2.C

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 60f107d50c4..792ab330dd0 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -8200,7 +8200,6 @@ void
  cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
tree asmspec_tree, int flags)
  {
-  tree type;
vec *cleanups = NULL;
const char *asmspec = NULL;
int was_readonly = 0;
@@ -8220,7 +8219,7 @@ cp_finish_decl (tree decl, tree init, bool 
init_const_expr_p,
/* Parameters are handled by store_parm_decls, not cp_finish_decl.  */
gcc_assert (TREE_CODE (decl) != PARM_DECL);
  
-  type = TREE_TYPE (decl);

+  tree type = TREE_TYPE (decl);
if (type == error_mark_node)
  return;
  
@@ -8410,7 +8409,7 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p,

  if (decl_maybe_constant_var_p (decl)
  /* FIXME setting TREE_CONSTANT on refs breaks the back end.  */
  && !TYPE_REF_P (type))
-   TREE_CONSTANT (decl) = 1;
+   TREE_CONSTANT (decl) = true;
}
/* This is handled mostly by gimplify.cc, but we have to deal with
 not warning about int x = x; as it is a GCC extension to turn off
@@ -8421,6 +8420,14 @@ cp_finish_decl (tree decl, tree init, bool 
init_const_expr_p,
  && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self))
suppress_warning (decl, OPT_Winit_self);
  }
+  else if (VAR_P (decl)
+  && COMPLETE_TYPE_P (type)
+  && !TYPE_REF_P (type)
+  && !dependent_type_p (type)
+  && is_really_empty_class (type, /*ignore_vptr*/false))
+/* We have no initializer but there's nothing to initialize anyway.
+   Treat DECL as constant due to c++/109876.  */
+TREE_CONSTANT (decl) = true;
  
if (flag_openmp

&& TREE_CODE (decl) == FUNCTION_DECL
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index fa15b75b9c5..255d18b9539 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -27983,6 +27983,13 @@ value_dependent_expression_p (tree expression)
else if (TYPE_REF_P (TREE_TYPE (expression)))
/* FIXME cp_finish_decl doesn't fold reference initializers.  */
return true;
+  /* We have a constexpr variable and we're processing a template.  When
+there's lifetime extension involved (for which finish_compound_literal
+used to create a temporary), we'll not be able to evaluate the
+variable until instantiating, so 

[PATCH v2] c++: wrong error with static constexpr var in tmpl [PR109876]

2023-07-13 Thread Marek Polacek via Gcc-patches
On Fri, May 26, 2023 at 09:47:10PM -0400, Jason Merrill wrote:
> On 5/26/23 19:18, Marek Polacek wrote:
> > Since r8-509, we'll no longer create a static temporary var for
> > the initializer '{ 1, 2 }' for num in the attached test because
> > the code in finish_compound_literal is now guarded by
> > '&& fcl_context == fcl_c99' but it's fcl_functional here.  This
> > causes us to reject num as non-constant when evaluating it in
> > a template.
> > 
> > Jason's idea was to treat num as value-dependent even though it
> > actually isn't.  This patch implements that suggestion.
> > 
> > The is_really_empty_class check is sort of non-obvious but the
> > comment should explain why I added it.
> > 
> > Co-authored-by: Jason Merrill 
> > 
> > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> > 
> > PR c++/109876
> > 
> > gcc/cp/ChangeLog:
> > 
> > * pt.cc (value_dependent_expression_p) : Treat a
> > constexpr-declared non-constant variable as value-dependent.
> > 
> > gcc/testsuite/ChangeLog:
> > 
> > * g++.dg/cpp0x/constexpr-template12.C: New test.
> > * g++.dg/cpp1z/constexpr-template1.C: New test.
> > ---
> >   gcc/cp/pt.cc  | 12 ++
> >   .../g++.dg/cpp0x/constexpr-template12.C   | 38 +++
> >   .../g++.dg/cpp1z/constexpr-template1.C| 25 
> >   3 files changed, 75 insertions(+)
> >   create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-template12.C
> >   create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-template1.C
> > 
> > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> > index 7fb3e75bceb..38fd8070705 100644
> > --- a/gcc/cp/pt.cc
> > +++ b/gcc/cp/pt.cc
> > @@ -27969,6 +27969,18 @@ value_dependent_expression_p (tree expression)
> > else if (TYPE_REF_P (TREE_TYPE (expression)))
> > /* FIXME cp_finish_decl doesn't fold reference initializers.  */
> > return true;
> > +  /* We have a constexpr variable and we're processing a template.  
> > When
> > +there's lifetime extension involved (for which finish_compound_literal
> > +used to create a temporary), we'll not be able to evaluate the
> > +variable until instantiating, so pretend it's value-dependent.  */
> > +  else if (DECL_DECLARED_CONSTEXPR_P (expression)
> > +  && !TREE_CONSTANT (expression)
> > +  /* When there's nothing to initialize, we'll never mark the
> > + VAR_DECL TREE_CONSTANT, therefore it would remain
> > + value-dependent and we wouldn't instantiate.  */
 
Sorry it's taken so long to get back to this.

> Interesting.  Can we change that (i.e. mark it TREE_CONSTANT) rather than
> work around it here?

I think we can.  Maybe as in the below:

-- >8 --
Since r8-509, we'll no longer create a static temporary var for
the initializer '{ 1, 2 }' for num in the attached test because
the code in finish_compound_literal is now guarded by
'&& fcl_context == fcl_c99' but it's fcl_functional here.  This
causes us to reject num as non-constant when evaluating it in
a template.

Jason's idea was to treat num as value-dependent even though it
actually isn't.  This patch implements that suggestion.

We weren't marking objects whose type is an empty class type
constant.  This patch changes that so that v_d_e_p doesn't need
to check is_really_empty_class.

Co-authored-by: Jason Merrill 

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

PR c++/109876

gcc/cp/ChangeLog:

* decl.cc (cp_finish_decl): Set TREE_CONSTANT when initializing
an object of empty class type.
* pt.cc (value_dependent_expression_p) : Treat a
constexpr-declared non-constant variable as value-dependent.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/constexpr-template12.C: New test.
* g++.dg/cpp1z/constexpr-template1.C: New test.
* g++.dg/cpp1z/constexpr-template2.C: New test.
---
 gcc/cp/decl.cc| 13 +--
 gcc/cp/pt.cc  |  7 
 .../g++.dg/cpp0x/constexpr-template12.C   | 38 +++
 .../g++.dg/cpp1z/constexpr-template1.C| 25 
 .../g++.dg/cpp1z/constexpr-template2.C| 25 
 5 files changed, 105 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-template12.C
 create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-template1.C
 create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-template2.C

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 60f107d50c4..792ab330dd0 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -8200,7 +8200,6 @@ void
 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
tree asmspec_tree, int flags)
 {
-  tree type;
   vec *cleanups = NULL;
   const char *asmspec = NULL;
   int was_readonly = 0;
@@ -8220,7 +8219,7 @@ cp_finish_decl (tree decl, tree init, bool 
init_const_expr_p,
   /* Parameters are handled by store_parm_decls, not