You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GCC 13.2编译C++20模板协程触发内部错误,求相关参考

GCC 13.2 C++20协程内部编译错误(instantiate_type)问题分析

问题场景

使用x86-64 GCC 13.2编译器,通过-std=c++20 -O2参数编译C++20协程代码时,触发内部编译错误(ICE),错误提示为internal compiler error: in instantiate_type, at cp/class.cc:8792。相同代码在MSVC和Clang编译器下可正常编译。

复现代码

#include <coroutine>

struct state_seq {
    struct promise_type
    {        
        struct _p_awaiter
        {
            bool await_ready() const { return false; }
            void await_suspend(std::coroutine_handle<> handle) {}
            int await_resume() const { return 42; }
        };

        [[noreturn]]
        static void unhandled_exception() { throw; };

        std::suspend_never initial_suspend() noexcept { return {}; }
        std::suspend_always final_suspend() noexcept { return {};}

        void return_value(int) { }
        _p_awaiter yield_value(int) { return {}; }

        state_seq get_return_object() {
            return state_seq {
                ._handle = std::coroutine_handle<promise_type>::from_promise(*this)
            };
        }
    };
    
    std::coroutine_handle<promise_type> _handle;
};

template<int TUnused = 0>  // 移除此行可正常编译
state_seq parse() 
{
    auto i = co_yield 5;
    // const auto& r_i = co_yield 45; // 使用const引用赋值可正常编译
    i = co_yield 45; // 触发ICE的代码行
    co_return i;
}

void  test() { auto seq = parse(); }

编译错误日志

<source>: In instantiation of 'state_seq parse() [with int TUnused = 0]':
<source>:41:32:   required from here
<source>:37:7: internal compiler error: in instantiate_type, at cp/class.cc:8792
   37 |     i = co_yield 45; // ERROR: internal compiler error: in instantiate_type, at cp/class.cc:8792
      |     ~~^~~~~~~~~~
0x1ce7bde internal_error(char const*, ...)
    ???:0
0x7290fc fancy_abort(char const*, int, char const*)
    ???:0
0x92701d cp_build_modify_expr(unsigned int, tree_node*, tree_code, tree_node*, int)
    ???:0
0x927ae2 build_x_modify_expr(unsigned int, tree_node*, tree_code, tree_node*, tree_node*, int)
    ???:0
0x8beb17 instantiate_decl(tree_node*, bool, bool)
    ???:0
0x8d993b instantiate_pending_templates(int)

临时规避方案

  • 移除parse函数的模板参数声明,将其改为普通函数
  • 将co_yield的结果赋值给const auto&类型变量,而非值类型变量

相关Bug信息

该问题是GCC的已知Bug,对应Bugzilla条目为Bug 109687,描述为"ICE in instantiate_type when assigning co_yield result in templated coroutine"。此Bug已在GCC 14.x版本中得到修复,升级编译器版本可彻底解决问题。

内容的提问来源于stack exchange,提问作者Алексей Фет

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 23:05:07