https://issues.dlang.org/show_bug.cgi?id=23643
RazvanN <[email protected]> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |[email protected] --- Comment #1 from RazvanN <[email protected]> --- (In reply to Jack Stouffer from comment #0) > 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. I'm sorry but I don't agree that this is a bug. The compiler cannot know if you want the function to be ctfe only or if it is going to be called from a different object file. Since the function is not a template, the compiler assumes that you want the object code for it and therefore nags you about the fact that you are doing illegal operations for betterC. If you want it to be ctfe only, just template it. What you are asking for is for the compiler to keep track of whether a function is called from ctfe or runtime contexts and if it is called only from ctfe to decide to either not output the object code or give some indication on how to fix this. But what if the person implementing myToString wants to call it from a different file and needs to have the object file working with betterC? Than the error is misleading. The error is correct: you are using druntime in a function that is betterC. --
