Propagate "x == constValue" in bounds checking.

This commit is contained in:
Nigel Tao committed 2017-06-10 11:53:50 +10:00
1 parent 793eb043a4
commit 70d62bc608
3 files changed
+43 -24

No files matched your search

+26 -15
View File
@@ -558,23 +558,34 @@ func (q *checker) bcheckExpr(n *a.Expr, depth uint32) (nMin *big.Int, nMax *big.
if cv := n.ConstValue(); cv != nil {
nMin, nMax = cv, cv
} else {
// TODO: look in q.facts for "n == constantValue".
switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
case 0:
nMin, nMax, retErr = q.bcheckExprOther(n, depth)
case t.FlagsUnaryOp:
nMin, nMax, retErr = q.bcheckExprUnaryOp(n, depth)
case t.FlagsBinaryOp:
if n.ID0().Key() == t.KeyXBinaryAs {
nMin, nMax, retErr = q.bcheckExpr(n.LHS().Expr(), depth)
} else {
nMin, nMax, retErr = q.bcheckExprBinaryOp(n.LHS().Expr(), n.ID0().Key(), n.RHS().Expr(), depth)
// Look in the known facts for "n == constantValue".
for _, x := range q.facts {
if other := eqEqOtherHandSide(x, n); other != nil {
if cv := other.ConstValue(); cv != nil {
nMin, nMax = cv, cv
break
}
}
case t.FlagsAssociativeOp:
nMin, nMax, retErr = q.bcheckExprAssociativeOp(n, depth)
}
default:
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExpr", n.ID0().Key())
if nMin == nil {
switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
case 0:
nMin, nMax, retErr = q.bcheckExprOther(n, depth)
case t.FlagsUnaryOp:
nMin, nMax, retErr = q.bcheckExprUnaryOp(n, depth)
case t.FlagsBinaryOp:
if n.ID0().Key() == t.KeyXBinaryAs {
nMin, nMax, retErr = q.bcheckExpr(n.LHS().Expr(), depth)
} else {
nMin, nMax, retErr = q.bcheckExprBinaryOp(n.LHS().Expr(), n.ID0().Key(), n.RHS().Expr(), depth)
}
case t.FlagsAssociativeOp:
nMin, nMax, retErr = q.bcheckExprAssociativeOp(n, depth)
default:
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExpr", n.ID0().Key())
}
}
}
if retErr != nil {