[Bug c++/96994] Missing code from consteval constructor initializing const variable

2021-04-20 Thread jakub at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

Jakub Jelinek  changed:

   What|Removed |Added

 Status|NEW |RESOLVED
 Resolution|--- |FIXED

--- Comment #11 from Jakub Jelinek  ---
Fixed.

[Bug c++/96994] Missing code from consteval constructor initializing const variable

2020-10-05 Thread cvs-commit at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

--- Comment #10 from CVS Commits  ---
The releases/gcc-10 branch has been updated by Jakub Jelinek
:

https://gcc.gnu.org/g:2513dad670c00dd9db3a85be348f6f4020b18b81

commit r10-8853-g2513dad670c00dd9db3a85be348f6f4020b18b81
Author: Jakub Jelinek 
Date:   Thu Oct 1 11:18:35 2020 +0200

c++: Fix up default initialization with consteval default ctor [PR96994]

> > The following testcase is miscompiled (in particular the a and i
> > initialization).  The problem is that build_special_member_call due to
> > the immediate constructors (but not evaluated in constant expression
mode)
> > doesn't create a CALL_EXPR, but returns a TARGET_EXPR with CONSTRUCTOR
> > as the initializer for it,
>
> That seems like the bug; at the end of build_over_call, after you
>
> >call = cxx_constant_value (call, obj_arg);
>
> You need to build an INIT_EXPR if obj_arg isn't a dummy.

That works.  obj_arg is NULL if it is a dummy from the earlier code.

2020-10-01  Jakub Jelinek  

PR c++/96994
* call.c (build_over_call): If obj_arg is non-NULL, return
INIT_EXPR
setting obj_arg to call.

* g++.dg/cpp2a/consteval18.C: New test.

(cherry picked from commit 56da736cc6ced0f1c339744321a14ae569db8606)

[Bug c++/96994] Missing code from consteval constructor initializing const variable

2020-10-01 Thread cvs-commit at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

--- Comment #9 from CVS Commits  ---
The master branch has been updated by Jakub Jelinek :

https://gcc.gnu.org/g:56da736cc6ced0f1c339744321a14ae569db8606

commit r11-3582-g56da736cc6ced0f1c339744321a14ae569db8606
Author: Jakub Jelinek 
Date:   Thu Oct 1 11:18:35 2020 +0200

c++: Fix up default initialization with consteval default ctor [PR96994]

> > The following testcase is miscompiled (in particular the a and i
> > initialization).  The problem is that build_special_member_call due to
> > the immediate constructors (but not evaluated in constant expression
mode)
> > doesn't create a CALL_EXPR, but returns a TARGET_EXPR with CONSTRUCTOR
> > as the initializer for it,
>
> That seems like the bug; at the end of build_over_call, after you
>
> >call = cxx_constant_value (call, obj_arg);
>
> You need to build an INIT_EXPR if obj_arg isn't a dummy.

That works.  obj_arg is NULL if it is a dummy from the earlier code.

2020-10-01  Jakub Jelinek  

PR c++/96994
* call.c (build_over_call): If obj_arg is non-NULL, return
INIT_EXPR
setting obj_arg to call.

* g++.dg/cpp2a/consteval18.C: New test.

[Bug c++/96994] Missing code from consteval constructor initializing const variable

2020-09-21 Thread mpolacek at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

Marek Polacek  changed:

   What|Removed |Added

   Keywords||patch

--- Comment #8 from Marek Polacek  ---
Patch was posted:
https://gcc.gnu.org/pipermail/gcc-patches/2020-September/553948.html

[Bug c++/96994] Missing code from consteval constructor initializing const variable

2020-09-14 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

--- Comment #7 from Jakub Jelinek  ---
So, what I see is that expand_default_init calls build_special_member_call for
the default ctor, but because the default ctor is an immediate method, it
returns a TARGET_EXPR with CONSTRUCTOR as the initializer, rather than a call.
expand_default_init doesn't return anything, just appends the rval as
statement, but that doesn't really do anything.
So, one way to fix this would be in expand_default_init check for rval
being a TARGET with TREE_CONSTANT as the initializer and if it is that, build
an INIT_EXPR like it e.g. does that for constexpr ctors.
--- gcc/cp/init.c.jj2020-09-10 11:24:05.019805303 +0200
+++ gcc/cp/init.c   2020-09-14 15:06:59.467341241 +0200
@@ -1999,6 +1999,9 @@ expand_default_init (tree binfo, tree tr
rval = build2 (INIT_EXPR, type, exp, e);
}
 }
