Comment #0 by ellery-newcomer — 2009-07-15T23:25:29Z
The following code does not print 'z=1!' when compiled, as if a premature attempted access to z causes the compiler to believe z doesn't exist even after the import statement. Take out the first static if, and issue goes away. Change the import to 'import test2: z;' and issue goes away.
Question: Would I be incorrect in assuming the expected output of this compile should be
no z!
z!
z=1!
test.d
-------
import tango.io.Stdout;
static if(is(typeof(z))){
pragma(msg,"z!");
}else{
pragma(msg,"no z!");
}
import test2;
static if(is(typeof(z))){
pragma(msg,"z!");
}else{
pragma(msg,"no z!");
}
static if(z == 1) pragma(msg,"z=1!");
void main(){
}
-------
test2.d
-------
module test2;
const int z = 1;
Comment #1 by jarrett.billingsley — 2009-07-16T08:12:41Z
I don't think your ideas on the ordering are right. You can place imports anywhere in a module and their symbols will be accessible before or after. For instance:
[mod.d]
module mod;
void foo() {}
[test.d]
void bar() { foo(); } // fine
import mod;
So, if anything, I'd expect the output to be
z!
z!
z=1!
But the compiler doesn't seem to be doing that. That the static if before the import causes later references to 'z' to be invalid is even stranger. Also, if you replace 'z' with something else (like "void z(){}"), you get other behavior, making me think that the semantic analysis is being done differently for different types.
Comment #2 by ellery-newcomer — 2009-07-16T19:00:38Z
Well, the distinction is whether the symbol is used in compile time expressions or run time expressions. Currently, DMD performs some or all compile time evaluation in the same pass as it builds the symbol table, which is why you get the behavior for the first static if.
Comment #3 by ellery-newcomer — 2012-08-20T12:02:12Z
It seems that now for dmd 2.060, the output is
z!
z!
z=1!
and since I'm to lazy to try to figure out what my complicated case was, I guess this issue is resolved.