www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9748] New: Wrong scope of templated nested functions in static foreach

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9748

           Summary: Wrong scope of templated nested functions in static
                    foreach
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: thecybershadow gmail.com



17:01:51 EET ---
template Tuple(T...) { alias T Tuple; }

void main()
{
    foreach (i; Tuple!(1, 2, 3))
    {
        uint j;
        void set()(int n) { j = n; }
        set(i);
        assert(j==i);
    }
}

This fails because the "set" function will always refer to the "j" variable
declared in the first foreach iteration. If you move the "j" declaration
outside the loop, the asserts pass.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 18 2013
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9748




More explicit test case:

template Tuple(T...) { alias T Tuple; }
extern(C) int printf(const char*, ...);
void main()
{
    //foreach (i; Tuple!(1, 2))
    {
        enum i = 1;
        uint j;
        void set()(int n) { j = n; printf("set1\n"); }
        set(i);
        printf("i = %d, j = %d\n", i, j);
        assert(j == i);
    }
    //foreach (i; Tuple!(1, 2))
    {
        enum i = 2;
        uint j;
        void set()(int n) { j = n; printf("set2\n"); }
        set(i);
        printf("i = %d, j = %d\n", i, j);
        assert(j == i);
    }
}

The second 'set' function template is not called.

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