https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110871
Bug ID: 110871 Summary: coroutine precondition should be evaluated before the initial suspend Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: akrzemi1 at gmail dot com Target Milestone: --- Created attachment 55672 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55672&action=edit Sample program illustrating the bug When I declare a precondition on a coroutine, it is not evaluated when the corresponding promise type performs the initial suspend. While "C++20 contracts" or the "Contracts MVP" (https://isocpp.org/files/papers/P2388R4.html) never mention how this should work, the intuition dictates that the preconditions protect against the usage of the incorrect combination of function parameters. Function parameters of a coroutine may be used before the initial suspend to initialize the promise object (when it provides a variadic constructor). The coroutine precondition should be evaluated before this initialization, therefore before the initial suspend. Also, this follows from the rule of thumb that implementations should have freedom to check the precondition either in the caller or in the callee and either choice should not affect the program semantics. The following Compiler Explorer example illustrtes the issue. "Disaster!" should never be printed: https://godbolt.org/z/fxMjT4vfx