Came up here: https://github.com/dlang/dmd/pull/13993#issuecomment-1100944395
A pure function can allocate something with an impure destructor to violate the assumptions a pure function gives, for example that the result of a pure factory function is unique:
```
import core.memory;
char[] globalVar;
struct S
{
char[] chars;
~this()
{
globalVar = chars;
}
}
// pure factory function
char[] returnUnique() pure
{
auto s = new S(new char[4]);
s.chars[] = 'a';
return s.chars;
}
void main()
{
string s = returnUnique(); // safe cast to immutable
GC.collect(); // impure destructor is run
assert(s == "aaaa"); // string is immutable
globalVar[] = 'b'; // alias escaped through impure destructor
assert(s == "aaaa"); // we mutated immutable memory
}
```
Comment #1 by robert.schadek — 2024-12-13T19:22:20Z