public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/104055] New: Temporary with conteval constructor is ignored
@ 2022-01-16 20:56 fchelnokov at gmail dot com
  2022-01-17  1:05 ` [Bug c++/104055] " pinskia at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: fchelnokov at gmail dot com @ 2022-01-16 20:56 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 104055
           Summary: Temporary with conteval constructor is ignored
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fchelnokov at gmail dot com
  Target Milestone: ---

Execution of the following program
```
#include <iostream>

int g = 0;

struct A { 
    ~A() { g = 1; }
    consteval A() {}
};

int main() {
    A{};
    std::cout << g;
}
```
prints `0` in GCC. Demo: https://gcc.godbolt.org/z/7xxn67WbM

This means that the destructor of the temporary (which sets g=1) is not called.

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

* [Bug c++/104055] Temporary with conteval constructor is ignored
  2022-01-16 20:56 [Bug c++/104055] New: Temporary with conteval constructor is ignored fchelnokov at gmail dot com
@ 2022-01-17  1:05 ` pinskia at gcc dot gnu.org
  2022-01-17 10:46 ` [Bug c++/104055] Temporary with consteval constructor does not cause the deconstructor to be called jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-17  1:05 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2022-01-17
     Ever confirmed|0                           |1

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed simplified testcase:
extern "C" void exit(int);
extern "C" void abort(void);
struct A { 
    ~A() { exit(0); }
    consteval A() {}
};

int main() {
    {
      A{};
    }
    abort();
}

Here is a different testcase which shows the same problem too:
extern "C" void exit(int);
extern "C" void abort(void);
struct A { 
    ~A() { exit(0); }
    consteval A() {}
};

template <class T>
struct f
{
    f(T){}
};

f t(A{});

int main() {
  return 1;
}

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

* [Bug c++/104055] Temporary with consteval constructor does not cause the deconstructor to be called
  2022-01-16 20:56 [Bug c++/104055] New: Temporary with conteval constructor is ignored fchelnokov at gmail dot com
  2022-01-17  1:05 ` [Bug c++/104055] " pinskia at gcc dot gnu.org
@ 2022-01-17 10:46 ` jakub at gcc dot gnu.org
  2022-01-17 10:59 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-01-17 10:46 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Because of the consteval build_over_call will do:
9920              if (obj_arg && is_dummy_object (obj_arg))
9921                {
9922                  call = build_cplus_new (DECL_CONTEXT (fndecl), call,
complain);
9923                  obj_arg = NULL_TREE;
9924                }
where the build_cplus_new creates a TARGET_EXPR with TARGET_EXPR_CLEANUP of
A::~A (&temp) on it.
But then it calls
9938              call = cxx_constant_value (call, obj_arg);
and cxx_constant_value triggers:
7818      if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
7819        {
7820          r = adjust_temp_type (type, r);
7821          if (TREE_CODE (t) == TARGET_EXPR
7822              && TARGET_EXPR_INITIAL (t) == r)
7823            return t;
7824          else if (TREE_CODE (t) != CONSTRUCTOR)
7825            {
7826              r = get_target_expr_sfinae (r, tf_warning_or_error |
tf_no_cleanup);
7827              TREE_CONSTANT (r) = true;
7828            }
7829        }
where it creates a new TARGET_EXPR but this time without TARGET_EXPR_CLEANUP
because of the tf_no_cleanup and that is what is returned.
I've tried to copy TARGET_EXPR_CLEANUP from t to r either in the above
constexpr.c spot, or in build_over_call, but neither worked as the cleanup
refers to a different slot.  Now, I think the initializer shouldn't be refering
to the slot decl (e.g. take its address), because that wouldn't be a constant
expression.

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

* [Bug c++/104055] Temporary with consteval constructor does not cause the deconstructor to be called
  2022-01-16 20:56 [Bug c++/104055] New: Temporary with conteval constructor is ignored fchelnokov at gmail dot com
  2022-01-17  1:05 ` [Bug c++/104055] " pinskia at gcc dot gnu.org
  2022-01-17 10:46 ` [Bug c++/104055] Temporary with consteval constructor does not cause the deconstructor to be called jakub at gcc dot gnu.org
@ 2022-01-17 10:59 ` pinskia at gcc dot gnu.org
  2022-01-17 11:03 ` jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-17 10:59 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #2)
> Now, I think the initializer shouldn't be
> refering to the slot decl (e.g. take its address), because that wouldn't be
> a constant expression.

Note that sounds similar to the issue in PR 104000 :) (which I just happened to
look and then start go down a hole looking into consteval bug reports).

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

* [Bug c++/104055] Temporary with consteval constructor does not cause the deconstructor to be called
  2022-01-16 20:56 [Bug c++/104055] New: Temporary with conteval constructor is ignored fchelnokov at gmail dot com
                   ` (2 preceding siblings ...)
  2022-01-17 10:59 ` pinskia at gcc dot gnu.org
@ 2022-01-17 11:03 ` jakub at gcc dot gnu.org
  2022-01-18 23:44 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-01-17 11:03 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 52211
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52211&action=edit
gcc12-pr104055.patch

