Thread overview
[Issue 3182] New: compile time access of imported symbol
Jul 17, 2009
Ellery Newcomer
Aug 20, 2012
Ellery Newcomer
July 16, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3182

           Summary: compile time access of imported symbol
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: ellery-newcomer@utulsa.edu


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;

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 16, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3182


Jarrett Billingsley <jarrett.billingsley@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jarrett.billingsley@gmail.c
                   |                            |om




--- Comment #1 from Jarrett Billingsley <jarrett.billingsley@gmail.com>  2009-07-16 08:12:41 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 17, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3182





--- Comment #2 from Ellery Newcomer <ellery-newcomer@utulsa.edu>  2009-07-16 19:00:38 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 20, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=3182


Ellery Newcomer <ellery-newcomer@utulsa.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WORKSFORME


--- Comment #3 from Ellery Newcomer <ellery-newcomer@utulsa.edu> 2012-08-20 12:02:12 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------