CTFE in imported static initializers
    Steven Schveighoffer 
    schveiguy at gmail.com
       
    Mon May 13 20:39:57 UTC 2019
    
    
  
I have just discovered an interesting behavior, and I'm wondering if 
this behavior is intentional or even necessary.
Let's say I have this module called mod2.d:
module mod2;
string buildModData()
{
     string result;
     foreach(i; 0 .. 10000)
         result = result ~ "lots and lots and lots of data";
     return result;
}
static moddata = buildModData();
OK, so building this with -c on my system takes about 1 second of time:
Stevens-MacBook-Pro:ctfetest steves$ time dmd -c mod2.d
real	0m0.994s
user	0m0.434s
sys	0m0.554s
Now, I make a new module mod1.d:
module mod1;
import mod2;
And...
Stevens-MacBook-Pro:ctfetest steves$ time dmd -c mod1.d
real	0m0.993s
user	0m0.435s
sys	0m0.553s
This is because the CTFE engine is re-evaluating the static initializer, 
and then basically, throwing it away.
Why? I can't even use it at compile time...
pragma(msg, moddata.length);
mod1.d(5): Error: static variable moddata cannot be read at compile time
mod1.d(5):        while evaluating pragma(msg, moddata.length)
Why would the compiler re-run CTFE stuff that has already been run, or 
that at least it has no intention of using at compile time?
I found this because I'm trying to fix the import std.path issue (with 
-unittest on). I found this kind of stuff happening in phobos.
Does anyone have a good answer for why this should happen, or should I 
file a bug?
-Steve
    
    
More information about the Digitalmars-d-learn
mailing list