digitalmars.D.learn - Issue with struct invariants
- Kirill Kryukov (37/37) Mar 05 Hi all,
- Sergey (3/8) Mar 05 It seems working with dmd and ldc.
- Paul Backus (5/13) Mar 05 I tried compiling your example using both DMD and LDC, and was
- kdevel (4/7) Mar 05 Compiled your code with DMD v2.109.1, gdc (GCC) 11.3.0 and gdc
- Kirill Kryukov (3/3) Mar 06 Thanks for checking. Looks like it works with DMD, LDC, and older
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.
Mar 05
On Wednesday, 5 March 2025 at 16:58:18 UTC, Kirill Kryukov wrote:Hi all, 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.It seems working with dmd and ldc. Maybe GDC specific bug?
Mar 05
On Wednesday, 5 March 2025 at 16:58:18 UTC, Kirill Kryukov wrote: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:[...]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.I tried compiling your example using both DMD and LDC, and was unable to reproduce the crash. So this may be a GDC-specific problem.
Mar 05
On Wednesday, 5 March 2025 at 16:58:18 UTC, Kirill Kryukov wrote: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.Compiled your code with DMD v2.109.1, gdc (GCC) 11.3.0 and gdc (GCC) 12.1.0, using `gdc -Og -o repro repro.d` with the latter two: All executables ran without error.
Mar 05
Thanks for checking. Looks like it works with DMD, LDC, and older GDC, so this could be a GDC regression. I'll try to post it in GDC (GCC) Bugzilla if I can get an account there.
Mar 06