为什么 GCC-11 在包含这组特定的标准库时会出现段错误?

1lu*_*min 6 c++ gcc c++20 c++-modules

双重编辑:仅在使用 libstdc++ 时,同一代码上的可能错误和 Clang 段错误导致 GCC 段错误。起初我不知道这是因为编译器段错误并不是特别有用,但是使用标志 -stdlib=libc++,您可以使用 clang 来很好地编译此代码。使用 libstdc++ 时的 Clang 段错误是一个单独的问题,这个问题特别关注 GCC。

可以在相关的错误报告 #101184 中找到进一步简化的 GCC 示例。如果您对解决方案感兴趣,我建议您关注错误报告。


在下面的代码墙中,有几个小的 c++20 模块在使用包含的命令构建时会导致GCC-11编译器段错误。

我尝试在下面的文本中解释这个问题,但这个问题有点挑剔,我发现理解它的最好方法是稍微玩一下。

其中两个模块“filler1”和“filler2”不导出任何内容,仅在导出模块指令之前包含几个标准库。因为实际上没有从这两者中导出任何内容,所以我预计它不会对程序行为产生任何影响,但出于某种原因,如果两者都包含在内,则会出现 GCC 段错误。如果以任何顺序删除任何一个,程序都可以正常编译。此外,这些填充模块中包含的标准库是特殊的,如果删除“功能”或“内存”,即使它们没有在整个程序中的任何地方使用,它也可以很好地编译。

还有 testmod1 模块,它只包含“向量”。但与填充模块不同,要发生段错误,必须在导出模块指令之后定义任意类型的向量,可以导出也可以不导出。

最后一个可能也是最奇怪的部分是 testmodp 模块。除了所有先前存在的微粒之外,模板结构:template<typename...Ts>与成员向量:std::vectorstd::variant<Ts...>必须定义,然后由 test_impl 模块实例化. 如果我从结构中删除模板并将成员更改为:“ std::vector<std::variant<int, float>> ”它也可以正常工作,即使它与模板类型实例化的类型相同反而。谁能帮我理解这里发生了什么?

注意:因为这个错误非常特别,一个 5 文件示例是我可以在不改变行为的情况下将它提取到的最小文件,我也不允许包含链接,所以如果你想测试它,复制所有带有相应名称的内容。如果这里有什么不对的地方,请随时在评论中联系(:

谢谢阅读!

文件“filler1.cc”

module;
#include <memory>

export module filler1;
Run Code Online (Sandbox Code Playgroud)

文件“filler2.cc”

module;
#include <memory>
#include <functional>

export module filler2;
Run Code Online (Sandbox Code Playgroud)

文件“testmodp.cc”

module;
#include <functional>
#include <vector>
#include <variant>

export module testmodp;

export template<typename... Ts> // must be a template with args used in meta_variant for error to occur
struct testmodp{
    typedef std::variant<Ts...> meta_variant;
    std::vector<meta_variant> meta_gs;
};
Run Code Online (Sandbox Code Playgroud)

文件“testmod1.cc”

module;
#include <vector>

export module testmod1;

export struct testasdf{
    std::vector<int> testvec;
};
Run Code Online (Sandbox Code Playgroud)

文件“test_impl.cc”

module;
#include <memory>

import filler1;
import filler2;

import testmodp;
import testmod1;

export module test_impl;

export namespace test_impl {
    void test_init(){
        new testmodp<int,float>();
    }
};
Run Code Online (Sandbox Code Playgroud)

要运行的GCC命令

module;
#include <memory>

export module filler1;
Run Code Online (Sandbox Code Playgroud)

GCC内部编译器错误:

module_test/test3/test_impl.cc:11:8: internal compiler error: in write_location, at cp/module.cc:15605
   11 | export module test_impl;
      |        ^~~~~~
0x1797368 internal_error(char const*, ...)
        ???:0
0x67f8f9 fancy_abort(char const*, int, char const*)
        ???:0
0x7652d9 trees_out::core_vals(tree_node*)
        ???:0
0x768e78 trees_out::tree_value(tree_node*)
        ???:0
0x761e9c trees_out::tree_node(tree_node*)
        ???:0
0x763cd6 trees_out::write_var_def(tree_node*)
        ???:0
0x764fd5 module_state::write_cluster(elf_out*, depset**, unsigned int, depset::hash&, unsigned int*, unsigned int*)
        ???:0
0x767ec9 module_state::write(elf_out*, cpp_reader*)
        ???:0
0x768d86 finish_module_processing(cpp_reader*)
        ???:0
0x713d1b c_parse_final_cleanups()
        ???:0
Please submit a full bug report,
Please include the complete backtrace with any bug report.
Run Code Online (Sandbox Code Playgroud)