dirEntries within static foreach: memory.d(827): Error: fakePureErrno cannot be interpreted at compile time, because it has no available source code

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Feb 11 00:58:17 UTC 2019


On Sunday, February 10, 2019 5:19:02 PM MST kdevel via Digitalmars-d-learn 
wrote:
> I am trying to get this code compiled:
>
> ```TemplateStore.d
> module TemplateStore;
> import std.path;
> import std.conv;
> import std.file;
>
> immutable string[string] template_map;
>
> static this ()
> {
>     static foreach (f; dirEntries (``, `*.html`,
> SpanMode.shallow)) {
>        pragma (msg, `reading template <` ~ f ~ ">");
>        template_map[f] = import (f);
>     }
> }
> ```
>
> dmd v2.082.0 says
>
> ...linux/bin64/../../src/druntime/import/core/memory.d(827):
> Error: fakePureErrno cannot be interpreted at compile time,
> because it has no available source code
> Error: invalid foreach aggregate <error>
> Error: invalid foreach aggregate <error>
>
> If I use an immutable array of file names instead of dirEntries
> the code compiles, but I don't want to update that array each
> time a new template file is added.
>
> As a workaround I wrote a helper which writes the quoted
> filenames comma-separated into a file named `LS` using this
> fragment:
>
>     foreach (f; dirEntries (``, `*.html`, SpanMode.shallow))
>        writefln ("`%s`,", f);
>
> Then I try to convince the dmd compiler to read LS and use its
> content as initializer for the immutable array template_files:
>
>     immutable LS = import (`LS`);
>     pragma (msg, `LS = <` ~ LS ~ `>`);
>     immutable template_files = [
>        mixin(LS)
>     ];
>
> dmd now says (1) if the list in LS has a trailing comma:
>
>     TemplateStore.d-mixin-13(26): Error: expression expected, not
> End of File
>
> or (2) if the list has no trailing comma:
>
>     TemplateStore.d-mixin-13(13): Error: Using the result of a
> comma expression is not allowed
>     TemplateStore.d-mixin-13(14): Error: Using the result of a
> comma expression is not allowed
>     TemplateStore.d-mixin-13(15): Error: Using the result of a
> comma expression is not allowed
>
>
> What can I do about it?

Nothing. You can't call C functions during CTFE, because the compiler
doesn't have their source code, and all functions that interact with the
filesystem have to ultimately call C functions. So, you can't use something
like dirEntries with CTFE.

The only way that you can do anything along the lines of reading files
during CTFE is to use string imports to insert the text of a file directly
into the module.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list