msys2/MINGW-packages

g++ Fails to Compile a Member Coroutine When Promise has Access to the Instance

Closed this issue · 2 comments

Description / Steps to reproduce the issue

The following C++20 code fails to compile. A clean installation of the latest Debian did fine with it. The problem is with the lines marked Note 1 and Note 2. If the code is commented out at either of the two location it will compile and the program will run. I think it used to work before one of the latest msys2 updates that updated gcc.

#include <coroutine>
#include <exception>
#include <iostream>
#include <string>


//
struct [[nodiscard]] C;


//
struct A {
	//
	std::string m_a { };
	//
	C setA(std::string a);
};


//
struct [[nodiscard]] C {
	//
	struct P {
		//
		P() { }
		//
		template <typename... TArgs>			// Note 1
		P(A& instance, TArgs&&... args) { }		// Note 1
		//
		auto get_return_object() noexcept {
			return std::coroutine_handle<P>::from_promise(*this);
		}
		//
		std::suspend_always initial_suspend() const noexcept {
			return { };
		}
		//
		void unhandled_exception() noexcept {
			std::terminate();
		}
		//
		std::suspend_always final_suspend() const noexcept {
			return { };
		}
		//
		void return_void() const noexcept { }
	};
	//
	using promise_type = P;
	//
	C(std::coroutine_handle<P> handle) : m_handle { handle } { }
	//
	~C() {
		if (m_handle) {
			m_handle.destroy();
			m_handle = nullptr;
		}
	}
	//
	std::coroutine_handle<P> m_handle;
};


//
inline C A::setA(std::string a) {
	m_a = a;		// Note 2
	co_return;
}


//
int main(int argc, char* argv[]) {
	A a { };
	auto cr = a.setA("foo");

	std::cout << cr.m_handle.done() << std::endl;

	cr.m_handle.resume();

	std::cout << cr.m_handle.done() << std::endl;

	std::cout << "done" << std::endl;

	return 0;
}

Expected behavior

Code should compile and the program should produce the following:

$ g++ -std=c++20 main.cpp
$ ./a.exe
0
1
done

Actual behavior

$ g++ -std=c++20 main.cpp
during RTL pass: expand
main.cpp: In member function 'C A::setA(std::string)':
main.cpp:68:1: internal compiler error: in expand_expr_real_1, at expr.cc:11376
   68 | }
      | ^
Please submit a full bug report, with preprocessed source (by using -freport-bug).
See <https://github.com/msys2/MINGW-packages/issues> for instructions.

As a sidenote, when I compile it with -freport-bug keyword I also get the following at the end:

g++.exe: fatal error: cannot execute '<abbreviated>/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/cc1plus.exe'
: open temporary output file: No such file or directory
compilation terminated.

Verification

Windows Version

MINGW64_NT-10.0-19045

MINGW environments affected

  • MINGW64
  • MINGW32
  • UCRT64
  • CLANG64
  • CLANG32
  • CLANGARM64

Are you willing to submit a PR?

No response

See here on godbolt, the same internal error also happens with Linux gcc-14.2.

I'll try to file it with GCC then. Thanks for the quick response.