diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 1e19f26d..7ed8cf71 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -1153,7 +1153,7 @@ func (q *checker) bcheckExprCallSpecialCases(n *a.Expr, depth uint32) (bounds, e recv := lhs.LHS().AsExpr() method := lhs.Ident() - advance, advanceExpr, update := (*big.Int)(nil), (*a.Expr)(nil), false + actualAdvanceIsAlwaysPositive, advance, advanceExpr, update := false, (*big.Int)(nil), (*a.Expr)(nil), false if recvTyp := recv.MType(); recvTyp == nil { return bounds{}, errNotASpecialCase @@ -1233,6 +1233,9 @@ func (q *checker) bcheckExprCallSpecialCases(n *a.Expr, depth uint32) (bounds, e } else if err != nil { return bounds{}, err } + if cv := actual.ConstValue(); (cv != nil) && (cv.Sign() > 0) { + actualAdvanceIsAlwaysPositive = true + } if cv := worstCase.ConstValue(); cv != nil { advance, update = cv, true } else { @@ -1298,6 +1301,9 @@ func (q *checker) bcheckExprCallSpecialCases(n *a.Expr, depth uint32) (bounds, e return bounds{}, fmt.Errorf("check: could not prove %s pre-condition: %s.length() >= %s", method.Str(q.tm), subject.Str(q.tm), adv) } + if actualAdvanceIsAlwaysPositive { + q.facts = append(q.facts, makeCanUndoByte(recv)) + } // TODO: drop other subject-related facts? } @@ -1536,15 +1542,26 @@ func makeConstValueExpr(tm *t.Map, cv *big.Int) (*a.Expr, error) { return o, nil } -// makeSliceLength returns "x.length()". -func makeSliceLength(slice *a.Expr) *a.Expr { - x := a.NewExpr(0, t.IDDot, t.IDLength, slice.AsNode(), nil, nil, nil) - x.SetMBounds(bounds{funcBounds, funcBounds}) - x.SetMType(a.NewTypeExpr(t.IDFunc, 0, t.IDLength, slice.MType().AsNode(), nil, nil)) - x = a.NewExpr(0, t.IDOpenParen, 0, x.AsNode(), nil, nil, nil) +// makeCanUndoByte returns "x.can_undo_byte()". +func makeCanUndoByte(x *a.Expr) *a.Expr { + ret := a.NewExpr(0, t.IDDot, t.IDCanUndoByte, x.AsNode(), nil, nil, nil) + ret.SetMBounds(bounds{funcBounds, funcBounds}) + ret.SetMType(a.NewTypeExpr(t.IDFunc, 0, t.IDCanUndoByte, x.MType().AsNode(), nil, nil)) + ret = a.NewExpr(0, t.IDOpenParen, 0, ret.AsNode(), nil, nil, nil) // TODO: call SetMBounds? - x.SetMType(typeExprU64) - return x + ret.SetMType(typeExprBool) + return ret +} + +// makeSliceLength returns "x.length()". +func makeSliceLength(x *a.Expr) *a.Expr { + ret := a.NewExpr(0, t.IDDot, t.IDLength, x.AsNode(), nil, nil, nil) + ret.SetMBounds(bounds{funcBounds, funcBounds}) + ret.SetMType(a.NewTypeExpr(t.IDFunc, 0, t.IDLength, x.MType().AsNode(), nil, nil)) + ret = a.NewExpr(0, t.IDOpenParen, 0, ret.AsNode(), nil, nil, nil) + // TODO: call SetMBounds? + ret.SetMType(typeExprU64) + return ret } // makeSliceLengthEqEq returns "x.length() == n". diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 350ab54a..73930243 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -41424,14 +41424,10 @@ wuffs_jpeg__decoder__fill_bitstream( if (v_c < 255u) { goto label__0__continue; } else if (((uint64_t)(io2_a_src - iop_a_src)) <= 0u) { - if (iop_a_src > io1_a_src) { - iop_a_src--; - } + iop_a_src--; goto label__0__break; } else if (wuffs_base__peek_u8be__no_bounds_check(iop_a_src) > 0u) { - if (iop_a_src > io1_a_src) { - iop_a_src--; - } + iop_a_src--; goto label__0__break; } else { iop_a_src += 1u; diff --git a/std/jpeg/decode_jpeg.wuffs b/std/jpeg/decode_jpeg.wuffs index fdf7f4c3..dae40c4c 100644 --- a/std/jpeg/decode_jpeg.wuffs +++ b/std/jpeg/decode_jpeg.wuffs @@ -1503,15 +1503,11 @@ pri func decoder.fill_bitstream!(src: base.io_reader) { continue } else if args.src.length() <= 0 { - if args.src.can_undo_byte() { - args.src.undo_byte!() - } + args.src.undo_byte!() break } else if args.src.peek_u8() > 0 { - if args.src.can_undo_byte() { - args.src.undo_byte!() - } + args.src.undo_byte!() break } else {