diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 238ee61d..c35208a7 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -290,8 +290,9 @@ func (q *checker) bcheckStatement(n *a.Node) error { q.facts = q.facts[:0] for _, o := range assigns { lhs := o.AsAssign().LHS() - q.facts = append(q.facts, - q.makeSliceLengthEqEq(lhs.Ident(), lhs.MType(), n.Length())) + lhsExpr := a.NewExpr(0, 0, lhs.Ident(), nil, nil, nil, nil) + lhsExpr.SetMType(lhs.MType()) + q.facts = append(q.facts, q.makeSliceLengthEqEq(lhsExpr, n.Length())) } if err := q.bcheckBlock(n.Body()); err != nil { return err @@ -522,6 +523,31 @@ func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error { } } + // Look for "lhs = x[i .. j]" where i and j are constants. + if rhs.Operator() == t.IDDotDot { + icv := (*big.Int)(nil) + if i := rhs.MHS().AsExpr(); i == nil { + icv = zero + } else if i.ConstValue() != nil { + icv = i.ConstValue() + } + + jcv := (*big.Int)(nil) + if j := rhs.RHS().AsExpr(); (j != nil) && (j.ConstValue() != nil) { + jcv = j.ConstValue() + } + + if (icv != nil) && (jcv != nil) { + n := big.NewInt(0).Sub(jcv, icv) + id, err := q.tm.Insert(n.String()) + if err != nil { + return err + } + // TODO: dupe lhs before making a new fact referencing it? + q.facts = append(q.facts, q.makeSliceLengthEqEq(lhs, id)) + } + } + } else { // Update any facts involving lhs. if err := q.facts.update(func(x *a.Expr) (*a.Expr, error) { @@ -1449,11 +1475,8 @@ func makeSliceLength(slice *a.Expr) *a.Expr { } // makeSliceLengthEqEq returns "x.length() == n". -func (q *checker) makeSliceLengthEqEq(x t.ID, xTyp *a.TypeExpr, n t.ID) *a.Expr { - xExpr := a.NewExpr(0, 0, x, nil, nil, nil, nil) - xExpr.SetMType(xTyp) - - lhs := makeSliceLength(xExpr) +func (q *checker) makeSliceLengthEqEq(x *a.Expr, n t.ID) *a.Expr { + lhs := makeSliceLength(x) nValue, err := strconv.Atoi(n.Str(q.tm)) if err != nil { diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index aca2b6a1..41f51c68 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -33481,9 +33481,6 @@ wuffs_png__decoder__swizzle_low_bit_depth( if (((uint64_t)(v_fragment.len)) > 128) { v_remaining = wuffs_base__slice_u8__subslice_i(v_fragment, 128); v_fragment = wuffs_base__slice_u8__subslice_j(v_fragment, 128); - if (((uint64_t)(v_fragment.len)) > 128) { - return wuffs_base__make_empty_struct(); - } } v_i = 0; while (v_i < ((uint64_t)(v_fragment.len))) { @@ -33502,9 +33499,6 @@ wuffs_png__decoder__swizzle_low_bit_depth( if (((uint64_t)(v_fragment.len)) > 256) { v_remaining = wuffs_base__slice_u8__subslice_i(v_fragment, 256); v_fragment = wuffs_base__slice_u8__subslice_j(v_fragment, 256); - if (((uint64_t)(v_fragment.len)) > 256) { - return wuffs_base__make_empty_struct(); - } } v_i = 0; while (v_i < ((uint64_t)(v_fragment.len))) { @@ -33519,9 +33513,6 @@ wuffs_png__decoder__swizzle_low_bit_depth( if (((uint64_t)(v_fragment.len)) > 512) { v_remaining = wuffs_base__slice_u8__subslice_i(v_fragment, 512); v_fragment = wuffs_base__slice_u8__subslice_j(v_fragment, 512); - if (((uint64_t)(v_fragment.len)) > 512) { - return wuffs_base__make_empty_struct(); - } } v_i = 0; while (v_i < ((uint64_t)(v_fragment.len))) { diff --git a/std/png/decode_png.wuffs b/std/png/decode_png.wuffs index a3640db1..7d5f28e0 100644 --- a/std/png/decode_png.wuffs +++ b/std/png/decode_png.wuffs @@ -761,9 +761,7 @@ pri func decoder.swizzle_low_bit_depth!(dst: slice base.u8, dst_palette: slice b if fragment.length() > 128 { remaining = fragment[128 ..] fragment = fragment[.. 128] - if fragment.length() > 128 { // TODO: unnecessary. - return nothing - } + assert fragment.length() <= 128 } i = 0 while i < fragment.length(), @@ -786,9 +784,7 @@ pri func decoder.swizzle_low_bit_depth!(dst: slice base.u8, dst_palette: slice b if fragment.length() > 256 { remaining = fragment[256 ..] fragment = fragment[.. 256] - if fragment.length() > 256 { // TODO: unnecessary. - return nothing - } + assert fragment.length() <= 256 } i = 0 while i < fragment.length(), @@ -807,9 +803,7 @@ pri func decoder.swizzle_low_bit_depth!(dst: slice base.u8, dst_palette: slice b if fragment.length() > 512 { remaining = fragment[512 ..] fragment = fragment[.. 512] - if fragment.length() > 512 { // TODO: unnecessary. - return nothing - } + assert fragment.length() <= 512 } i = 0 while i < fragment.length(),