Issue with struct invariants

Kirill Kryukov kkryukov at gmail.com
Wed Mar 5 16:58:18 UTC 2025


Hi all,

I was debugging an old project that started crashing after 
upgrading to a recent toolchain. I noticed that commenting out 
invariants helps. I reduced it to the following:

```
import std.conv: to;

struct A
{
     ulong n;
     string str()
     {
         if (n == 0) { return "0"; }
         return to!string(n);
     }
}

struct B
{
     ulong n;
     invariant() {}
     string str()
     {
         if (n == 0) { return "0"; }
         return to!string(n);
     }
}

void main()
{
     A a = { 1 };
     B b = { 2 };
     assert(a.str() == "1");  // OK
     assert(b.str() == "2");  // Fails
}
```

Is this a bug, or am I misunderstanding something?

Compiled with "gdc -Og -o repro repro.d", using gdc (GCC) 14.2.1 
20240912 (Red Hat 14.2.1-3), on Linux.

Thanks.


More information about the Digitalmars-d-learn mailing list