+  else if (TREE_CODE (rval) == TARGET_EXPR
+  && TREE_CONSTANT (TARGET_EXPR_INITIAL (rval)))
+rval = build2 (INIT_EXPR, type, exp, rval);

   /* FIXME put back convert_to_void?  */
   if (TREE_SIDE_EFFECTS (rval))
fixes the testcase

[Bug c++/96994] Missing code from consteval constructor initializing const variable

2020-09-10 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

--- Comment #6 from Jakub Jelinek  ---
Though, when the ctor is constexpr, it is constant initialized even without it,
so probably the bug is somewhere else.

[Bug c++/96994] Missing code from consteval constructor initializing const variable

2020-09-10 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

--- Comment #5 from Jakub Jelinek  ---
More complete testcase:
struct A { consteval A () { i = 1; } consteval A (int x) : i (x) {} int i = 0;
};
struct B { constexpr B () { i = 1; } constexpr B (int x) : i (x) {} int i = 0;
};
A const a;
constexpr A b;
B const c;
A const constinit d;
A const e = 2;
constexpr A f = 3;
B const g = 4;
A const constinit h = 5;
A i;
B j;
A k = 6;
B l = 7;
static_assert (b.i == 1 && f.i == 3);

int
main()
{
  if (a.i != 1 || c.i != 1 || d.i != 1 || e.i != 2 || g.i != 4 || h.i != 5
  || i.i != 1 || j.i != 1 || k.i != 6 || l.i != 7)
__builtin_abort ();
}

[Bug c++/96994] Missing code from consteval constructor initializing const variable

2020-09-10 Thread mpolacek at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

--- Comment #4 from Marek Polacek  ---
Yes, a way to fix this would be to do the build_functional_cast in
check_initializer:

 6892   else if (DECL_DECLARED_CONSTEXPR_P (decl)
 6893|| (flags & LOOKUP_CONSTINIT))
 6894 {  
 6895   /* Declared constexpr or constinit, but no suitable
initializer;
 6896  massage init appropriately so we can pass it into
 6897  store_init_value for the error.  */
 6898   if (CLASS_TYPE_P (type)
 6899   && (!init || TREE_CODE (init) == TREE_LIST))
 6900 { 
 6901   init = build_functional_cast (input_location, type,
 6902 init, tf_none);

[Bug c++/96994] Missing code from consteval constructor initializing const variable

2020-09-10 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

--- Comment #3 from Jakub Jelinek  ---
Though:
struct A { consteval A (int x) { i = x; } int i = 0; };
struct B { constexpr B (int x) { i = x; } int i = 0; };
A const a = 1;
constexpr A b = 2;
B const c = 3;
A constinit d = 4;
static_assert (b.i == 2);

int
main()
{
  if (a.i != 1 || c.i != 3 || d.i != 4)
__builtin_abort ();
}
works fine, so the problem is maybe just with default construction for
consteval ctor.

[Bug c++/96994] Missing code from consteval constructor initializing const variable

2020-09-10 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

Jakub Jelinek  changed:

   What|Removed |Added

 CC||jakub at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek  ---
Testcase without includes:
struct A { consteval A () { i = 1; } int i = 0; };
struct B { constexpr B () { i = 1; } int i = 0; };
A const a;
constexpr A b;
B const c;
A constinit d;
static_assert (b.i == 1);

int
main()
{
  if (a.i != 1 || c.i != 1 || d.i != 1)
__builtin_abort ();
}

I wonder if we shouldn't treat variables with consteval ctors like if
constinit, which as the testcase shows works fine.

[Bug c++/96994] Missing code from consteval constructor initializing const variable

2020-09-09 Thread mpolacek at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

Marek Polacek  changed:

   What|Removed |Added

 Status|UNCONFIRMED |NEW
 CC||mpolacek at gcc dot gnu.org
   Last reconfirmed||2020-09-09
   Keywords||wrong-code
 Ever confirmed|0   |1

--- Comment #1 from Marek Polacek  ---
Confirmed.