digitalmars.D.bugs - [Issue 23643] New: [betterC] Better Error Message For CTFE GC Usage
- d-bugmail puremagic.com (62/62) Jan 19 2023 https://issues.dlang.org/show_bug.cgi?id=23643
https://issues.dlang.org/show_bug.cgi?id=23643 Issue ID: 23643 Summary: [betterC] Better Error Message For CTFE GC Usage Product: D Version: D2 Hardware: All OS: All Status: NEW Severity: enhancement Priority: P1 Component: dmd Assignee: nobody puremagic.com Reporter: jack jackstouffer.com Consider the following real code example, char[] unsignedToTempString(uint radix = 10)(ulong value, return scope char[] buf) safe if (radix >= 2 && radix <= 16) { size_t i = buf.length; do { uint x = void; if (value < radix) { x = cast(uint)value; value = 0; } else { x = cast(uint)(value % radix); value /= radix; } buf[--i] = cast(char)((radix <= 10 || x < 10) ? x + '0' : x - 10 + 'a'); } while (value); return buf[i .. $]; } char[] myToString(ulong n) { char[20] buf; auto s = unsignedToTempString(n, buf); return s ~ (n > uint.max ? "UL" : "U"); } enum a = myToString(5); This gives the following error Error: array concatenation of expression `cast(const(char)[])s ~ (n > 4294967295LU ? "UL" : "U")` requires the GC which is not available with -betterC The problem here is that DMD is trying to include the function myToString into the object file even though it's only being used during CTFE. The fix for this code is to make myToString a template. But the chance that a user is going to know that is very low. The compiler knows that it encountered this function during the CTFE run. So it should give a helpful error message, something like. Error: array concatenation of expression `cast(const(char)[])s ~ (n > 4294967295LU ? "UL" : "U")` requires the GC which is not available with -betterC This GC allocation was encountered during CTFE. If this function is supposed to be a CTFE only function then this error can be fixed by making the function a template. Simple, easy to understand, and very helpful. --
Jan 19 2023