[llvm-bugs] [Bug 47745] New: Coroutine is not accessing the reference correctly in O1

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Oct 6 12:34:06 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=47745

            Bug ID: 47745
           Summary: Coroutine is not accessing the reference correctly in
                    O1
           Product: clang
           Version: 10.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++2a
          Assignee: unassignedclangbugs at nondot.org
          Reporter: pushrunkey at gmail.com
                CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
                    llvm-bugs at lists.llvm.org, richard-llvm at metafoo.co.uk

Coroutine a member function of a class that has a reference to a member
variable to access the wrong address in -O1.

https://godbolt.org/z/aE53rd


```
#include <experimental/coroutine>
#include <iostream>

struct Task {
  struct promise_type {
    auto get_return_object() { return Task{handle_type::from_promise(*this)}; }
    std::experimental::suspend_always initial_suspend() { return {}; }
    std::experimental::suspend_always final_suspend() noexcept{ return {}; }
    void return_void() {}
    void unhandled_exception() { }
  };
  using handle_type = std::experimental::coroutine_handle<promise_type>;

  handle_type coro_;
};

struct A{
    int&ch;
    Task f() {
        ch = 42;
        // expected &ch == &main.ch 
        // but &ch == coroutine_handle().address()
        std::cout << &ch <<" : "<< ch<<std::endl; 
        co_return;
    }
};

int main(){
    int ch=3;
    std::cout << &ch <<" : "<< ch<<std::endl;
    auto x = A{ch}.f().coro_;
    auto y = x;
    std::cout<<x.address() <<std::endl;
    y.resume();
    std::cout << &ch <<" : "<< ch<<std::endl;
}
```

```
$ clang++-10 main.cpp -std=c++2a -stdlib=libc++ -O1 ; ./a.out
0x7fffe85df4ac : 3
0x1369470
0x1369470 : 42
0x7fffe85df4ac : 3
```

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20201006/948f963d/attachment.html>


More information about the llvm-bugs mailing list