This untested patch fixes it, but dunno what the right fix is...

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

* [Bug c++/104055] Temporary with consteval constructor does not cause the deconstructor to be called
  2022-01-16 20:56 [Bug c++/104055] New: Temporary with conteval constructor is ignored fchelnokov at gmail dot com
                   ` (3 preceding siblings ...)
  2022-01-17 11:03 ` jakub at gcc dot gnu.org
@ 2022-01-18 23:44 ` cvs-commit at gcc dot gnu.org
  2022-01-24  9:21 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-01-18 23:44 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 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:1a5145f1e3adf8b2ba4ad416a5ddef59a1e34d48

commit r12-6705-g1a5145f1e3adf8b2ba4ad416a5ddef59a1e34d48
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Jan 19 00:42:18 2022 +0100

    c++: Fix handling of temporaries with consteval ctors and non-trivial dtors
[PR104055]

    The following testcase is miscompiled.  We see the constructor is
immediate,
    in build_over_call we trigger:
              if (obj_arg && is_dummy_object (obj_arg))
                {
                  call = build_cplus_new (DECL_CONTEXT (fndecl), call,
complain);
                  obj_arg = NULL_TREE;
                }
    which makes call a TARGET_EXPR with the dtor in TARGET_EXPR_CLEANUP,
    but then call cxx_constant_value on it.  In
cxx_eval_outermost_constant_expr
    it triggers the:
          else if (TREE_CODE (t) != CONSTRUCTOR)
            {
              r = get_target_expr_sfinae (r, tf_warning_or_error |
tf_no_cleanup);
              TREE_CONSTANT (r) = true;
            }
    which wraps the CONSTRUCTOR r into a new TARGET_EXPR, but one without
    dtors (I think we need e.g. the TREE_CONSTANT for the callers),
    and finally build_over_call uses that.

    The following patch fixes that by using get_target_expr instead
    of get_target_expr_sfinae + TREE_CONSTANT (r) = true if t is
    a TARGET_EXPR with non-NULL TARGET_EXPR_CLEANUP.

    2022-01-19  Jakub Jelinek  <jakub@redhat.com>

            PR c++/104055
            * constexpr.cc (cxx_eval_outermost_constant_expr): If t is a
            TARGET_EXPR with TARGET_EXPR_CLEANUP, use get_target_expr rather
            than get_target_expr_sfinae with tf_no_cleanup, and don't set
            TREE_CONSTANT.

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

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

* [Bug c++/104055] Temporary with consteval constructor does not cause the deconstructor to be called
  2022-01-16 20:56 [Bug c++/104055] New: Temporary with conteval constructor is ignored fchelnokov at gmail dot com
                   ` (4 preceding siblings ...)
  2022-01-18 23:44 ` cvs-commit at gcc dot gnu.org
@ 2022-01-24  9:21 ` cvs-commit at gcc dot gnu.org
  2022-05-10  8:22 ` cvs-commit at gcc dot gnu.org
  2022-05-10 10:17 ` jakub at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-01-24  9:21 UTC (permalink / raw)
  To: gcc-bugs

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

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

https://gcc.gnu.org/g:91ac88cada3960517415bc92ae6d2d13a5e779a0

commit r11-9502-g91ac88cada3960517415bc92ae6d2d13a5e779a0
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Jan 19 00:42:18 2022 +0100

    c++: Fix handling of temporaries with consteval ctors and non-trivial dtors
[PR104055]

    The following testcase is miscompiled.  We see the constructor is
immediate,
    in build_over_call we trigger:
              if (obj_arg && is_dummy_object (obj_arg))
                {
                  call = build_cplus_new (DECL_CONTEXT (fndecl), call,
complain);
                  obj_arg = NULL_TREE;
                }
    which makes call a TARGET_EXPR with the dtor in TARGET_EXPR_CLEANUP,
    but then call cxx_constant_value on it.  In
