Bug 7034 – Infinite foreach on array

Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2011-11-29T19:11:00Z
Last change time
2013-01-16T18:54:13Z
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2011-11-29T19:11:45Z
Maybe this issues is already present in Bugzilla. I'd like this code to loop on all array 256 items once, initialize the array with all the bytes starting from the larger one, and then stop, but it goes into infinite loop: void main() { ubyte[256] table; foreach (ubyte i, ref x; table) x = 255 - i; } But Jonathan M Davis suggests that 'i' should always be size_t if it's an index: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=30863 This means Jonathan M Davis suggests to turn that code in a compilation error, so the right code become something like (the & is needed because currently D2 can't infer that 255-i fits in one ubyte): void main() { ubyte[256] table; foreach (i, ref x; table) x = (255 - i) & ubyte.max; } In any case, regardless of the solution that will be chosen (make it do the 'right' thing, or statically forbid that code requiring the index to be a size_t, or yet another solution), I suggest to not leave such trap in D2.
Comment #1 by bearophile_hugs — 2011-11-30T17:00:40Z
Comment #2 by yebblies — 2013-01-16T18:54:13Z
*** This issue has been marked as a duplicate of issue 5725 ***