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

            Bug ID: 116775
           Summary: C++20 Coroutine await_suspend is unexpectedly executed
                    when using in __builtin_constant_p
           Product: gcc
           Version: 14.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sunsijie at buaa dot edu.cn
  Target Milestone: ---

```
#include <coroutine>

struct awaitable {
    awaitable(int n) : delay{n} {}

    constexpr bool await_ready() const noexcept { return false; }

    auto await_suspend(std::coroutine_handle<> h) const {
        *((int*)0) = 42;
        return false;
    }

    int await_resume() const noexcept {
        return delay;
    }

    int delay;
};

struct Task {
    struct promise_type {
        promise_type() = default;
        Task get_return_object() { return {}; }
        std::suspend_never initial_suspend() { return {}; }
        std::suspend_always final_suspend() noexcept { return {}; }
        void unhandled_exception() {}
    };
};

Task foo() noexcept {
    (void)__builtin_constant_p((co_await awaitable{10}));
}

//call foo
int main() {
    foo();
}
```

__builtin_constant_p is described as `The expression is not evaluated,
side-effects are discarded.`, but in above example, the await_suspend is
executed unexpectedly.

Reply via email to