public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "pkeir at outlook dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/95977] No deallocation of temporary in return-statement during constant evaluation
Date: Wed, 29 Jul 2020 00:11:33 +0000	[thread overview]
Message-ID: <bug-95977-4-GYnAOtIAhN@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-95977-4@http.gcc.gnu.org/bugzilla/>

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.

  parent reply	other threads:[~2020-07-29  0:11 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-29 17:26 [Bug c++/95977] New: " 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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-95977-4-GYnAOtIAhN@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).