What does this compile-time error mean?

Jonathan M Davis jmdavisProg at gmx.com
Sun Feb 24 00:59:55 PST 2013


On Sunday, February 24, 2013 15:42:33 Alexandr Druzhinin wrote:
> This code:
> 
> import std.stdio;
> import std.container;
> import std.range;
> 
> auto main_cont = redBlackTree(iota(0, 100, 10).array);
> 
> void main() {
> 
>     writeln(main_cont.array);
> }
> 
> at compilation with dmd 2.062 generates:
> 
> D:\applications\D\dmd2\windows\bin\..\..\src\druntime\import\core\memory.d(3
> 16): Error: gc_malloc cannot be interpreted at compile time, because it has
> no available source code
> D:\applications\D\dmd2\windows\bin\..\..\src\phobos\std\array.d(321):
>       called from here: malloc(_param_0 * 4u, 2u)
> D:\applications\D\dmd2\windows\bin\..\..\src\phobos\std\array.d(272):
>       called from here: arrayAllocImpl(_param_0)
> D:\applications\D\dmd2\windows\bin\..\..\src\phobos\std\array.d(45):
>      called from here: uninitializedArray(r.length())
> src\main.d(5):        called from here: array(iota(0, 100, 10))
> src\main.d(5):        called from here: redBlackTree(array(iota(0, 100,
> 10)))
> 
> Is it my error?

Because main_cont is module-level variable, it must be initialized with a 
value at compile time. Classes can be used at compile time (at least some of 
the time), but they cannot stick around between compile time and runtime, 
meaning that you could potentially use them in a CTFE function, but you can't 
initialize a module-level or static variable (or enum) with them, and you're 
attempting to initialize maint_cont with a RedBlackTree, which is a class. It 
won't work.

Now, on top of that, given those particular errors, it looks like there may be 
other issues beyond that which prevent RedBlackTree and/or std.array.array 
from being used at compile time at all, but even if it were perfectly useable 
at compile time, it still couldn't persist after the code is compiled and 
therefore could not be assigned to maint_cont.

If you want to have main_cont be a RedBlackTree, you need to iniatialize it at 
runtime. To do that, you could do that something like

RedBlackTree!int main_cont;

static this()
{
    main_cont = redBlackTree(iota(0, 100, 10).array());
}

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list