public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/95977] New: No deallocation of temporary in return-statement during constant evaluation
@ 2020-06-29 17:26 johelegp at gmail dot com
  2020-06-29 21:39 ` [Bug c++/95977] " johelegp at gmail dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: johelegp at gmail dot com @ 2020-06-29 17:26 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 95977
           Summary: No deallocation of temporary in return-statement
                    during constant evaluation
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: johelegp at gmail dot com
  Target Milestone: ---

See https://godbolt.org/z/iyRFxf.
```C++
struct X {
    int* x{new int{42}};
    X() = default;
    constexpr X(const X& x) : x{new int{*x.x}} { }
    constexpr ~X() { delete x; }
};
constexpr int f() { X x; return *X{x}.x; }
constexpr int z{f()};
```
```
<source>:4:45: error: 'f()' is not a constant expression because allocated
storage has not been deallocated

    4 |     constexpr X(const X& x) : x{new int{*x.x}} { }

      |                                             ^
```

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

* [Bug c++/95977] No deallocation of temporary in return-statement during constant evaluation
  2020-06-29 17:26 [Bug c++/95977] New: No deallocation of temporary in return-statement during constant evaluation johelegp at gmail dot com
@ 2020-06-29 21:39 ` johelegp at gmail dot com
  2020-07-29  0:11 ` pkeir at outlook dot com
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: johelegp at gmail dot com @ 2020-06-29 21:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Johel Ernesto Guerrero Peña <johelegp at gmail dot com> ---
Slightly simplified: https://godbolt.org/z/9unpTF.
```C++
struct X {
    int* x{new int{42}};
    constexpr ~X() { delete x; }
};
constexpr int f() { return *X{}.x; }
constexpr int z{f()};
```

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

* [Bug c++/95977] No deallocation of temporary in return-statement during constant evaluation
  2020-06-29 17:26 [Bug c++/95977] New: No deallocation of temporary in return-statement during constant evaluation johelegp at gmail dot com
  2020-06-29 21:39 ` [Bug c++/95977] " johelegp at gmail dot com
@ 2020-07-29  0:11 ` pkeir at outlook dot com
  2020-11-04 15:41 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pkeir at outlook dot com @ 2020-07-29  0:11 UTC (permalink / raw)
  To: gcc-bugs

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

Paul Keir <pkeir at outlook dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pkeir at outlook dot com

--- Comment #2 from Paul Keir <pkeir at outlook dot com> ---
I've also encountered this bug. I changed your code to:

struct X {
    int* x{new int{42}};
    constexpr ~X() { delete x; }
};

int main(int argc, char *argv[])
{
  static_assert(42 == *X{}.x);
  return 0;
}

...and now an extra note appears at compilation:

note: ‘<anonymous>’ was not declared ‘constexpr’

15 |   static_assert(42 == *X{}.x);
   |                          ^

This made me think of the implicitly-defined default constructor for X, which
should be `constexpr`. But perhaps attribution of this `constexpr` qualifier is
failing due to the memory not having been freed at the point where the equality
is evaluated? This is certainly unintuitive, but I do note that the error
disappears when `static_assert(doit())` is used instead, with the following
definition: 

constexpr bool doit()
{
  int i = *X{}.x;
  return i==42;
}

...but while that perhaps make sense, I encountered two other changes to the
version above which surprisingly also remove the error:

1) Adding an explicitly defaulted constructor for X;
2) Using `X()` rather than `X{}` in the static assert.

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

* [Bug c++/95977] No deallocation of temporary in return-statement during constant evaluation
  2020-06-29 17:26 [Bug c++/95977] New: No deallocation of temporary in return-statement during constant evaluation johelegp at gmail dot com
  2020-06-29 21:39 ` [Bug c++/95977] " johelegp at gmail dot com
  2020-07-29  0:11 ` pkeir at outlook dot com
@ 2020-11-04 15:41 ` redi at gcc dot gnu.org
  2021-08-29  0:03 ` johelegp at gmail dot com
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2020-11-04 15:41 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2020-11-04
     Ever confirmed|0                           |1

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

* [Bug c++/95977] No deallocation of temporary in return-statement during constant evaluation
  2020-06-29 17:26 [Bug c++/95977] New: No deallocation of temporary in return-statement during constant evaluation johelegp at gmail dot com
                   ` (2 preceding siblings ...)
  2020-11-04 15:41 ` redi at gcc dot gnu.org
@ 2021-08-29  0:03 ` johelegp at gmail dot com
  2021-08-29  0:15 ` pinskia at gcc dot gnu.org
  2021-12-08  8:27 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: johelegp at gmail dot com @ 2021-08-29  0:03 UTC (permalink / raw)
  To: gcc-bugs

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

Johel Ernesto Guerrero Peña <johelegp at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
      Known to work|                            |10.3.0, 11.1.0
      Known to fail|                            |10.2.0
         Resolution|---                         |FIXED

--- Comment #3 from Johel Ernesto Guerrero Peña <johelegp at gmail dot com> ---
Fixed somewhere along the line.

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

* [Bug c++/95977] No deallocation of temporary in return-statement during constant evaluation
  2020-06-29 17:26 [Bug c++/95977] New: No deallocation of temporary in return-statement during constant evaluation johelegp at gmail dot com
                   ` (3 preceding siblings ...)
  2021-08-29  0:03 ` johelegp at gmail dot com
@ 2021-08-29  0:15 ` pinskia at gcc dot gnu.org
  2021-12-08  8:27 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-29  0:15 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=99705

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I think this was fixed via the patch for PR 99705.

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

* [Bug c++/95977] No deallocation of temporary in return-statement during constant evaluation
  2020-06-29 17:26 [Bug c++/95977] New: No deallocation of temporary in return-statement during constant evaluation johelegp at gmail dot com
                   ` (4 preceding siblings ...)
  2021-08-29  0:15 ` pinskia at gcc dot gnu.org
@ 2021-12-08  8:27 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-08  8:27 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |10.3

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

end of thread, other threads:[~2021-12-08  8:27 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-29 17:26 [Bug c++/95977] New: No deallocation of temporary in return-statement during constant evaluation johelegp at gmail dot com
2020-06-29 21:39 ` [Bug c++/95977] " johelegp at gmail dot com
2020-07-29  0:11 ` pkeir at outlook dot com
2020-11-04 15:41 ` redi at gcc dot gnu.org
2021-08-29  0:03 ` johelegp at gmail dot com
2021-08-29  0:15 ` pinskia at gcc dot gnu.org
2021-12-08  8:27 ` pinskia 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).