Skip to content

Commit

Permalink
unstabilize == between unbounded and bounded ranges over enums and bo…
Browse files Browse the repository at this point in the history
…ols and return true (chapel-lang#23036)

In Issue chapel-lang#20896  we have decided that `==` for unbounded ranges with finite Idxtypes (i.e. enums and bools) should behave as if those ranges were bounded since the represented sequence is the same in both cases.
This means for example
```chapel
writeln(false..==false..true); // unstable; print 'true'
writeln(false.. by -1 ==..true by -1); // unstable; print 'true'
```

[Reviewed by @vasslitvinov ]
  • Loading branch information
ShreyasKhandekar authored Aug 23, 2023
2 parents ca412d6 + ecb91ec commit f9be3fc
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 12 deletions.
23 changes: 21 additions & 2 deletions modules/internal/ChapelRange.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -1496,9 +1496,28 @@ module ChapelRange {

@chpldoc.nodoc
operator ==(r1: range(?), r2: range(?)) param
where r1.bounds != r2.bounds do
where r1.bounds != r2.bounds &&
(!isFiniteIdxType(r1.idxType) ||
!isFiniteIdxType(r2.idxType)) do
return false;

@chpldoc.nodoc
@unstable("== between unbounded and bounded ranges is unstable and its behavior may change in the future")
operator ==(r1: range(?), r2: range(?)): bool
where r1.bounds != r2.bounds && isFiniteIdxType(r1.idxType) &&
isFiniteIdxType(r2.idxType)
{
const boundedr1 = if r1.bounds == boundKind.both then r1
else new range(
r1.idxType, boundKind.both, r1.strides,
r1._low, r1._high, r1._stride, r1._alignment);
const boundedr2 = if r2.bounds == boundKind.both then r2
else new range(
r2.idxType, boundKind.both, r2.strides,
r2._low, r2._high, r2._stride, r2._alignment);
return boundedr1 == boundedr2;
}

@chpldoc.nodoc
operator ==(r1: range(?), r2: range(?)): bool
where r1.bounds == r2.bounds
Expand Down Expand Up @@ -1571,7 +1590,7 @@ module ChapelRange {
{
if r1._low != r2._low then return false;
if r1._high != r2._high then return false;
// the following can be 'if none == none ...'
// the following can be 'if none != none ...'
if r1._stride != r2._stride then return false;
if r1._alignment != r2._alignment then return false;
return true;
Expand Down
17 changes: 16 additions & 1 deletion test/types/range/unbounded/compareEnumBool.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use color;
var rgb = red..blue;
var rdd = red..;
var ddb = ..blue;
var negrgb = red..blue by -1;
var negrdd = red.. by -1;
var negddb = ..blue by -1;
var dd: range(color, boundKind.neither);

writeln(rgb == rdd);
Expand All @@ -14,12 +17,19 @@ writeln(rdd == ddb);
writeln(rdd == dd);

writeln(ddb == dd);

writeln(negrgb == negrdd);
writeln(negrdd == negddb);
writeln(negrdd == dd);
writeln(rdd == negrdd);
writeln(ddb == negddb);

{
var ft = false..true;
var fdd = false..;
var ddt = ..true;
var negft = false..true by -1;
var negfdd = false.. by -1;
var negddt = ..true by -1;
var dd: range(bool, boundKind.neither);

writeln(ft == fdd);
Expand All @@ -30,4 +40,9 @@ writeln(fdd == ddt);
writeln(fdd == dd);

writeln(ddt == dd);
writeln(negft == negfdd);
writeln(negfdd == negddt);
writeln(negfdd == dd);
writeln(fdd == negfdd);
writeln(ddt == negddt);
}
28 changes: 19 additions & 9 deletions test/types/range/unbounded/compareEnumBool.good
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
false
false
false
false
false
false
false
false
false
true
true
true
true
true
true
true
true
false
false
false
true
true
true
true
true
true
true
true
false
false
false
44 changes: 44 additions & 0 deletions test/unstable/compareRangeUnboundedEnumBool.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
enum color {red, green, blue};
use color;

var rgb = red..blue;
var rdd = red..;
var ddb = ..blue;
var negrgb = red..blue by -1;
var negrdd = red.. by -1;
var negddb = ..blue by -1;
var dd: range(color, boundKind.neither);

writeln(rgb == rdd);
writeln(rgb == ddb);
writeln(rgb == dd);

writeln(rdd == ddb);
writeln(rdd == dd);

writeln(ddb == dd);
writeln(negrgb == negrdd);
writeln(negrdd == negddb);
writeln(negrdd == dd);

{
var ft = false..true;
var fdd = false..;
var ddt = ..true;
var negft = false..true by -1;
var negfdd = false.. by -1;
var negddt = ..true by -1;
var dd: range(bool, boundKind.neither);

writeln(ft == fdd);
writeln(ft == ddt);
writeln(ft == dd);

writeln(fdd == ddt);
writeln(fdd == dd);

writeln(ddt == dd);
writeln(negft == negfdd);
writeln(negfdd == negddt);
writeln(negfdd == dd);
}
36 changes: 36 additions & 0 deletions test/unstable/compareRangeUnboundedEnumBool.good
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
compareRangeUnboundedEnumBool.chpl:12: warning: == between unbounded and bounded ranges is unstable and its behavior may change in the future
compareRangeUnboundedEnumBool.chpl:13: warning: == between unbounded and bounded ranges is unstable and its behavior may change in the future
compareRangeUnboundedEnumBool.chpl:14: warning: == between unbounded and bounded ranges is unstable and its behavior may change in the future
compareRangeUnboundedEnumBool.chpl:16: warning: == between unbounded and bounded ranges is unstable and its behavior may change in the future
compareRangeUnboundedEnumBool.chpl:17: warning: == between unbounded and bounded ranges is unstable and its behavior may change in the future
compareRangeUnboundedEnumBool.chpl:19: warning: == between unbounded and bounded ranges is unstable and its behavior may change in the future
compareRangeUnboundedEnumBool.chpl:20: warning: == between unbounded and bounded ranges is unstable and its behavior may change in the future
compareRangeUnboundedEnumBool.chpl:21: warning: == between unbounded and bounded ranges is unstable and its behavior may change in the future
compareRangeUnboundedEnumBool.chpl:22: warning: == between unbounded and bounded ranges is unstable and its behavior may change in the future
compareRangeUnboundedEnumBool.chpl:33: warning: == between unbounded and bounded ranges is unstable and its behavior may change in the future
compareRangeUnboundedEnumBool.chpl:34: warning: == between unbounded and bounded ranges is unstable and its behavior may change in the future
compareRangeUnboundedEnumBool.chpl:35: warning: == between unbounded and bounded ranges is unstable and its behavior may change in the future
compareRangeUnboundedEnumBool.chpl:37: warning: == between unbounded and bounded ranges is unstable and its behavior may change in the future
compareRangeUnboundedEnumBool.chpl:38: warning: == between unbounded and bounded ranges is unstable and its behavior may change in the future
compareRangeUnboundedEnumBool.chpl:40: warning: == between unbounded and bounded ranges is unstable and its behavior may change in the future
compareRangeUnboundedEnumBool.chpl:41: warning: == between unbounded and bounded ranges is unstable and its behavior may change in the future
compareRangeUnboundedEnumBool.chpl:42: warning: == between unbounded and bounded ranges is unstable and its behavior may change in the future
compareRangeUnboundedEnumBool.chpl:43: warning: == between unbounded and bounded ranges is unstable and its behavior may change in the future
true
true
true
true
true
true
true
true
false
true
true
true
true
true
true
true
true
false

0 comments on commit f9be3fc

Please sign in to comment.