diff --git a/lang/check/bounds.go b/lang/check/bounds.go index f82c6164..286b7677 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -660,7 +660,7 @@ func (q *checker) bcheckWhile(n *a.While) error { } } - if cv := n.Condition().ConstValue(); cv != nil && cv.Cmp(zero) == 0 { + if cv := n.Condition().ConstValue(); cv != nil && cv.Sign() == 0 { // We effectively have a "while false { etc }" loop. There's no need to // check the body. } else { @@ -815,7 +815,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, if err != nil { return nil, nil, err } - if aMin.Cmp(zero) < 0 { + if aMin.Sign() < 0 { // TODO: error, but a better check than aMin < 0 is that // a.MType() is u32. Checking this properly should fall out // when a *a.TypeExpr can express function types. @@ -1046,10 +1046,10 @@ func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax case t.KeyXBinaryStar: // TODO: handle multiplication by negative numbers. Note that this // might reverse the inequality: if 0 < a < b but c < 0 then a*c > b*c. - if lMin.Cmp(zero) < 0 { + if lMin.Sign() < 0 { return nil, nil, fmt.Errorf("check: multiply op argument %q is possibly negative", lhs.Str(q.tm)) } - if rMin.Cmp(zero) < 0 { + if rMin.Sign() < 0 { return nil, nil, fmt.Errorf("check: multiply op argument %q is possibly negative", rhs.Str(q.tm)) } return big.NewInt(0).Mul(lMin, rMin), big.NewInt(0).Mul(lMax, rMax), nil @@ -1058,10 +1058,10 @@ func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax // TODO. case t.KeyXBinaryShiftL: - if lMin.Cmp(zero) < 0 { + if lMin.Sign() < 0 { return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.Str(q.tm)) } - if rMin.Cmp(zero) < 0 { + if rMin.Sign() < 0 { return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", rhs.Str(q.tm)) } if rMax.Cmp(ffff) > 0 { @@ -1070,10 +1070,10 @@ func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax return big.NewInt(0).Lsh(lMin, uint(rMin.Uint64())), big.NewInt(0).Lsh(lMax, uint(rMax.Uint64())), nil case t.KeyXBinaryShiftR: - if lMin.Cmp(zero) < 0 { + if lMin.Sign() < 0 { return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.Str(q.tm)) } - if rMin.Cmp(zero) < 0 { + if rMin.Sign() < 0 { return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", rhs.Str(q.tm)) } if rMin.Cmp(maxIntBits) >= 0 { @@ -1088,13 +1088,13 @@ func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax case t.KeyXBinaryAmp, t.KeyXBinaryPipe: // TODO: should type-checking ensure that bitwise ops only apply to // *unsigned* integer types? - if lMin.Cmp(zero) < 0 { + if lMin.Sign() < 0 { return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly negative", lhs.Str(q.tm)) } if lMax.Cmp(numTypeBounds[t.KeyU64][1]) > 0 { return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly too large", lhs.Str(q.tm)) } - if rMin.Cmp(zero) < 0 { + if rMin.Sign() < 0 { return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly negative", rhs.Str(q.tm)) } if rMax.Cmp(numTypeBounds[t.KeyU64][1]) > 0 { @@ -1117,10 +1117,10 @@ func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax // TODO. case t.KeyXBinaryPercent: - if lMin.Cmp(zero) < 0 { + if lMin.Sign() < 0 { return nil, nil, fmt.Errorf("check: modulus op argument %q is possibly negative", lhs.Str(q.tm)) } - if rMin.Cmp(zero) <= 0 { + if rMin.Sign() <= 0 { return nil, nil, fmt.Errorf("check: modulus op argument %q is possibly non-positive", rhs.Str(q.tm)) } return zero, big.NewInt(0).Sub(rMax, one), nil diff --git a/lang/check/optimize.go b/lang/check/optimize.go index b77f12d9..b7d8bbbc 100644 --- a/lang/check/optimize.go +++ b/lang/check/optimize.go @@ -119,7 +119,7 @@ check1: if lhs := x.LHS().Expr(); !lhs.Eq(d) { continue } - if rcv := x.RHS().Expr().ConstValue(); rcv == nil || rcv.Cmp(zero) != 0 { + if rcv := x.RHS().Expr().ConstValue(); rcv == nil || rcv.Sign() != 0 { continue } break check1 diff --git a/lang/check/type.go b/lang/check/type.go index efb5838b..85278638 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -769,7 +769,7 @@ func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error { n.Operator().AmbiguousForm().Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm)) } if cv := rhs.ConstValue(); cv != nil { - n.SetConstValue(btoi(cv.Cmp(zero) == 0)) + n.SetConstValue(btoi(cv.Sign() == 0)) } n.SetMType(typeExprBool) return nil @@ -906,20 +906,20 @@ func evalConstValueBinaryOp(tm *t.Map, n *a.Expr, l *big.Int, r *big.Int) (*big. case t.KeyXBinaryStar: return big.NewInt(0).Mul(l, r), nil case t.KeyXBinarySlash: - if r.Cmp(zero) == 0 { + if r.Sign() == 0 { return nil, fmt.Errorf("check: division by zero in const expression %q", n.Str(tm)) } // TODO: decide on Euclidean division vs other definitions. See "go doc // math/big int.divmod" for details. return big.NewInt(0).Div(l, r), nil case t.KeyXBinaryShiftL: - if r.Cmp(zero) < 0 || r.Cmp(ffff) > 0 { + if r.Sign() < 0 || r.Cmp(ffff) > 0 { return nil, fmt.Errorf("check: shift %q out of range in const expression %q", n.RHS().Expr().Str(tm), n.Str(tm)) } return big.NewInt(0).Lsh(l, uint(r.Uint64())), nil case t.KeyXBinaryShiftR: - if r.Cmp(zero) < 0 || r.Cmp(ffff) > 0 { + if r.Sign() < 0 || r.Cmp(ffff) > 0 { return nil, fmt.Errorf("check: shift %q out of range in const expression %q", n.RHS().Expr().Str(tm), n.Str(tm)) } @@ -933,7 +933,7 @@ func evalConstValueBinaryOp(tm *t.Map, n *a.Expr, l *big.Int, r *big.Int) (*big. case t.KeyXBinaryHat: return big.NewInt(0).Xor(l, r), nil case t.KeyXBinaryPercent: - if r.Cmp(zero) == 0 { + if r.Sign() == 0 { return nil, fmt.Errorf("check: division by zero in const expression %q", n.Str(tm)) } return big.NewInt(0).Mod(l, r), nil @@ -950,9 +950,9 @@ func evalConstValueBinaryOp(tm *t.Map, n *a.Expr, l *big.Int, r *big.Int) (*big. case t.KeyXBinaryGreaterThan: return btoi(l.Cmp(r) > 0), nil case t.KeyXBinaryAnd: - return btoi((l.Cmp(zero) != 0) && (r.Cmp(zero) != 0)), nil + return btoi((l.Sign() != 0) && (r.Sign() != 0)), nil case t.KeyXBinaryOr: - return btoi((l.Cmp(zero) != 0) || (r.Cmp(zero) != 0)), nil + return btoi((l.Sign() != 0) || (r.Sign() != 0)), nil case t.KeyXBinaryTildePlus: return nil, fmt.Errorf("check: cannot apply ~+ operator to ideal numbers") }