Stop bounds checking after a jump or return.

This commit is contained in:
Nigel Tao committed 2017-06-08 12:53:38 +10:00
1 parent 5cce3afe4c
commit f6cc7950ff
2 files changed
+16 -12

No files matched your search

+15 -9
View File
@@ -145,6 +145,18 @@ func bcheckField(idMap *t.IDMap, n *a.Field) error {
return nil return nil
} }
func (q *checker) bcheckBlock(block []*a.Node) error {
for _, o := range block {
if err := q.bcheckStatement(o); err != nil {
return err
}
if k := o.Kind(); k == a.KJump || k == a.KReturn {
break
}
}
return nil
}
func (q *checker) bcheckStatement(n *a.Node) error { func (q *checker) bcheckStatement(n *a.Node) error {
q.errFilename, q.errLine = n.Raw().FilenameLine() q.errFilename, q.errLine = n.Raw().FilenameLine()
@@ -385,11 +397,9 @@ func (q *checker) bcheckIf(n *a.If) error {
// Check the if-true branch, assuming the if condition. // Check the if-true branch, assuming the if condition.
q.facts.appendFact(n.Condition()) q.facts.appendFact(n.Condition())
for _, o := range n.BodyIfTrue() { if err := q.bcheckBlock(n.BodyIfTrue()); err != nil {
if err := q.bcheckStatement(o); err != nil {
return err return err
} }
}
if !terminates(n.BodyIfTrue()) { if !terminates(n.BodyIfTrue()) {
branches = append(branches, snapshot(q.facts)) branches = append(branches, snapshot(q.facts))
} }
@@ -402,11 +412,9 @@ func (q *checker) bcheckIf(n *a.If) error {
q.facts.appendFact(inverse) q.facts.appendFact(inverse)
} }
if bif := n.BodyIfFalse(); len(bif) > 0 { if bif := n.BodyIfFalse(); len(bif) > 0 {
for _, o := range bif { if err := q.bcheckBlock(bif); err != nil {
if err := q.bcheckStatement(o); err != nil {
return err return err
} }
}
if !terminates(bif) { if !terminates(bif) {
branches = append(branches, snapshot(q.facts)) branches = append(branches, snapshot(q.facts))
} }
@@ -494,11 +502,9 @@ func (q *checker) bcheckWhile(n *a.While) error {
q.facts.appendFact(n.Condition()) q.facts.appendFact(n.Condition())
} }
// Check the body. // Check the body.
for _, o := range n.Body() { if err := q.bcheckBlock(n.Body()); err != nil {
if err := q.bcheckStatement(o); err != nil {
return err return err
} }
}
// Check the pre and inv conditions on the implicit continue after the // Check the pre and inv conditions on the implicit continue after the
// body. // body.
if !terminates(n.Body()) { if !terminates(n.Body()) {
+1 -3
View File
@@ -333,8 +333,7 @@ func (c *Checker) checkFuncBody(node *a.Node) error {
// Run bounds checks. // Run bounds checks.
if c.flags&FlagsOnlyTypeCheck == 0 { if c.flags&FlagsOnlyTypeCheck == 0 {
for _, o := range n.Body() { if err := q.bcheckBlock(n.Body()); err != nil {
if err := q.bcheckStatement(o); err != nil {
return &Error{ return &Error{
Err: err, Err: err,
Filename: q.errFilename, Filename: q.errFilename,
@@ -344,7 +343,6 @@ func (c *Checker) checkFuncBody(node *a.Node) error {
} }
} }
} }
}
n.Node().SetTypeChecked() n.Node().SetTypeChecked()
if err := n.Node().Walk(func(o *a.Node) error { if err := n.Node().Walk(func(o *a.Node) error {