public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/100850] New: [coroutine] Wrong addresses of variables captured by reference into lambda co-routines.
@ 2021-06-01  8:37 vsolontsov at volanttrading dot com
  2021-06-01  8:39 ` [Bug c++/100850] " vsolontsov at volanttrading dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: vsolontsov at volanttrading dot com @ 2021-06-01  8:37 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 100850
           Summary: [coroutine] Wrong addresses of variables captured by
                    reference into lambda co-routines.
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vsolontsov at volanttrading dot com
  Target Milestone: ---

In a code like this:
```
    int i;

    auto coro = [&i, pi = &i](int& i1) -> task {
//        co_await std::suspend_always{};
        std::cout << "&i==&i1: " << std::boolalpha << (&i == &i1) << std::endl;
        std::cout << "&i==pi: " << std::boolalpha << (&i == pi) << std::endl;
        std::cout << "&i1==pi: " << std::boolalpha << (&i1 == pi) << std::endl;
        co_return;
    }(i);
```
If the captured variables are not used until the very first suspension, the
addresses appear to be wrong. https://godbolt.org/z/c945q893j
If the `task::initial_suspend()` returns `suspend_never` and the `co_await` in
the co-routine is commented out, all good. Otherwise, if there's a suspension
before the first use of the variables, the address get screwed.

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

* [Bug c++/100850] [coroutine] Wrong addresses of variables captured by reference into lambda co-routines.
  2021-06-01  8:37 [Bug c++/100850] New: [coroutine] Wrong addresses of variables captured by reference into lambda co-routines vsolontsov at volanttrading dot com
@ 2021-06-01  8:39 ` vsolontsov at volanttrading dot com
  2021-06-01  8:41 ` vsolontsov at volanttrading dot com
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: vsolontsov at volanttrading dot com @ 2021-06-01  8:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Vlad <vsolontsov at volanttrading dot com> ---
Forgot to mention... It seems to be a regression -- gcc 10.2 and 10.3 don't
demonstrate the issue on this example.

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

* [Bug c++/100850] [coroutine] Wrong addresses of variables captured by reference into lambda co-routines.
  2021-06-01  8:37 [Bug c++/100850] New: [coroutine] Wrong addresses of variables captured by reference into lambda co-routines vsolontsov at volanttrading dot com
  2021-06-01  8:39 ` [Bug c++/100850] " vsolontsov at volanttrading dot com
@ 2021-06-01  8:41 ` vsolontsov at volanttrading dot com
  2021-06-01 16:18 ` vsolontsov at volanttrading dot com
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: vsolontsov at volanttrading dot com @ 2021-06-01  8:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Vlad <vsolontsov at volanttrading dot com> ---
Yet another piece -- appears with -O2/-O3/-Os.
-O0/-O1 is free from this bug.

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

* [Bug c++/100850] [coroutine] Wrong addresses of variables captured by reference into lambda co-routines.
  2021-06-01  8:37 [Bug c++/100850] New: [coroutine] Wrong addresses of variables captured by reference into lambda co-routines vsolontsov at volanttrading dot com
  2021-06-01  8:39 ` [Bug c++/100850] " vsolontsov at volanttrading dot com
  2021-06-01  8:41 ` vsolontsov at volanttrading dot com
@ 2021-06-01 16:18 ` vsolontsov at volanttrading dot com
  2021-06-02  9:51 ` iains at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: vsolontsov at volanttrading dot com @ 2021-06-01 16:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Vlad <vsolontsov at volanttrading dot com> ---
My bad. It's actually a UB. The lambda lifetime is just over by the moment of
resumption of the co-routine.

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

* [Bug c++/100850] [coroutine] Wrong addresses of variables captured by reference into lambda co-routines.
  2021-06-01  8:37 [Bug c++/100850] New: [coroutine] Wrong addresses of variables captured by reference into lambda co-routines vsolontsov at volanttrading dot com
                   ` (2 preceding siblings ...)
  2021-06-01 16:18 ` vsolontsov at volanttrading dot com
@ 2021-06-02  9:51 ` iains at gcc dot gnu.org
  2021-06-02 10:14 ` vsolontsov at volanttrading dot com
  2021-06-02 10:20 ` iains at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: iains at gcc dot gnu.org @ 2021-06-02  9:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Iain Sandoe <iains at gcc dot gnu.org> ---
(In reply to Vlad from comment #3)
> My bad. It's actually a UB. The lambda lifetime is just over by the moment
> of resumption of the co-routine.

(oddly enough) we were discussing thia in a BSI meeting yesterday, perhaps the
compiler can do more to warn you if you are making a "dangerous" use of
reference.

The standard itself has a warning about it - but most people don't read the
standard, of course.

Can this be closed then?

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

* [Bug c++/100850] [coroutine] Wrong addresses of variables captured by reference into lambda co-routines.
  2021-06-01  8:37 [Bug c++/100850] New: [coroutine] Wrong addresses of variables captured by reference into lambda co-routines vsolontsov at volanttrading dot com
                   ` (3 preceding siblings ...)
  2021-06-02  9:51 ` iains at gcc dot gnu.org
@ 2021-06-02 10:14 ` vsolontsov at volanttrading dot com
  2021-06-02 10:20 ` iains at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: vsolontsov at volanttrading dot com @ 2021-06-02 10:14 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Vlad <vsolontsov at volanttrading dot com> ---
> Can this be closed then?

Sure. Thanks you very much! 

For the history, before it's closed I'd like to leave this reference:
https://stackoverflow.com/questions/60592174/lambda-lifetime-explanation-for-c20-coroutines

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

* [Bug c++/100850] [coroutine] Wrong addresses of variables captured by reference into lambda co-routines.
  2021-06-01  8:37 [Bug c++/100850] New: [coroutine] Wrong addresses of variables captured by reference into lambda co-routines vsolontsov at volanttrading dot com
                   ` (4 preceding siblings ...)
  2021-06-02 10:14 ` vsolontsov at volanttrading dot com
@ 2021-06-02 10:20 ` iains at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: iains at gcc dot gnu.org @ 2021-06-02 10:20 UTC (permalink / raw)
  To: gcc-bugs

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

Iain Sandoe <iains at gcc dot gnu.org> changed:

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

--- Comment #6 from Iain Sandoe <iains at gcc dot gnu.org> ---
this is a known "gotcha" with coroutines and use of references (it's accepted
that perhaps there could be better diagnostics about it).

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

end of thread, other threads:[~2021-06-02 10:20 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-01  8:37 [Bug c++/100850] New: [coroutine] Wrong addresses of variables captured by reference into lambda co-routines vsolontsov at volanttrading dot com
2021-06-01  8:39 ` [Bug c++/100850] " vsolontsov at volanttrading dot com
2021-06-01  8:41 ` vsolontsov at volanttrading dot com
2021-06-01 16:18 ` vsolontsov at volanttrading dot com
2021-06-02  9:51 ` iains at gcc dot gnu.org
2021-06-02 10:14 ` vsolontsov at volanttrading dot com
2021-06-02 10:20 ` iains 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).