www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10685] New: Immutable interval foreach to propagate index value range

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

           Summary: Immutable interval foreach to propagate index value
                    range
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2013-07-20 13:56:55 PDT ---
Foreach on a defined interval with an immutable index is a very common
operation:


void main() {
    foreach (immutable i; 0 .. 10)
        char c = 'x' + i;
}


DMD 2.064alpha gives:

test.d(3): Error: cannot implicitly convert expression (120 + i) of type int to
char


I suggest to give (propagate) the immutable variable 'i' the range [0, 10] so
the assignment to c requires no cast.

Se also Issue 10018 , Issue 10615 , Issue 10594

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 20 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10685



--- Comment #1 from bearophile_hugs eml.cc 2013-07-20 14:22:09 PDT ---
This range propagation is also useful for array bounds. See this code:


void main() {
    int[100] a;
    foreach (immutable i; 0 .. 10)
        a[i] = 1;
}


Compiled with DMD gives:

_D4test7__arrayZ:
L0:        enter    4,0
        push    EAX
        mov    ECX,offset FLAT:_D4test12__ModuleInfoZ
        push    ECX
        call    near ptr __d_array_bounds

__Dmain:
L0:     enter   0198h,0
        push    EBX
        push    ESI
        push    EDI
        mov ECX,064h
        xor EAX,EAX
        lea EDI,-0198h[EBP]
        rep
        stosd
        mov -8[EBP],EAX
L19:    cmp dword ptr -8[EBP],0Ah
        jge L4B
        mov ECX,-8[EBP]
        mov -4[EBP],ECX
        mov EDX,-4[EBP]
        cmp EDX,064h
        jb  L37
        mov EAX,4
        call    near ptr _D4test7__arrayZ
L37:    mov EBX,1
        mov ESI,-4[EBP]
        mov -0198h[ESI*4][EBP],EBX
        inc dword ptr -8[EBP]
        jmp short   L19
L4B:    xor EAX,EAX
        pop EDI
        pop ESI
        pop EBX
        leave
        ret


If the range of 'i' is propagated dmd could generate code like, thanks to Issue
9097 already implemented:


__Dmain:
        enter   0198h,0
        push    EBX
        push    EDI
        mov ECX,064h
        xor EAX,EAX
        lea EDI,-0198h[EBP]
        rep
        stosd
        mov -8[EBP],EAX
L18:    cmp dword ptr -8[EBP],0Ah
        jge L38
        mov ECX,-8[EBP]
        mov -4[EBP],ECX
        mov EDX,1
        mov EBX,-4[EBP]
        mov -0198h[EBX*4][EBP],EDX
        inc dword ptr -8[EBP]
        jmp short   L18
L38:    xor EAX,EAX
        pop EDI
        pop EBX
        leave
        ret

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 20 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10685



--- Comment #2 from bearophile_hugs eml.cc 2013-07-25 16:21:34 PDT ---
Another code example that becomes valid


void main() {
    char[26] arr;
    foreach (immutable i, ref c; arr)
        c = 'a' + i;
}


Currently gives:

test.d(4): Error: cannot implicitly convert expression (97u + i) of type uint
to char

But 'i' should have a static range of [0, 25] so 'a'+i should be in the
acceptable range for a char.

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