[Issue 22334] TypeInfo is used in inexplicable places

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Sep 28 12:16:19 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=22334

--- Comment #2 from SHOO <zan77137 at nifty.com> ---
(In reply to Max Samukha from comment #1)
> Assigning to an array's length may reallocate, and allocation still needs
> TypeInfo. You could try to get away with a lambda (not tested):
> 
> alias _ctfeAppend = (store, dat)
> {
>     	alias T = typeof(dat);
> 
>         static if (is(T U: const(U)))
>         {
>             pragma(msg, U);
>                 U[] buf;
>                 buf.length = 1;
>                 buf[0] = dat;
>                 store = cast(T[])buf;
>         }
> };
> 
> However, the lambda hack is unusable for anything slightly less trivial (no
> overloading, if-constraints, variadic parameters, etc.)

Apparently, lambda does not solve the problem either...
And I doubt that reallocate is the cause, as the following code works:

-------------------------------------------
void _ctfeAppend(T)(ref T[] store, T dat)
{
        static if (is(T U: const(U)))
        {
                U[] buf;
                buf.length = 1;
                buf[0] = dat;
                store = cast(T[])buf;
        }
}
string[] getData()
{
        if (__ctfe)
        {
                string[] dat;
                dat._ctfeAppend("aaa");
                return dat;
        }
        return null;
}

extern(C) void main()
{
        static immutable dat = getData()[0];
        static assert(dat == "aaa");
}
-------------------------------------------

--


More information about the Digitalmars-d-bugs mailing list