Bug 10685 – Immutable interval foreach to propagate index value range

Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-07-20T13:56:00Z
Last change time
2013-11-16T21:43:57Z
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2013-07-20T13:56:55Z
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
Comment #1 by bearophile_hugs — 2013-07-20T14:22:09Z
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
Comment #2 by bearophile_hugs — 2013-07-25T16:21:34Z
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.
Comment #3 by yebblies — 2013-11-16T21:43:57Z
Same thing as 9570 / static array index *** This issue has been marked as a duplicate of issue 9570 ***