April 08, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=259



--- Comment #39 from Andrei Alexandrescu <andrei@erdani.com> 2013-04-08 07:21:23 PDT ---
Thanks Lionello for taking this over. I thought of one more case this morning so let me insert in the food chain:

(Recall a is signed and b is unsigned.)

==============

1. If a.sizeof > b.sizeof, then a < b is lowered into a < cast(typeof(a)) b. Then signed comparison proceeds normally. This is a classic value-based conversion dating from the C days, and we do it in D as well.

2. Otherwise, if a is determined through Value Range Propagation to be greater than or equal to zero, then a < b is lowered into cast(U) a < b, where U is the unsigned variant of typeof(a). Then unsigned comparison proceeds normally.

3. (NEW) Otherwise, if b is determined through Value Range Propagation to be
less than or equal to typeof(b).max / 2, then a < b is lowered into a < cast(S)
b, where S is the signed variant of typeof(b). Then signed comparison proceeds
normally.

4. Otherwise, the comparison is in error. The error message may recommend using the std.traits.unsigned function, which executes a size-informed cast.

==============

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 08, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=259



--- Comment #40 from Stewart Gordon <smjg@iname.com> 2013-04-08 15:57:45 PDT ---
Sounds good but ... given that people have been trying for 7 years without success to implement the basic rule from the spec, how many more years can we realistically expect it to be before this new scheme being suggested is in the compiler?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 08, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=259



--- Comment #41 from Andrei Alexandrescu <andrei@erdani.com> 2013-04-08 16:14:12 PDT ---
(In reply to comment #40)
> Sounds good but ... given that people have been trying for 7 years without success to implement the basic rule from the spec, how many more years can we realistically expect it to be before this new scheme being suggested is in the compiler?

Now that we have a preapproved solution, a solid VRP implementation, and a much expanded contribution base, I can only assume it'll take days. Lionello?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 09, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=259



--- Comment #42 from Lionello Lunesu <lio+bugzilla@lunesu.com> 2013-04-09 03:02:20 PDT ---
Yes, I'll have this done this week.

For point 1: how to cope with the fact that I can safely cast an uint to long, but can't cast an int to an ulong (without issues)?

Surely cast(int)-1 < cast(ulong)1 should evaluate to true? But according to
point 1 it would be rewritten as cast(ulong)-1 < cast(ulong)1 which is
0xFFFFFFFFFFFFFFFF < 0x1 and evaluate to false.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 09, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=259



--- Comment #43 from Stewart Gordon <smjg@iname.com> 2013-04-09 05:21:52 PDT ---
(In reply to comment #42)
> Yes, I'll have this done this week.
> For point 1: how to cope with the fact that I can safely cast an uint to long,
> but can't cast an int to an ulong (without issues)?
> Surely cast(int)-1 < cast(ulong)1 should evaluate to true? But according to
> point 1 it would be rewritten as cast(ulong)-1 < cast(ulong)1 which is
> 0xFFFFFFFFFFFFFFFF < 0x1 and evaluate to false.

No.  Point 1 is the case where a.sizeof > b.sizeof.  This isn't the case here.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 09, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=259



--- Comment #44 from Andrei Alexandrescu <andrei@erdani.com> 2013-04-09 08:37:50 PDT ---
Thanks Lionello for working on this! It will make D noticeably better at bread-and-butter work. Regarding your question - what Stewart said. Let me know if you hit any snag.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 10, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=259



--- Comment #45 from Lionello Lunesu <lio+bugzilla@lunesu.com> 2013-04-10 09:20:22 PDT ---
uint b;
if (b > -2) { ... }

The integral expression -2 doesn't have any size per se, but whole size thing shouldn't even apply here, since the two expressions have ranges that are completely disjoint. Points 2 and 3 won't catch this case either and would cause an error. We could consider all integrals 'long' and apply point 1. Or, test for disjoint ranges and replace the whole comparison with a constant? (I have a feeling this optimization might already be happening in a later pass?)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 10, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=259



--- Comment #46 from Andrei Alexandrescu <andrei@erdani.com> 2013-04-10 09:45:57 PDT ---
(In reply to comment #45)
> uint b;
> if (b > -2) { ... }
> 
> The integral expression -2 doesn't have any size per se, but whole size thing shouldn't even apply here, since the two expressions have ranges that are completely disjoint. Points 2 and 3 won't catch this case either and would cause an error. We could consider all integrals 'long' and apply point 1. Or, test for disjoint ranges and replace the whole comparison with a constant? (I have a feeling this optimization might already be happening in a later pass?)

I'd say we need to make this an error. If we don't (i.e. make the comparison always true), then we'd silently change behavior.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 12, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=259



--- Comment #47 from Lionello Lunesu <lio+bugzilla@lunesu.com> 2013-04-11 19:24:53 PDT ---
https://github.com/D-Programming-Language/dmd/pull/1889

There are many things silently(!) breaking, though, as some templates are not being chosen because of internal comparison errors.

For example, FormatSpec.width and FormatSpec.precision are ints but often compared against array lengths (for padding and such). TBH, using negative width and precision to mean "argument index" is a hack and we'd be better off changing them to uint and use a flag for the "argument index" case.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 12, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=259



--- Comment #48 from Lionello Lunesu <lio+bugzilla@lunesu.com> 2013-04-11 19:42:37 PDT ---
// For the record: my test cases. Will add/fix existing unittests as well.
import std.traits;
int i;
uint ui;
long l;
ulong ul;
// 0. same-signed-ness
static assert(__traits(compiles, ui>ul));
static assert(__traits(compiles, ul>ui));
static assert(__traits(compiles, i>l));
static assert(__traits(compiles, l>i));
static assert(__traits(compiles, 1>2));
static assert(!(1>2));
static assert(__traits(compiles, 2>1));
static assert(2>1);
// 1. sizeof(signed) > sizeof(unsigned)
static assert(__traits(compiles, l>ui));
static assert(__traits(compiles, ui>l));
static assert(__traits(compiles, -1L>2));
static assert(!(-1L>2));
static assert(__traits(compiles, 2>-1L));
static assert(2>-1L);
// 2. signed.min >= 0
static assert(__traits(compiles, ui>cast(int)2));
static assert(__traits(compiles, cast(int)2>ui));
static assert(__traits(compiles, ul>cast(int)2));
static assert(__traits(compiles, cast(int)2>ul));
// 3. unsigned.max < typeof(unsigned.max/2)
static assert(__traits(compiles, i>cast(uint)2));
static assert(__traits(compiles, cast(uint)2>i));
static assert(__traits(compiles, cast(int)-1>cast(uint)3));
static assert(__traits(compiles, cast(uint)3>cast(int)-1));
static assert(__traits(compiles, -1>2UL));
static assert(!(-1>2UL));
static assert(__traits(compiles, 2UL>-1));
static assert(2UL>-1);
// error
static assert(!__traits(compiles, ul>-2));
static assert(!__traits(compiles, -2>ul));
static assert(!__traits(compiles, i>ul));
static assert(!__traits(compiles, ul>i));
static assert(!__traits(compiles, l>ul));
static assert(!__traits(compiles, ul>l));
static assert(!__traits(compiles, i>ui));
static assert(!__traits(compiles, ui>i));

void main(){}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------