Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public TransferResult<CFValue, CFStore> visitGreaterThan(

// Refine the else branch, which is the inverse of the then branch, unless an operand might be
// NaN.
if (!isFloatingPointComparison(rfi)) {
if (!isFloatingPointComparison(rfi.left, rfi.right)) {
refineGTE(rfi.right, rfi.rightAnno, rfi.left, rfi.leftAnno, rfi.elseStore, in);
}

Expand All @@ -60,7 +60,7 @@ public TransferResult<CFValue, CFStore> visitGreaterThanOrEqual(
refineGTE(rfi.left, rfi.leftAnno, rfi.right, rfi.rightAnno, rfi.thenStore, in);

// Refine the else branch, unless an operand might be NaN.
if (!isFloatingPointComparison(rfi)) {
if (!isFloatingPointComparison(rfi.left, rfi.right)) {
refineGT(rfi.right, rfi.rightAnno, rfi.left, rfi.leftAnno, rfi.elseStore, in);
}

Expand All @@ -81,7 +81,7 @@ public TransferResult<CFValue, CFStore> visitLessThanOrEqual(
refineGTE(rfi.right, rfi.rightAnno, rfi.left, rfi.leftAnno, rfi.thenStore, in);

// Refine the else branch, unless an operand might be NaN.
if (!isFloatingPointComparison(rfi)) {
if (!isFloatingPointComparison(rfi.left, rfi.right)) {
refineGT(rfi.left, rfi.leftAnno, rfi.right, rfi.rightAnno, rfi.elseStore, in);
}
return rfi.newResult;
Expand All @@ -101,24 +101,35 @@ public TransferResult<CFValue, CFStore> visitLessThan(
refineGT(rfi.right, rfi.rightAnno, rfi.left, rfi.leftAnno, rfi.thenStore, in);

// Refine the else branch, unless an operand might be NaN.
if (!isFloatingPointComparison(rfi)) {
if (!isFloatingPointComparison(rfi.left, rfi.right)) {
refineGTE(rfi.left, rfi.leftAnno, rfi.right, rfi.rightAnno, rfi.elseStore, in);
}
return rfi.newResult;
}

/**
* Returns true if the given comparison is a floating-point comparison: that is, if an operand has
* a floating-point type, primitive or boxed. An operand of such a comparison might be NaN. Every
* comparison with NaN is false, so the else branch of such a comparison does not imply the
* inverse comparison: if {@code d} is NaN, then both {@code i > d} and {@code i <= d} are false.
* Returns true if the comparison of the given operands is a floating-point comparison: that is,
* if an operand has a floating-point type, primitive or boxed. Such a comparison differs from an
* integral comparison in two ways.
*
* @param rfi refinement information for a numerical comparison
* @return true if the comparison is a floating-point comparison
* <ul>
* <li>An operand of such a comparison might be NaN. Every comparison with NaN is false, so the
* else branch of such a comparison does not imply the inverse comparison: if {@code d} is
* NaN, then both {@code i > d} and {@code i <= d} are false.
* <li>The comparison converts an integral operand to a floating-point type, which might round
* it. If the {@code float} {@code f} is 16777216.0f and the {@code int} {@code i} is
* 16777217, then {@code f >= i} is true, because {@code i} rounds to 16777216.0f, but
* {@code i < f + 1} is false. A strict comparison is not affected, because rounding is
* monotonic: if {@code f > i} is true, then so is {@code i < f}.
* </ul>
*
* @param left the left operand of a numerical comparison
* @param right the right operand of the comparison
* @return true if the comparison of {@code left} and {@code right} is a floating-point comparison
*/
private static boolean isFloatingPointComparison(IndexRefinementInfo rfi) {
return TypesUtils.isFloatingPoint(rfi.left.getType())
|| TypesUtils.isFloatingPoint(rfi.right.getType());
protected static boolean isFloatingPointComparison(Node left, Node right) {
return TypesUtils.isFloatingPoint(left.getType())
|| TypesUtils.isFloatingPoint(right.getType());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
*
* <ul>
* <li>1. if left &gt; right, right has type {@code @LessThan("left")}
* <li>2. if left &ge; right, right has type {@code @LessThan("left + 1")}
* <li>2. if left &ge; right, right has type {@code @LessThan("left + 1")}, unless the comparison
* is a floating-point comparison (see {@link #isFloatingPointComparison})
* <li>3. if {@code 0 < right}, {@code left - right} has type {@code @LessThan("left")}
* </ul>
*
Expand Down Expand Up @@ -80,9 +81,9 @@ protected void refineGTE(
TransferInput<CFValue, CFStore> in) {
// left >= right so right is less than left
// Refine right to @LessThan("left + 1")

// left > right so right is less than left
// Refine right to @LessThan("left")
if (isFloatingPointComparison(left, right)) {
return;
}
JavaExpression leftJe = JavaExpression.fromNode(left);
if (leftJe != null && !leftJe.isAssignableByOtherCode()) {
if (isDoubleOrFloatLiteral(leftJe)) {
Expand Down
47 changes: 47 additions & 0 deletions checker/tests/index/LessThanFloatComparison.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import org.checkerframework.checker.index.qual.LessThan;

// A non-strict comparison that is performed in floating point does not imply an integral bound,
// because converting an integral operand to a floating-point type might round it. A strict
// comparison does imply a bound, because rounding is monotonic.
class LessThanFloatComparison {

// If `limit` is 16777216 and `i` is 16777217, then `f` is 16777216.0f and `f >= i` is true,
// because `i` rounds to 16777216.0f. But `i < f + 1` is false.
void floatRounding(int limit, int i) {
float f = limit;
if (f >= i) {
// :: error: [assignment]
@LessThan("f + 1") int j = i;
}
}

// If `limit` is 2^53 and `i` is 2^53 + 1, then `d >= i` is true, because `i` rounds to 2^53.
void doubleRounding(long limit, long i) {
double d = limit;
if (d >= i) {
// :: error: [assignment]
@LessThan("d + 1") long j = i;
}
}

void floatStrict(float f, int i) {
if (f > i) {
@LessThan("f") int j = i;
}
if (i < f) {
@LessThan("f") int j = i;
}
}

void doubleStrict(double d, long i) {
if (d > i) {
@LessThan("d") long j = i;
}
}

void intComparison(int limit, int i) {
if (limit >= i) {
@LessThan("limit + 1") int j = i;
}
}
}