```
import std.stdio : writeln;
void main() {
immutable Wrapper* a = getFieldName!S;
immutable Wrapper* b = getFieldName!(immutable S);
writeln(a, " ", b);
writeln(a.value.ptr, " ", b.value.ptr);
writeln(a.value.length, " ", b.value.length);
writeln(a.value, " ", b.value);
}
struct S {
int a;
}
template getFieldName(T) {
immutable Wrapper* getFieldName = getFieldNameInner!(T.tupleof);
}
template getFieldNameInner(fields...) {
immutable Wrapper w = Wrapper(fields[0].stringof);
immutable Wrapper* getFieldNameInner = &w;
}
struct Wrapper {
immutable string value;
}
```
This code should print two arbitrary pointer values, then 1, then "a".
If the variable `b` is removed, it does.
When `b` is present, `a.value.ptr` is different and identical to `b.value.ptr`, and it segfaults when trying to print `a.value`.
The issue doesn't occur if `b` uses just `getFieldName!S` instead of `getFieldName!(immutable S)` or it uses some other struct instead.
Somehow, passing both `S` and `immutable S` to the template causes `a` and `b` to share the same `Wrapper*`,
but the string it contains has an invalid pointer.
Comment #1 by robert.schadek — 2024-12-13T19:07:30Z