Issue 23637 - [betterC] DMD issues GC usage error during CTFE
Summary: [betterC] DMD issues GC usage error during CTFE
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: betterC
Depends on:
Blocks:
 
Reported: 2023-01-16 21:04 UTC by Jack Stouffer
Modified: 2023-01-18 17:29 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jack Stouffer 2023-01-16 21:04:09 UTC
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 Jack Stouffer 2023-01-16 21:18:27 UTC
I should note that this is a bug in master
Comment 2 RazvanN 2023-01-18 14:03:07 UTC
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 Jack Stouffer 2023-01-18 17:29:49 UTC
(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.