https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124247
Bug ID: 124247
Summary: [modules] std::format with cross-module std::formatter
specialization crashes at -O0 (works at -O2)
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: kachalenko.denis at gmail dot com
Target Milestone: ---
When a module exports a type with a std::formatter specialization, and another
translation unit imports that module and calls std::format with that type, the
program crashes at runtime with an access violation (0xC0000005) when compiled
at -O0, -O1, or -Og. It works correctly at -O2 and -O3.
std::format with builtin types (int, string_view, etc.) works fine at all
optimization levels, even with the same module imported. The crash only occurs
when using a std::formatter specialization defined in an imported module.
The binary compiles and links without errors at all optimization levels.
Minimal reproducer (two files):
--- reproducer.cppm ---
export module Reproducer;
import std;
export struct MyString
{
const char* Data;
std::size_t Length;
MyString(const char* S) : Data(S), Length(std::strlen(S)) {}
};
template<>
struct std::formatter<MyString> : std::formatter<std::string_view>
{
auto format(const MyString& Value, std::format_context& Context) const
{
return std::formatter<std::string_view>::format(
std::string_view(Value.Data, Value.Length), Context);
}
};
--- main.cpp ---
import std;
import Reproducer;
int main()
{
MyString Str("hello");
std::string Result = std::format("{}", Str);
std::cout << Result << std::endl;
return 0;
}
Tested behavior:
-O0 → compiles, links, CRASHES at runtime (access violation 0xC0000005)
-O1 → CRASHES
-Og → CRASHES
-O2 → runs correctly, prints "hello"
-O3 → runs correctly
-g has no effect — only optimization level matters.
std::format("hello {}", 42) works at -O0 (no cross-module specialization).
Environment:
GCC: 16.0.1 20260221 (experimental)
OS: Windows 11 Pro (10.0.26200)
Target: x86_64-w64-mingw32
Runtime: MSYS2 UCRT64
Build system: xmake 3.0.7 (manages module compilation)