Jump to page: 1 2 3
Thread overview
Signed-unsigned comparisons in Phobos
Aug 12, 2011
bearophile
Aug 12, 2011
Don
Aug 12, 2011
kennytm
Aug 12, 2011
Don
Aug 12, 2011
Jonathan M Davis
Aug 12, 2011
Walter Bright
Aug 12, 2011
Jacob Carlborg
Aug 12, 2011
Timon Gehr
Aug 12, 2011
Walter Bright
Aug 13, 2011
Jacob Carlborg
Aug 12, 2011
Don
Aug 12, 2011
Marco Leise
Aug 12, 2011
bearophile
Aug 12, 2011
Timon Gehr
Aug 12, 2011
Marco Leise
Aug 12, 2011
Timon Gehr
Aug 12, 2011
Marco Leise
Aug 12, 2011
bearophile
Aug 12, 2011
Timon Gehr
Aug 12, 2011
Don
Aug 12, 2011
Ellery Newcomer
Aug 12, 2011
Timon Gehr
Aug 12, 2011
Timon Gehr
Aug 12, 2011
Jacob Carlborg
Aug 12, 2011
Marco Leise
Aug 12, 2011
Simen Kjaeraas
Aug 12, 2011
Marco Leise
Aug 12, 2011
Simen Kjaeraas
Aug 12, 2011
Jacob Carlborg
Aug 12, 2011
Simen Kjaeraas
August 12, 2011
I have added this bit of code to my dmd, it gives warnings in cases of integral signed-unsigned comparisons: https://github.com/D-Programming-Language/dmd/pull/119

Then I have compiled Phobos (with unittests) with the -wi switch, this has generated some of those signed-unsigned warning messages, maybe this list is of interest for someone:
http://codepad.org/Ku42j1SR

Note: that "ignored variadic arguments to the constructor" that lists no line number is generated at line 1766 of template ConstructorGeneratingPolicy() inside typecons.d Phobos module.

Bye,
bearophile
August 12, 2011
bearophile wrote:
> I have added this bit of code to my dmd, it gives warnings in cases of integral signed-unsigned comparisons:
> https://github.com/D-Programming-Language/dmd/pull/119
> 
> Then I have compiled Phobos (with unittests) with the -wi switch, this has generated some of those signed-unsigned warning messages, maybe this list is of interest for someone:
> http://codepad.org/Ku42j1SR
> 
> Note: that "ignored variadic arguments to the constructor" that lists no line number is generated at line 1766 of template ConstructorGeneratingPolicy() inside typecons.d Phobos module.
> 
> Bye,
> bearophile

I've had a look at a dozen or so of these, and they were all real. I didn't see any which require a cast to "make the compiler shut up".
That's pretty impressive. In C++ I find that such messages are nearly always false positives.

The one case where it's a bit annoying is this:

int [] x = new int[6]; // or x = some array literal.
for (int i = 0; i < x.length; ++i) {...}

Here is a suggestion for how we could eliminate such false positives.
http://d.puremagic.com/issues/show_bug.cgi?id=6478
August 12, 2011
Don <nospam@nospam.com> wrote:

> I've had a look at a dozen or so of these, and they were all real. I didn't see any which require a cast to "make the compiler shut up". That's pretty impressive. In C++ I find that such messages are nearly always false positives.
> 
> The one case where it's a bit annoying is this:
> 
> int [] x = new int[6]; // or x = some array literal.
> for (int i = 0; i < x.length; ++i) {...}
> 
> Here is a suggestion for how we could eliminate such false positives. http://d.puremagic.com/issues/show_bug.cgi?id=6478

Doesn't this require flow analysis? And the type of index 'i' should be 'size_t' anyway.
August 12, 2011
kennytm wrote:
> Don <nospam@nospam.com> wrote:
> 
>> I've had a look at a dozen or so of these, and they were all real. I
>> didn't see any which require a cast to "make the compiler shut up".
>> That's pretty impressive. In C++ I find that such messages are nearly
>> always false positives.
>>
>> The one case where it's a bit annoying is this:
>>
>> int [] x = new int[6]; // or x = some array literal.
>> for (int i = 0; i < x.length; ++i) {...}
>>
>> Here is a suggestion for how we could eliminate such false positives.
>> http://d.puremagic.com/issues/show_bug.cgi?id=6478
> 
> Doesn't this require flow analysis? 
Yes. See the bug report.

> And the type of index 'i' should be 'size_t' anyway.

Why? It will only ever be in the range 0..6.
August 12, 2011
Am 12.08.2011, 12:22 Uhr, schrieb kennytm <kennytm@gmail.com>:

> Don <nospam@nospam.com> wrote:
>
>> I've had a look at a dozen or so of these, and they were all real. I
>> didn't see any which require a cast to "make the compiler shut up".
>> That's pretty impressive. In C++ I find that such messages are nearly
>> always false positives.
>>
>> The one case where it's a bit annoying is this:
>>
>> int [] x = new int[6]; // or x = some array literal.
>> for (int i = 0; i < x.length; ++i) {...}
>>
>> Here is a suggestion for how we could eliminate such false positives.
>> http://d.puremagic.com/issues/show_bug.cgi?id=6478
>
> Doesn't this require flow analysis? And the type of index 'i' should be
> 'size_t' anyway.