cxx_eval_outermost_constant_expr
    it triggers the:
          else if (TREE_CODE (t) != CONSTRUCTOR)
            {
              r = get_target_expr_sfinae (r, tf_warning_or_error |
tf_no_cleanup);
              TREE_CONSTANT (r) = true;
            }
    which wraps the CONSTRUCTOR r into a new TARGET_EXPR, but one without
    dtors (I think we need e.g. the TREE_CONSTANT for the callers),
    and finally build_over_call uses that.

    The following patch fixes that by using get_target_expr instead
    of get_target_expr_sfinae + TREE_CONSTANT (r) = true if t is
    a TARGET_EXPR with non-NULL TARGET_EXPR_CLEANUP.

    2022-01-19  Jakub Jelinek  <jakub@redhat.com>

            PR c++/104055
            * constexpr.c (cxx_eval_outermost_constant_expr): If t is a
            TARGET_EXPR with TARGET_EXPR_CLEANUP, use get_target_expr rather
            than get_target_expr_sfinae with tf_no_cleanup, and don't set
            TREE_CONSTANT.

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

    (cherry picked from commit 1a5145f1e3adf8b2ba4ad416a5ddef59a1e34d48)

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

* [Bug c++/104055] Temporary with consteval constructor does not cause the deconstructor to be called
  2022-01-16 20:56 [Bug c++/104055] New: Temporary with conteval constructor is ignored fchelnokov at gmail dot com
                   ` (5 preceding siblings ...)
  2022-01-24  9:21 ` cvs-commit at gcc dot gnu.org
@ 2022-05-10  8:22 ` cvs-commit at gcc dot gnu.org
  2022-05-10 10:17 ` jakub at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-05-10  8:22 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 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:1c3c885e1bba86f01917445928006d26062c5b15

commit r10-10668-g1c3c885e1bba86f01917445928006d26062c5b15
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Jan 19 00:42:18 2022 +0100

    c++: Fix handling of temporaries with consteval ctors and non-trivial dtors
[PR104055]

    The following testcase is miscompiled.  We see the constructor is
immediate,
    in build_over_call we trigger:
              if (obj_arg && is_dummy_object (obj_arg))
                {
                  call = build_cplus_new (DECL_CONTEXT (fndecl), call,
complain);
                  obj_arg = NULL_TREE;
                }
    which makes call a TARGET_EXPR with the dtor in TARGET_EXPR_CLEANUP,
    but then call cxx_constant_value on it.  In
cxx_eval_outermost_constant_expr
    it triggers the:
          else if (TREE_CODE (t) != CONSTRUCTOR)
            {
              r = get_target_expr_sfinae (r, tf_warning_or_error |
tf_no_cleanup);
              TREE_CONSTANT (r) = true;
            }
    which wraps the CONSTRUCTOR r into a new TARGET_EXPR, but one without
    dtors (I think we need e.g. the TREE_CONSTANT for the callers),
    and finally build_over_call uses that.

    The following patch fixes that by using get_target_expr instead
    of get_target_expr_sfinae + TREE_CONSTANT (r) = true if t is
    a TARGET_EXPR with non-NULL TARGET_EXPR_CLEANUP.

    2022-01-19  Jakub Jelinek  <jakub@redhat.com>

            PR c++/104055
            * constexpr.c (cxx_eval_outermost_constant_expr): If t is a
            TARGET_EXPR with TARGET_EXPR_CLEANUP, use get_target_expr rather
            than get_target_expr_sfinae with tf_no_cleanup, and don't set
            TREE_CONSTANT.

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

    (cherry picked from commit 1a5145f1e3adf8b2ba4ad416a5ddef59a1e34d48)

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

* [Bug c++/104055] Temporary with consteval constructor does not cause the deconstructor to be called
  2022-01-16 20:56 [Bug c++/104055] New: Temporary with conteval constructor is ignored fchelnokov at gmail dot com
                   ` (6 preceding siblings ...)
  2022-05-10  8:22 ` cvs-commit at gcc dot gnu.org
@ 2022-05-10 10:17 ` jakub at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-05-10 10:17 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |jakub at gcc dot gnu.org
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #8 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed for 10.4 too.

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

end of thread, other threads:[~2022-05-10 10:17 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-16 20:56 [Bug c++/104055] New: Temporary with conteval constructor is ignored fchelnokov at gmail dot com
2022-01-17  1:05 ` [Bug c++/104055] " pinskia at gcc dot gnu.org
2022-01-17 10:46 ` [Bug c++/104055] Temporary with consteval constructor does not cause the deconstructor to be called jakub at gcc dot gnu.org
2022-01-17 10:59 ` pinskia at gcc dot gnu.org
2022-01-17 11:03 ` jakub at gcc dot gnu.org
2022-01-18 23:44 ` cvs-commit at gcc dot gnu.org
2022-01-24  9:21 ` cvs-commit at gcc dot gnu.org
2022-05-10  8:22 ` cvs-commit at gcc dot gnu.org
2022-05-10 10:17 ` 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).