Bug 118903 - constexpr variables with co_await expression in its initialization expression
Summary: constexpr variables with co_await expression in its initialization expression
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 15.0
: P3 normal
Target Milestone: ---
Assignee: Iain Sandoe
URL: https://gcc.gnu.org/pipermail/gcc-pat...
Keywords: accepts-invalid, c++-coroutines
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2025-02-17 08:31 UTC by Hana Dusíková
Modified: 2025-07-29 15:21 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2025-02-17 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Hana Dusíková 2025-02-17 08:31:08 UTC
According 
https://eel.is/c++draft/expr.const#10.23 "an await-expression ([expr.await]);"

await expression are not allowed in constant evaluation. GCC accepts it, but silently not evaluate it.

```c++
auto test() -> super_simple_coroutine {
    constexpr auto x = co_await awaitable{};
}
```

This code should be rejected. EDG and Clang rejects it, GCC silently accepts it but attempt to read value yields error about reading uninitialized value, MSVC does yeild similar error straight error about reading the error.

I think GCC should reject this.
Comment 1 Drea Pinski 2025-02-17 19:03:43 UTC
Dop you have a full example because the code snipit here definitely is rejected.
Comment 2 Hana Dusíková 2025-02-17 19:06:06 UTC
https://compiler-explorer.com/z/58reoonEW

(trunk GCC 2ef2b206c4617abae60002280455f7175aaa6064)

```c++
#include <coroutine>

struct awaitable {
    constexpr bool await_ready() {
        return true;
    }
    void await_suspend(std::coroutine_handle<void>) {

    }
    constexpr int await_resume() {
        return 42;
    }
};

struct super_simple_coroutine {
    struct promise_type {
        constexpr auto initial_suspend() {
            return std::suspend_never();
        }
        constexpr auto final_suspend() const noexcept {
            return std::suspend_never();
        }
        constexpr void unhandled_exception() {
            // do nothing
        }
        constexpr auto get_return_object() {
            return super_simple_coroutine{};
        }
        constexpr void return_void() {

        }
    };
};

auto fib() -> super_simple_coroutine {
    // if `co_await` is part of BodyStatement of a function
    // it makes it coroutine
    constexpr auto x = co_await awaitable{};
}
```
Comment 3 Drea Pinski 2025-02-17 19:11:37 UTC
Confirmed. Yes looks like GCC ignores constexpr with co_await.
Comment 4 Iain Sandoe 2025-05-30 19:11:07 UTC
testing a patch
Comment 5 Iain Sandoe 2025-05-30 19:53:05 UTC
posted

actually co_await was being recognised as not suitable for constexpr; but I had omitted the diagnostic.
Comment 6 GCC Commits 2025-06-02 18:30:29 UTC
The master branch has been updated by Iain D Sandoe <iains@gcc.gnu.org>:

https://gcc.gnu.org/g:0ae77a05c416c9f750cb87f1bef0800651168b7e

commit r16-1059-g0ae77a05c416c9f750cb87f1bef0800651168b7e
Author: Iain Sandoe <iain@sandoe.co.uk>
Date:   Fri May 30 20:09:40 2025 +0100

    c++: Emit an error for attempted constexpr co_await [PR118903].
    
    We checked that the coroutine expressions were not suitable for
    constexpr, but did not emit and error when needed.
    
            PR c++/118903
    
    gcc/cp/ChangeLog:
    
            * constexpr.cc (potential_constant_expression_1): Emit
            an error when co_await et. al. are used in constexpr
            contexts.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/coroutines/pr118903.C: New test.
    
    Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>
Comment 7 GCC Commits 2025-07-29 15:05:43 UTC
The releases/gcc-15 branch has been updated by Iain D Sandoe <iains@gcc.gnu.org>:

https://gcc.gnu.org/g:b9b3471a9eb3f490b74b57236b4c122045dbcf56

commit r15-10092-gb9b3471a9eb3f490b74b57236b4c122045dbcf56
Author: Iain Sandoe <iain@sandoe.co.uk>
Date:   Fri May 30 20:09:40 2025 +0100

    c++: Emit an error for attempted constexpr co_await [PR118903].
    
    We checked that the coroutine expressions were not suitable for
    constexpr, but did not emit and error when needed.
    
            PR c++/118903
    
    gcc/cp/ChangeLog:
    
            * constexpr.cc (potential_constant_expression_1): Emit
            an error when co_await et. al. are used in constexpr
            contexts.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/coroutines/pr118903.C: New test.
    
    Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>
    (cherry picked from commit 0ae77a05c416c9f750cb87f1bef0800651168b7e)
Comment 8 Iain Sandoe 2025-07-29 15:21:48 UTC
fixed for 15.2