From 10146b75ddff07d35b8320151e85770e237182f4 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Tue, 2 Feb 2021 22:11:45 +1100 Subject: [PATCH] Drop old facts after an impure function call --- lang/check/bounds.go | 43 ++++++++++++++++++++++++-- release/c/wuffs-unsupported-snapshot.c | 1 + std/png/decode_png.wuffs | 3 +- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 48399997..238ee61d 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -434,6 +434,14 @@ func (q *checker) bcheckAssert(n *a.Assert) error { } func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error { + oldFacts := (map[*a.Expr]struct{})(nil) + if (rhs.Operator() == t.IDOpenParen) && rhs.Effect().Impure() { + oldFacts = map[*a.Expr]struct{}{} + for _, x := range q.facts { + oldFacts[x] = struct{}{} + } + } + lTyp := (*a.TypeExpr)(nil) if lhs != nil { if _, err := q.bcheckExpr(lhs, 0); err != nil { @@ -447,8 +455,39 @@ func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error { return err } - if rhs.Effect().Coroutine() && (op != t.IDEqQuestion) { - if err := q.facts.update(updateFactsForSuspension); err != nil { + if (rhs.Operator() == t.IDOpenParen) && rhs.Effect().Impure() { + if rhs.Effect().Coroutine() && (op != t.IDEqQuestion) { + if err := q.facts.update(updateFactsForSuspension); err != nil { + return err + } + } + + recv := rhs.LHS().AsExpr().LHS().AsExpr() + if err := q.facts.update(func(x *a.Expr) (*a.Expr, error) { + if _, ok := oldFacts[x]; !ok { + // No-op. Don't drop any newly minted facts. + } else { + // Drop any old facts involving the receiver. + if x.Mentions(recv) { + return nil, nil + } + // Drop any facts involving a pass-by-reference argument. + for _, arg := range rhs.Args() { + v := arg.AsArg().Value() + if typ := v.MType(); typ.IsBool() || typ.IsNullptr() || + typ.IsNumTypeOrIdeal() || typ.IsStatus() { + continue + } + // TODO: take extra care if v is a slice? For example, + // facts involving "v.length()" aren't affected by passing + // v to an impure function. + if x.Mentions(v) { + return nil, nil + } + } + } + return x, nil + }); err != nil { return err } } diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 3616613f..aca2b6a1 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -32404,6 +32404,7 @@ wuffs_png__decoder__decode_ihdr( uint8_t t_3 = *iop_a_src++; self->private_impl.f_color_type = t_3; } + self->private_impl.f_bytes_per_row = 0; wuffs_png__decoder__calculate_bytes_per_row(self); if (self->private_impl.f_bytes_per_row == 0) { status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file); diff --git a/std/png/decode_png.wuffs b/std/png/decode_png.wuffs index f967b686..a3640db1 100644 --- a/std/png/decode_png.wuffs +++ b/std/png/decode_png.wuffs @@ -247,8 +247,7 @@ pri func decoder.decode_ihdr?(src: base.io_reader) { // Color. this.color_type = args.src.read_u8?() - // TODO: uncomment. - // this.bytes_per_row = 0 + this.bytes_per_row = 0 this.calculate_bytes_per_row!() if this.bytes_per_row == 0 { return "#unsupported PNG file"