Improve error message for unreachable code

This commit is contained in:
Nigel Tao committed 2020-04-03 10:58:04 +11:00
1 parent e5c86ec191
commit b791b441ef
1 file changed
+23 -21
+23 -21
View File
@@ -196,40 +196,42 @@ func invert(tm *t.Map, n *a.Expr) (*a.Expr, error) {
} }
func (q *checker) bcheckBlock(block []*a.Node) error { func (q *checker) bcheckBlock(block []*a.Node) error {
// TODO: after a "break loop", do we need to set placeholder MBounds so unreachable := false
// that everything (including unreachable code) is bounds checked?
loop:
for _, o := range block { for _, o := range block {
q.errFilename, q.errLine = o.AsRaw().FilenameLine()
if unreachable {
return fmt.Errorf("check: unreachable code")
}
if err := q.bcheckStatement(o); err != nil { if err := q.bcheckStatement(o); err != nil {
return err return err
} }
switch o.Kind() { switch o.Kind() {
default:
continue
case a.KJump: case a.KJump:
break loop // No-op.
case a.KRet: case a.KRet:
if o.AsRet().Keyword() == t.IDReturn { if o.AsRet().Keyword() == t.IDYield {
break loop // Drop any facts involving args or this.
} if err := q.facts.update(func(x *a.Expr) (*a.Expr, error) {
// o is a yield statement. if x.Mentions(exprArgs) || x.Mentions(exprThis) {
// return nil, nil
// Drop any facts involving args or this. }
if err := q.facts.update(func(x *a.Expr) (*a.Expr, error) { return x, nil
if x.Mentions(exprArgs) || x.Mentions(exprThis) { }); err != nil {
return nil, nil return err
} }
return x, nil // TODO: drop any facts involving ptr-typed local variables?
}); err != nil { continue
return err
} }
// TODO: drop any facts involving ptr-typed local variables?
} }
unreachable = true
} }
return nil return nil
} }
func (q *checker) bcheckStatement(n *a.Node) error { func (q *checker) bcheckStatement(n *a.Node) error {
q.errFilename, q.errLine = n.AsRaw().FilenameLine()
switch n.Kind() { switch n.Kind() {
case a.KAssert: case a.KAssert:
if err := q.bcheckAssert(n.AsAssert()); err != nil { if err := q.bcheckAssert(n.AsAssert()); err != nil {
@@ -386,7 +388,7 @@ func (q *checker) bcheckAssert(n *a.Assert) error {
if reasonFunc := q.reasonMap[reasonID]; reasonFunc != nil { if reasonFunc := q.reasonMap[reasonID]; reasonFunc != nil {
err = reasonFunc(q, n) err = reasonFunc(q, n)
} else { } else {
err = fmt.Errorf("no such reason %s", reasonID.Str(q.tm)) err = fmt.Errorf("check: no such reason %s", reasonID.Str(q.tm))
} }
} else if condition.Operator().IsBinaryOp() && condition.Operator() != t.IDAs { } else if condition.Operator().IsBinaryOp() && condition.Operator() != t.IDAs {
err = q.proveBinaryOp(condition.Operator(), err = q.proveBinaryOp(condition.Operator(),
@@ -546,7 +548,7 @@ func (q *checker) unify(branches [][]*a.Expr) error {
return nil return nil
} }
if len(branches) > 10000 { if len(branches) > 10000 {
return fmt.Errorf("too many if-else branches") return fmt.Errorf("check: too many if-else branches")
} }
m := map[string]int{} m := map[string]int{}