diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 03ee9ce8..7413fd66 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -820,17 +820,6 @@ const MaxBodyDepth = 255 // - RHS: out-parameters // - List1: asserts // - List2: body -// -// Statement means one of: -// - Assert -// - Assign -// - IOBind -// - If -// - Iterate -// - Jump -// - Ret -// - Var -// - While type Func Node func (n *Func) AsNode() *Node { return (*Node)(n) } @@ -989,3 +978,48 @@ func NewFile(filename string, topLevelDecls []*Node) *File { list0: topLevelDecls, } } + +// Statement means one of: +// - Assert +// - Assign +// - IOBind +// - If +// - Iterate +// - Jump +// - Ret +// - Var +// - While + +// Terminates returns whether a block of statements terminates. In other words, +// whether the block is non-empty and its final statement is a "return", +// "break", "continue" or an "if-else" chain where all branches terminate. +func Terminates(body []*Node) bool { + // TODO: strengthen this to include "while" statements? For inspiration, + // the Go spec has https://golang.org/ref/spec#Terminating_statements + if len(body) == 0 { + return false + } + n := body[len(body)-1] + switch n.Kind() { + case KIf: + n := n.AsIf() + for { + if !Terminates(n.BodyIfTrue()) { + return false + } + bif := n.BodyIfFalse() + if len(bif) > 0 && !Terminates(bif) { + return false + } + n = n.ElseIf() + if n == nil { + return len(bif) > 0 + } + } + case KJump: + return true + case KRet: + return n.AsRet().Keyword() == t.IDReturn + } + return false +} diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 16450ea7..f8da3d91 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -532,41 +532,6 @@ func (q *checker) bcheckAssignment1(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs return rb, nil } -// terminates returns whether a block of statements terminates. In other words, -// whether the block is non-empty and its final statement is a "return", -// "break", "continue" or an "if-else" chain where all branches terminate. -// -// TODO: strengthen this to include "while" statements? For inspiration, the Go -// spec has https://golang.org/ref/spec#Terminating_statements -func terminates(body []*a.Node) bool { - if len(body) > 0 { - n := body[len(body)-1] - switch n.Kind() { - case a.KIf: - n := n.AsIf() - for { - if !terminates(n.BodyIfTrue()) { - return false - } - bif := n.BodyIfFalse() - if len(bif) > 0 && !terminates(bif) { - return false - } - n = n.ElseIf() - if n == nil { - return len(bif) > 0 - } - } - case a.KJump: - return true - case a.KRet: - return n.AsRet().Keyword() == t.IDReturn - } - return false - } - return false -} - func snapshot(facts []*a.Expr) []*a.Expr { return append([]*a.Expr(nil), facts...) } @@ -615,7 +580,7 @@ func (q *checker) bcheckIf(n *a.If) error { if err := q.bcheckBlock(n.BodyIfTrue()); err != nil { return err } - if !terminates(n.BodyIfTrue()) { + if !a.Terminates(n.BodyIfTrue()) { branches = append(branches, snapshot(q.facts)) } @@ -632,7 +597,7 @@ func (q *checker) bcheckIf(n *a.If) error { if err := q.bcheckBlock(bif); err != nil { return err } - if !terminates(bif) { + if !a.Terminates(bif) { branches = append(branches, snapshot(q.facts)) } break @@ -717,7 +682,7 @@ func (q *checker) bcheckWhile(n *a.While) error { } // Check the pre and inv conditions on the implicit continue after the // body. - if !terminates(n.Body()) { + if !a.Terminates(n.Body()) { for _, o := range n.Asserts() { if o.AsAssert().Keyword() == t.IDPost { continue