cue: fix linter warnings and divide by zero error
Spec says the result of divide by zero
is an error, not infinity.
Change-Id: I47faa0cc91325d9a729cbb444aaa924b553604ef
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2864
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cue/binop.go b/cue/binop.go
index f5a6068..d83fb16 100644
--- a/cue/binop.go
+++ b/cue/binop.go
@@ -438,7 +438,7 @@
case diff == 2:
if k&floatKind == 0 && x.op == opGtr && y.op == opLss {
- apd.BaseContext.Add(&d, d.SetInt64(1), &a.v)
+ _, _ = apd.BaseContext.Add(&d, d.SetInt64(1), &a.v)
n := *a
n.k = k
n.v = d
@@ -902,22 +902,37 @@
case opLss, opLeq, opEql, opNeq, opGeq, opGtr:
return cmpTonode(src, op, x.v.Cmp(&y.v))
case opAdd:
- ctx.Add(&n.v, &x.v, &y.v)
+ _, _ = ctx.Add(&n.v, &x.v, &y.v)
case opSub:
- ctx.Sub(&n.v, &x.v, &y.v)
+ _, _ = ctx.Sub(&n.v, &x.v, &y.v)
case opMul:
- ctx.Mul(&n.v, &x.v, &y.v)
+ _, _ = ctx.Mul(&n.v, &x.v, &y.v)
case opQuo:
- ctx.Quo(&n.v, &x.v, &y.v)
- ctx.Reduce(&n.v, &n.v)
+ cond, _ := ctx.Quo(&n.v, &x.v, &y.v)
+ if cond.DivisionByZero() {
+ return ctx.mkErr(src, "divide by zero")
+ }
+ _, _, _ = ctx.Reduce(&n.v, &n.v)
n.k = floatKind
case opIDiv:
+ if y.v.IsZero() {
+ return ctx.mkErr(src, "divide by zero")
+ }
intOp(ctx, n, (*big.Int).Div, x, y)
case opIMod:
+ if y.v.IsZero() {
+ return ctx.mkErr(src, "divide by zero")
+ }
intOp(ctx, n, (*big.Int).Mod, x, y)
case opIQuo:
+ if y.v.IsZero() {
+ return ctx.mkErr(src, "divide by zero")
+ }
intOp(ctx, n, (*big.Int).Quo, x, y)
case opIRem:
+ if y.v.IsZero() {
+ return ctx.mkErr(src, "divide by zero")
+ }
intOp(ctx, n, (*big.Int).Rem, x, y)
}
return n
@@ -938,11 +953,11 @@
func intOp(ctx *context, n *numLit, fn intFunc, a, b *numLit) {
var x, y apd.Decimal
- ctx.RoundToIntegralValue(&x, &a.v)
+ _, _ = ctx.RoundToIntegralValue(&x, &a.v)
if x.Negative {
x.Coeff.Neg(&x.Coeff)
}
- ctx.RoundToIntegralValue(&y, &b.v)
+ _, _ = ctx.RoundToIntegralValue(&y, &b.v)
if y.Negative {
y.Coeff.Neg(&y.Coeff)
}
@@ -990,7 +1005,8 @@
}
n.v.SetInt64(int64(x.d))
d := apd.New(int64(y.d), 0)
- ctx.Quo(&n.v, &n.v, d)
+ // TODO: check result if this code becomes undead.
+ _, _ = ctx.Quo(&n.v, &n.v, d)
return n
case opIRem:
n := &numLit{
diff --git a/cue/resolve_test.go b/cue/resolve_test.go
index 9a07c9c..d0a114c 100644
--- a/cue/resolve_test.go
+++ b/cue/resolve_test.go
@@ -136,6 +136,7 @@
sum: -1 + +2 // 1
div1: 2.0 / 3 * 6 // 4
div2: 2 / 3 * 6 // 4
+ divZero: 1.0 / 0
b: 1 != 4
v1: 1.0T/2.0
@@ -158,6 +159,7 @@
`sum: 1, ` +
`div1: 4.00000000000000000000000, ` +
`div2: 4.00000000000000000000000, ` +
+ `divZero: _|_((1.0 / 0):divide by zero), ` +
`b: true, ` +
`v1: 5e+11, ` +
`v2: true, ` +
diff --git a/cue/types_test.go b/cue/types_test.go
index f84f448..e31e6b1 100644
--- a/cue/types_test.go
+++ b/cue/types_test.go
@@ -342,20 +342,12 @@
kind: FloatKind,
}, {
value: "1/0",
- float: "∞",
- float64: math.Inf(1),
+ float: "",
+ float64: 0,
prec: 2,
fmt: 'f',
- err: ErrAbove.Error(),
- kind: FloatKind,
- }, {
- value: "-1/0",
- float: "-∞",
- float64: math.Inf(-1),
- prec: 2,
- fmt: 'f',
- err: ErrBelow.Error(),
- kind: FloatKind,
+ err: "divide by zero",
+ kind: BottomKind,
}, {
value: "1.797693134862315708145274237317043567982e+308",
float: "1.8e+308",