microsoft / cppwinrt

C++/WinRT
MIT License
1.61k stars 232 forks source link

Bug: Upgrading from 2.0.221104.6 to a later version no longer builds co_await on a DispatcherQueue #1391

Closed jaimecbernardo closed 5 months ago

jaimecbernardo commented 5 months ago

Version

2.0.240111.5

Summary

After updating CppWinRT from 2.0.221104.6, which build this code, to 2.0.221117.1 or later, this code no longer builds.

I've uploaded a repository to make this easier to try to build and fail. https://github.com/jaimecbernardo/Sample-CppWinRT-DispatcherQueue-build-regression

The code is taken from https://www.nuget.org/packages/robmikh.common/0.0.22-beta package, from this file: https://github.com/robmikh/robmikh.common/blob/aaa31a1bcd4fac19ce4c544d2cfd415612add7f0/robmikh.common/include/robmikh.common/dispatcherqueue.desktop.interop.h

CppWinRT 2.0.221104.6 builds this code. This repo can be built at this commit: https://github.com/jaimecbernardo/Sample-CppWinRT-DispatcherQueue-build-regression/commit/c1a7221f9eeb6dab8a16fa6b812d97eb5132902c

The next version available (2.0.221117.1) https://github.com/jaimecbernardo/Sample-CppWinRT-DispatcherQueue-build-regression/commit/7742b755e2c4c6dd9293c85bdcc2cf8dfd543c8b , and the lastest (2.0.221104.6) https://github.com/jaimecbernardo/Sample-CppWinRT-DispatcherQueue-build-regression/commit/c1a7221f9eeb6dab8a16fa6b812d97eb5132902c , no longer allow build this repository.

Reproducible example

#pragma once
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.System.h>
#include <dispatcherqueue.h>

// This code on a pre-compiled header file will no longer build.
inline winrt::fire_and_forget ShutdownAndThenPostQuitMessage(winrt::Windows::System::DispatcherQueueController const& controller, int exitCode)
{
    auto queue = controller.DispatcherQueue();
    co_await controller.ShutdownQueueAsync();
    co_await queue;
    PostQuitMessage(exitCode);
    co_return;
}

Expected behavior

The code should be able to build successfully.

Actual behavior

1>------ Build started: Project: TestBuildCppWinRT, Configuration: Debug x64 ------
1>pch.cpp
1>C:\prog\janeasystems\TestBuildCppWinRT\x64\Debug\Generated Files\winrt\impl\Windows.Graphics.Capture.0.h(27,12): error C3312: no callable 'await_resume' function found for type 'winrt::Windows::System::DispatcherQueue'
1>(compiling source file 'pch.cpp')
1>Done building project "TestBuildCppWinRT.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Additional comments

No response

kennykerr commented 5 months ago

This may be a change in C++/ADL rules/compliance. If you add using namespace winrt; it works.

If that's not suitable, you can use co_await winrt::resume_foreground(queue); explicitly.

kennykerr commented 5 months ago

cc @robmikh

robmikh commented 5 months ago

Ah, thanks! I used the wil version in the past as a substitute, good to know it's still there in C++/WinRT. At some point I need to update those headers, but I've been spending most of my free time using Rust.

sylveon commented 5 months ago

Worth pointing out that the wil version has some extra fixes that the cppwinrt one doesn't, as well as being the only one that works on WinUI 3.

kennykerr commented 5 months ago

but I've been spending most of my free time using Rust

Don't look back. 😏

jaimecbernardo commented 5 months ago

Thank you. 😄 Ended up going with the same workaround as @robmikh : https://github.com/robmikh/Win32CaptureSample/commit/fc758e343ca886795b05af5003d9a3bb85ff4da2