google / googletest

GoogleTest - Google Testing and Mocking Framework
https://google.github.io/googletest/
BSD 3-Clause "New" or "Revised" License
34.79k stars 10.15k forks source link

Usage of `ASSERT_THROW` in a coroutine yields an error #1420

Closed Naios closed 5 years ago

Naios commented 6 years ago

3c5e064ca on clang-5.0 (libc++-5.0)

Given any awaitable object (in this example returned by supplier()), std::future<void> for example.

The concrete usage of ASSERT_THROW within a couroutine yields to an error when using clang:

../test/unit-test/test-continuable-await.cpp:117:3: error: return statement not allowed in coroutine; did you mean 'co_return'?
  ASSERT_THROW(co_await supplier().then([] { throw await_exception{}; }),
  ^
../dep/googletest/googletest/googletest/include/gtest/gtest.h:1858:52: note: expanded from macro 'ASSERT_THROW'
  GTEST_TEST_THROW_(statement, expected_exception, GTEST_FATAL_FAILURE_)
                                                   ^
../test/unit-test/test-continuable-await.cpp:107:3: note: function is a coroutine due to use of 'co_await' here
  co_await supplier();
gennadiycivil commented 5 years ago

I have been cleaning up older and inactive GitHub googletest issues. You may see an issue "closed" if it appears to be inactive/abandoned Thank you

Tuniwutzi commented 4 months ago

I'm facing this issue as well, using latest main (34ad51b3dc4f922d8ab622491dd44fc2c39afee9). It makes it impossible to use any GTest macro from within a coroutine. Example:

#include <gtest/gtest.h>

#include <coroutine>

struct ExampleCoroutine
{
    struct promise_type
    {
        std::suspend_never initial_suspend()
        {
            return {};
        }
        std::suspend_never final_suspend() noexcept
        {
            return {};
        }
        ExampleCoroutine get_return_object()
        {
            return {};
        }
        void return_void() {}
        void unhandled_exception() {}
    };
};

TEST(Foo, Bar)
{
    []() -> ExampleCoroutine
    {
        co_await std::suspend_never{};
        ASSERT_EQ(true, false); // Compiler error here, due to return statement in macro
    }();
}

My compiler is MSVC 19.33.31630.0, error is error C3773: Use of 'return' in this context is a non-conforming extension in C++23

Is it possible to reopen this issue? (@gennadiycivil are you the right person to ping?)