[Issue 23904] Spurious "struct [...] already exists"
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue May 9 11:02:10 UTC 2023
https://issues.dlang.org/show_bug.cgi?id=23904
RazvanN <razvan.nitu1305 at gmail.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |razvan.nitu1305 at gmail.com
--- Comment #2 from RazvanN <razvan.nitu1305 at gmail.com> ---
Further reduced:
======================
template f1(alias fun)
{
struct UnusedButConflicting
{
}
auto f1()
{
}
}
template f2(T)
{
alias fun = {};
auto f2(auto ref T)
{
f1!fun();
}
}
void main()
{
int a;
f2(5);
f2(a);
}
==========================
The problem here comes from the way dmd stores types. It essentially creates a
decoration by getting the mangled name of the type. In this case f2 creates
different template instances depending on whether f2 is called with an lvalue
or an rvalue (due to the presence of auto ref). Having 2 template instances,
this leads to having 2 f1 template instances due to `fun` being declared twice
in 2 different scopes, although the type is the same.
When semantic is performed on the unused struct the compiler tries to get a
unique identifier for the struct type by generating the mangled type. However,
the mangling is performing only by taking into account types. Since for both
template instances the type of the alias parameter is the same is the same
(although the value is different), the mangling ends up being the same,
however, the declarations are different, hence the error.
It seems that using the type for alias template parameters to generate the
mangling for such declarations is not a correct solution.
--
More information about the Digitalmars-d-bugs
mailing list