public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/97435] New: Lifetime of temporaries not correctly extending when optiimzation are enabled
@ 2020-10-15  6:44 chris at chrissavoie dot com
  2020-10-15  7:33 ` [Bug c++/97435] " rguenth at gcc dot gnu.org
  2020-10-15  8:51 ` redi at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: chris at chrissavoie dot com @ 2020-10-15  6:44 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 97435
           Summary: Lifetime of temporaries not correctly extending when
                    optiimzation are enabled
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: chris at chrissavoie dot com
  Target Milestone: ---

Godbolt example: https://godbolt.org/z/fssed1

The following code:
struct A {
    __attribute__((noinline))
    A(int i) : data(i) {}
    A& operator+=(int i) { data += i; return *this; }
    int data;
};

int main() {
    A const& b = A(5) += 5;
    A c(6);
    return b.data + c.data; // 16 expected
}

Will return 16 when compiled without optimizations, 11 when compiled with
optimizations (-O1 or greater) and 6 with noinline disabled and optimizations
on.

Browsing versions on godbolt it looks like the inline version of the bug
appeared between versions 4.9.3 and 5.1. The noinline version appeared between
versions 4.6.4 and 4.7.3.

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

* [Bug c++/97435] Lifetime of temporaries not correctly extending when optiimzation are enabled
  2020-10-15  6:44 [Bug c++/97435] New: Lifetime of temporaries not correctly extending when optiimzation are enabled chris at chrissavoie dot com
@ 2020-10-15  7:33 ` rguenth at gcc dot gnu.org
  2020-10-15  8:51 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-10-15  7:33 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
I believe the A(5) object doesn't live beyond the stmts lifetime and thus this
is invalid.

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

* [Bug c++/97435] Lifetime of temporaries not correctly extending when optiimzation are enabled
  2020-10-15  6:44 [Bug c++/97435] New: Lifetime of temporaries not correctly extending when optiimzation are enabled chris at chrissavoie dot com
  2020-10-15  7:33 ` [Bug c++/97435] " rguenth at gcc dot gnu.org
@ 2020-10-15  8:51 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: redi at gcc dot gnu.org @ 2020-10-15  8:51 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Yes, this is undefined behaviour.

It's equivalent to:

const int& f(const int& i) { return i; }
const int& i = f(5);
int j = i;  // undefined

Lifetime is only extended if the reference is bound directly to the temporary
object, not to another reference that happens to be bound to it inside some
function.

Consider the case where the body of A::operator+= is not visible to the
compiler. How is it supposed to know whether the returned value refers to *this
or not, and therefore how is it supposed to know whether to destroy the
temporary A(5) immediately or not?

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

end of thread, other threads:[~2020-10-15  8:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-15  6:44 [Bug c++/97435] New: Lifetime of temporaries not correctly extending when optiimzation are enabled chris at chrissavoie dot com
2020-10-15  7:33 ` [Bug c++/97435] " rguenth at gcc dot gnu.org
2020-10-15  8:51 ` redi 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).