https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124734
Bug ID: 124734
Summary: False -Wodr on coroutine frame types in anonymous
namespaces across TUs
Product: gcc
Version: 14.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: matt at godbolt dot org
Target Milestone: ---
(also reproduces on gcc 15)
When two different coroutine function templates with the same local name exist
in anonymous namespaces in different translation units, GCC's LTO -Wodr checker
reports a false ODR violation on the compiler-generated `.Frame` types.
The coroutine functions have different bodies (and therefore legitimately
different frame layouts), but because `_GLOBAL__N_1` is the same synthetic name
in both TUs, the `.Frame` types get identical mangled names and the ODR checker
treats them as the same type.
I don't think this is a duplicate of #101118 (which fixed unstable DECL_UID
field naming for the *same* coroutine across TUs). Here the frames are for
*different* coroutines that happen to share a name.
I also don't think this is covered by #70413 (which fixed COMDAT marking for
user template instantiations with anonymous-namespace arguments). The `.Frame`
type is compiler-generated during `morph_fn_to_coro()` and does not go through
the visibility-constraining code path in `decl2.cc`.
Clang (thin LTO) does not report this issue.
## Minimal reproducer
### a.cc
```cpp
#include <coroutine>
struct Task {
struct promise_type {
Task get_return_object() { return {}; }
std::suspend_never initial_suspend() { return {}; }
std::suspend_never final_suspend() noexcept { return {}; }
void return_void() {}
void unhandled_exception() {}
};
};
namespace {
template <typename T>
Task testCase(T& poller) {
int x = 42;
co_return;
}
} // namespace
void call_a(int& p) { testCase(p); }
```
### b.cc
```cpp
#include <coroutine>
struct Task {
struct promise_type {
Task get_return_object() { return {}; }
std::suspend_never initial_suspend() { return {}; }
std::suspend_never final_suspend() noexcept { return {}; }
void return_void() {}
void unhandled_exception() {}
};
};
namespace {
template <typename T>
Task testCase(T& poller) {
double y = 3.14;
co_return;
}
} // namespace
void call_b(int& p) { testCase(p); }
```
### main.cc
```cpp
void call_a(int& p);
void call_b(int& p);
int main() {
int p = 0;
call_a(p);
call_b(p);
}
```
### Build command
```
g++ -std=c++20 -flto -Werror=odr -c a.cc -o a.o
g++ -std=c++20 -flto -Werror=odr -c b.cc -o b.o
g++ -std=c++20 -flto -Werror=odr -c main.cc -o main.o
g++ -flto -Werror=odr a.o b.o main.o -o test
```
### Error output (GCC 14.2)
```
a.cc:19:1: error: type 'struct _ZN12_GLOBAL__N_18testCaseIiEE4TaskRT_.Frame'
violates the C++ One Definition Rule [-Werror=odr]
19 | }
| ^
b.cc:19:1: note: a different type is defined in another translation unit
19 | }
| ^
a.cc:15:6: note: the first difference of corresponding definitions is field
'x_1_2'
15 | Task testCase(T& poller) {
| ^
b.cc:15:6: note: a field with different name is defined in another translation
unit
15 | Task testCase(T& poller) {
| ^
```
The `.Frame` type for `testCase<int>` in a.cc and b.cc gets the same mangled
name `_ZN12_GLOBAL__N_18testCaseIiEE4TaskRT_.Frame` because `_GLOBAL__N_1` is
the same synthetic namespace name in all TUs. These are distinct types
(different coroutine bodies, different promoted locals) that should never be
cross-TU compared.
As Jan Hubička noted in #101118 comment 8: "If it is never used to pass data
between translation units, the correct solution would be to put it to the
anonymous namespace." The frame type is type-erased for external callers via
`coroutine_handle<void>`, so it is inherently TU-local.
I'm definitely out of my scope here, but after playing around a bit and asking
questions of an LLM (so take with a bag of salt), fixes could include one of:
1. Place the `.Frame` type in the anonymous namespace when the coroutine
function is in one (propagate `DECL_NAMESPACE_SCOPE_P` / anonymous namespace
flag)
2. Mark the `.Frame` type so that `type_in_anonymous_namespace_p` returns true
when the parent coroutine function is in an anonymous namespace
3. Skip ODR checking for compiler-generated `.Frame` types whose parent
function has internal linkage
Notes:
- GCC 14.2.0 and GCC 15
- Linux x86_64
- `-flto` (full LTO, not thin)
- Clang 21 with `-flto=thin` does NOT reproduce