D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5725 - ubyte/ushort infinite foreach loops
Summary: ubyte/ushort infinite foreach loops
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: bootcamp, wrong-code
: 7034 8964 (view as issue list)
Depends on:
Blocks:
 
Reported: 2011-03-10 05:19 UTC by bearophile_hugs
Modified: 2020-03-21 03:56 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2011-03-10 05:19:30 UTC
Problem found by Magnus Lie Hetland.
This program produces two infinite loops, DMD 2.052:


void main() {
    alias ubyte T1;
    T1[T1.max + 1] array1;
    foreach (T1 i, _; array1) {}
    alias ushort T2;
    T2[T2.max + 1] array2;
    foreach (T2 i, _; array2) {}
}
Comment 1 bearophile_hugs 2011-06-26 01:54:29 UTC
Related. This compiles with no errors, and maybe goes in infinite loop:

import std.stdio;
void main() {
    auto array = new int[270];
    foreach (ubyte i, ref x; array)
        x = i;
    writeln(array);
}


If the range of the fixed-sized array is larger than the max number that the index can represent, then I suggest to raise a compile-time error, just like this program does:

void main() {
   ubyte x = 270;
}

test.d(2): Error: cannot implicitly convert expression (270) of type int to ubyte
Comment 2 yebblies 2013-01-16 18:54:13 UTC
*** Issue 7034 has been marked as a duplicate of this issue. ***
Comment 3 yebblies 2013-01-16 19:02:25 UTC
*** Issue 8964 has been marked as a duplicate of this issue. ***
Comment 4 Tiberiu Lepadatu 2019-03-08 11:40:23 UTC
Why isn't this marked as solved?
Comment 5 basile-z 2019-03-17 17:01:37 UTC
fixed at some point:

/tmp/temp_7F8FB1CE2190.d(4,5): Error: index type `ubyte` cannot cover index range 0..256
/tmp/temp_7F8FB1CE2190.d(7,5): Error: index type `ushort` cannot cover index range 0..65536