I think I once shot myself in the foot with this when I used 'auto' for 'i' and the code wouldn't compile on x86_64, because I assigned the variable to an int or uint later on in the loop. You just have to be aware that this is an unsigned integer of machine word length. So I agree with kennytm on this.

Just remember that reverse loops are written like this:

for (size_t i = x.length; i-- > 0; ) {...}
August 12, 2011
Marco Leise:

> Just remember that reverse loops are written like this:
> 
> for (size_t i = x.length; i-- > 0; ) {...}

Thankfully in D there is foreach_reverse :-)

import std.stdio;

void main() {
    auto array = [10, 20, 30];

    for (size_t i = array.length; i-- > 0; )
        writeln(i, " ", array[i]);

    foreach_reverse (i, item; array)
        writeln(i, " ", item);
}

Bye,
bearophile
August 12, 2011
On 08/12/2011 03:08 PM, bearophile wrote:
> Marco Leise:
>
>> Just remember that reverse loops are written like this:
>>
>> for (size_t i = x.length; i-->  0; ) {...}
>
> Thankfully in D there is foreach_reverse :-)
>
> import std.stdio;
>
> void main() {
>      auto array = [10, 20, 30];
>
>      for (size_t i = array.length; i-->  0; )
>          writeln(i, " ", array[i]);
>
>      foreach_reverse (i, item; array)
>          writeln(i, " ", item);
> }
>
> Bye,
> bearophile

But some people seem to strongly dislike it though. It is a bit unfortunate that you don't currently get the same functionality by using retro.
August 12, 2011
On 2011-08-12 14:55, Marco Leise wrote:
> Am 12.08.2011, 12:22 Uhr, schrieb kennytm <kennytm@gmail.com>:
>
>> Don <nospam@nospam.com> wrote:
>>
>>> I've had a look at a dozen or so of these, and they were all real. I
>>> didn't see any which require a cast to "make the compiler shut up".
>>> That's pretty impressive. In C++ I find that such messages are nearly
>>> always false positives.
>>>
>>> The one case where it's a bit annoying is this:
>>>
>>> int [] x = new int[6]; // or x = some array literal.
>>> for (int i = 0; i < x.length; ++i) {...}
>>>
>>> Here is a suggestion for how we could eliminate such false positives.
>>> http://d.puremagic.com/issues/show_bug.cgi?id=6478
>>
>> Doesn't this require flow analysis? And the type of index 'i' should be
>> 'size_t' anyway.
>
> I think I once shot myself in the foot with this when I used 'auto' for
> 'i' and the code wouldn't compile on x86_64, because I assigned the
> variable to an int or uint later on in the loop. You just have to be
> aware that this is an unsigned integer of machine word length. So I
> agree with kennytm on this.
>
> Just remember that reverse loops are written like this:
>
> for (size_t i = x.length; i-- > 0; ) {...}

I've done something similar myself.

-- 
/Jacob Carlborg
August 12, 2011
Am 12.08.2011, 15:08 Uhr, schrieb bearophile <bearophileHUGS@lycos.com>:

> Marco Leise:
>
>> Just remember that reverse loops are written like this:
>>
>> for (size_t i = x.length; i-- > 0; ) {...}
>
> Thankfully in D there is foreach_reverse :-)
>
> import std.stdio;
>
> void main() {
>     auto array = [10, 20, 30];
>
>     for (size_t i = array.length; i-- > 0; )
>         writeln(i, " ", array[i]);
>
>     foreach_reverse (i, item; array)
>         writeln(i, " ", item);
> }
>
> Bye,
> bearophile

Sure, but we were talking about for, not foreach. Maybe these cases in Phobos cannot use foreach, because they modify 'i' or they don't require 'item'.
August 12, 2011
On 08/12/2011 03:33 PM, Marco Leise wrote:
> Am 12.08.2011, 15:08 Uhr, schrieb bearophile <bearophileHUGS@lycos.com>:
>
>> Marco Leise:
>>
>>> Just remember that reverse loops are written like this:
>>>
>>> for (size_t i = x.length; i-- > 0; ) {...}
>>
>> Thankfully in D there is foreach_reverse :-)
>>
>> import std.stdio;
>>
>> void main() {
>> auto array = [10, 20, 30];
>>
>> for (size_t i = array.length; i-- > 0; )
>> writeln(i, " ", array[i]);
>>
>> foreach_reverse (i, item; array)
>> writeln(i, " ", item);
>> }
>>
>> Bye,
>> bearophile
>
> Sure, but we were talking about for, not foreach. Maybe these cases in
> Phobos cannot use foreach, because they modify 'i' or they don't require
> 'item'.

foreach_reverse(i;0..array.length) writeln(array[i]),i--;




« First   ‹ Prev
1 2 3