Bug 23637 – [betterC] DMD issues GC usage error during CTFE

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2023-01-16T21:04:09Z
Last change time
2023-01-18T17:29:49Z
Keywords
betterC
Assigned to
No Owner
Creator
Jack Stouffer

Comments

Comment #0 by jack — 2023-01-16T21:04:09Z
The following code does not compile 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); Fails with Error: array concatenation of expression `cast(const(char)[])s ~ (n > 4294967295LU ? "UL" : "U")` requires the GC which is not available with -betterC
Comment #1 by jack — 2023-01-16T21:18:27Z
I should note that this is a bug in master
Comment #2 by razvan.nitu1305 — 2023-01-18T14:03:07Z
This is not a bug. You are compiling with -betterC so code needs to be generated for `myToString`. That code ends up in the object file and may be linked at some point, therefore the compiler tells you that you are compiling with -betterC which means that you cannot use functions from druntime (the array concat hook). If you template the myToString method then the code compiles successfully because the template instances do not make it to the object file.
Comment #3 by jack — 2023-01-18T17:29:49Z
(In reply to RazvanN from comment #2) > If you template the myToString method then the code compiles successfully > because the template instances do not make it to the object file. I'm going to open an error message bug then. There's no way a normal user is going to know this.