Foreach loops on static arrays error message

Stefan Koch via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 6 01:49:33 PDT 2017


On Thursday, 6 July 2017 at 08:26:42 UTC, Guillaume Chatelet 
wrote:
> I stumbled upon https://issues.dlang.org/show_bug.cgi?id=12685
>
> In essence:
>
>> [...]
>
> `ubyte` can clearly hold a value from 0 to 255 so it should be 
> ok. No need for 256 ?!
>
> So I decided to fix it https://github.com/dlang/dmd/pull/6973
>
> Unfortunately when you think about how foreach is lowered it 
> makes sense - but the error message is misleading.
>
> The previous code is lowered to:
>
>> [...]
>
> `i < 256` is always true, this would loop forever.
>
> Questions:
> - What would be a better error message?
> - How about a different lowering in this case?
>
> From the programmer's point of view the original code makes 
> sense.
> A correct lowering would be:
>
>> [...]

I'd say this is not often encoutered.
One should avoid using a different type then size_t for the 
index, as it can have negative performance implications.


More information about the Digitalmars-d mailing list