Reject "x as T" if x can overflow T.

This commit is contained in:
Nigel Tao committed 2017-06-10 10:39:52 +10:00
1 parent 587bc2a55d
commit 8cd43d2c3c
2 files changed
+7 -7

No files matched your search

+6 -7
View File
@@ -576,13 +576,12 @@ func (q *checker) bcheckExpr(n *a.Expr, depth uint32) (*big.Int, *big.Int, error
if err != nil {
return nil, nil, err
}
nMin, nMax := max(lMin, rMin), min(lMax, rMax)
// TODO: check if nMin > nMax.
//
// TODO: should it be a compile time error if "x as T" can narrow
// the range? Do we want a separate "~as" operator that ignores
// overflow?
return nMin, nMax, nil
if lMin.Cmp(rMin) < 0 || lMax.Cmp(rMax) > 0 {
return nil, nil, fmt.Errorf("check: expression %q bounds [%v..%v] is not within bounds [%v..%v]",
n.LHS().Expr().String(q.tm), lMin, lMax, rMin, rMax,
)
}
return lMin, lMax, nil
}
return q.bcheckExprBinaryOp(n.LHS().Expr(), n.ID0().Key(), n.RHS().Expr(), depth)
case t.FlagsAssociativeOp: