public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/96994] New: Missing code from consteval constructor initializing const variable
@ 2020-09-09 11:27 sbergman at redhat dot com
  2020-09-09 22:06 ` [Bug c++/96994] " mpolacek at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: sbergman at redhat dot com @ 2020-09-09 11:27 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

            Bug ID: 96994
           Summary: Missing code from consteval constructor initializing
                    const variable
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sbergman at redhat dot com
  Target Milestone: ---

At least with a local build of recent GCC 11 trunk and with
gcc-c++-10.2.1-1.fc32.x86_64:

> $ cat test.cc
> #include <iostream>
> struct S1 {
>     consteval S1() { i = 1; }
>     int i = 0;
> };
> struct S2 {
>     constexpr S2() { i = 1; }
>     int i = 0;
> };
> S1 const s1a;
> constexpr S1 s1b;
> S2 const s2;
> int main() { std::cout << s1a.i << ' ' << s1b.i << ' ' << s2.i << '\n'; }

> $ g++ -std=c++20 test.cc
> $ ./a.out
> 0 1 1

The first "0" is not as expected, the following two "1" demonstrate how slight
modifications of the code would lead to the expected outcome.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/96994] Missing code from consteval constructor initializing const variable
  2020-09-09 11:27 [Bug c++/96994] New: Missing code from consteval constructor initializing const variable sbergman at redhat dot com
@ 2020-09-09 22:06 ` mpolacek at gcc dot gnu.org
  2020-09-10 17:52 ` jakub at gcc dot gnu.org
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-09-09 22:06 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

Marek Polacek <mpolacek at gcc dot gnu.org> 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 <mpolacek at gcc dot gnu.org> ---
Confirmed.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/96994] Missing code from consteval constructor initializing const variable
  2020-09-09 11:27 [Bug c++/96994] New: Missing code from consteval constructor initializing const variable sbergman at redhat dot com
  2020-09-09 22:06 ` [Bug c++/96994] " mpolacek at gcc dot gnu.org
@ 2020-09-10 17:52 ` jakub at gcc dot gnu.org
  2020-09-10 17:55 ` jakub at gcc dot gnu.org
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-09-10 17:52 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
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.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/96994] Missing code from consteval constructor initializing const variable
  2020-09-09 11:27 [Bug c++/96994] New: Missing code from consteval constructor initializing const variable sbergman at redhat dot com
  2020-09-09 22:06 ` [Bug c++/96994] " mpolacek at gcc dot gnu.org
  2020-09-10 17:52 ` jakub at gcc dot gnu.org
@ 2020-09-10 17:55 ` jakub at gcc dot gnu.org
  2020-09-10 18:00 ` mpolacek at gcc dot gnu.org
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-09-10 17:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
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.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/96994] Missing code from consteval constructor initializing const variable
  2020-09-09 11:27 [Bug c++/96994] New: Missing code from consteval constructor initializing const variable sbergman at redhat dot com
                   ` (2 preceding siblings ...)
  2020-09-10 17:55 ` jakub at gcc dot gnu.org
@ 2020-09-10 18:00 ` mpolacek at gcc dot gnu.org
  2020-09-10 18:10 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-09-10 18:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

--- Comment #4 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
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);

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/96994] Missing code from consteval constructor initializing const variable
  2020-09-09 11:27 [Bug c++/96994] New: Missing code from consteval constructor initializing const variable sbergman at redhat dot com
                   ` (3 preceding siblings ...)
  2020-09-10 18:00 ` mpolacek at gcc dot gnu.org
@ 2020-09-10 18:10 ` jakub at gcc dot gnu.org
  2020-09-10 18:11 ` jakub at gcc dot gnu.org
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-09-10 18:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
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 ();
}

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/96994] Missing code from consteval constructor initializing const variable
  2020-09-09 11:27 [Bug c++/96994] New: Missing code from consteval constructor initializing const variable sbergman at redhat dot com
                   ` (4 preceding siblings ...)
  2020-09-10 18:10 ` jakub at gcc dot gnu.org
@ 2020-09-10 18:11 ` jakub at gcc dot gnu.org
  2020-09-14 13:11 ` jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-09-10 18:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Though, when the ctor is constexpr, it is constant initialized even without it,
so probably the bug is somewhere else.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/96994] Missing code from consteval constructor initializing const variable
  2020-09-09 11:27 [Bug c++/96994] New: Missing code from consteval constructor initializing const variable sbergman at redhat dot com
                   ` (5 preceding siblings ...)
  2020-09-10 18:11 ` jakub at gcc dot gnu.org
@ 2020-09-14 13:11 ` jakub at gcc dot gnu.org
  2020-09-21 20:08 ` mpolacek at gcc dot gnu.org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-09-14 13:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
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.jj    2020-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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/96994] Missing code from consteval constructor initializing const variable
  2020-09-09 11:27 [Bug c++/96994] New: Missing code from consteval constructor initializing const variable sbergman at redhat dot com
                   ` (6 preceding siblings ...)
  2020-09-14 13:11 ` jakub at gcc dot gnu.org
@ 2020-09-21 20:08 ` mpolacek at gcc dot gnu.org
  2020-10-01  9:19 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-09-21 20:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch

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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/96994] Missing code from consteval constructor initializing const variable
  2020-09-09 11:27 [Bug c++/96994] New: Missing code from consteval constructor initializing const variable sbergman at redhat dot com
                   ` (7 preceding siblings ...)
  2020-09-21 20:08 ` mpolacek at gcc dot gnu.org
@ 2020-10-01  9:19 ` cvs-commit at gcc dot gnu.org
  2020-10-05  8:25 ` cvs-commit at gcc dot gnu.org
  2021-04-20 15:12 ` jakub at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-10-01  9:19 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

--- Comment #9 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

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

commit r11-3582-g56da736cc6ced0f1c339744321a14ae569db8606
Author: Jakub Jelinek <jakub@redhat.com>
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  <jakub@redhat.com>

            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.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/96994] Missing code from consteval constructor initializing const variable
  2020-09-09 11:27 [Bug c++/96994] New: Missing code from consteval constructor initializing const variable sbergman at redhat dot com
                   ` (8 preceding siblings ...)
  2020-10-01  9:19 ` cvs-commit at gcc dot gnu.org
@ 2020-10-05  8:25 ` cvs-commit at gcc dot gnu.org
  2021-04-20 15:12 ` jakub at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-10-05  8:25 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

--- Comment #10 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-10 branch has been updated by Jakub Jelinek
<jakub@gcc.gnu.org>:

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

commit r10-8853-g2513dad670c00dd9db3a85be348f6f4020b18b81
Author: Jakub Jelinek <jakub@redhat.com>
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  <jakub@redhat.com>

            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)

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/96994] Missing code from consteval constructor initializing const variable
  2020-09-09 11:27 [Bug c++/96994] New: Missing code from consteval constructor initializing const variable sbergman at redhat dot com
                   ` (9 preceding siblings ...)
  2020-10-05  8:25 ` cvs-commit at gcc dot gnu.org
@ 2021-04-20 15:12 ` jakub at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-04-20 15:12 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96994

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #11 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2021-04-20 15:12 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-09 11:27 [Bug c++/96994] New: Missing code from consteval constructor initializing const variable sbergman at redhat dot com
2020-09-09 22:06 ` [Bug c++/96994] " mpolacek at gcc dot gnu.org
2020-09-10 17:52 ` jakub at gcc dot gnu.org
2020-09-10 17:55 ` jakub at gcc dot gnu.org
2020-09-10 18:00 ` mpolacek at gcc dot gnu.org
2020-09-10 18:10 ` jakub at gcc dot gnu.org
2020-09-10 18:11 ` jakub at gcc dot gnu.org
2020-09-14 13:11 ` jakub at gcc dot gnu.org
2020-09-21 20:08 ` mpolacek at gcc dot gnu.org
2020-10-01  9:19 ` cvs-commit at gcc dot gnu.org
2020-10-05  8:25 ` cvs-commit at gcc dot gnu.org
2021-04-20 15:12 ` jakub at gcc dot gnu.org

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).