Require if and while conditions to be effect-free

This commit is contained in:
Nigel Tao committed 2018-07-29 14:20:15 +10:00
1 parent 5cca9471ec
commit df19bcdb03
4 files changed
+16 -28

No files matched your search

-4
View File
@@ -525,8 +525,6 @@ func (q *checker) bcheckIf(n *a.If) error {
for n != nil {
snap := snapshot(q.facts)
// Check the if condition.
//
// TODO: check that n.Condition() has no side effects.
if _, err := q.bcheckExpr(n.Condition(), 0); err != nil {
return err
}
@@ -581,8 +579,6 @@ func (q *checker) bcheckWhile(n *a.While) error {
}
// Check the while condition.
//
// TODO: check that n.Condition() has no side effects.
if _, err := q.bcheckExpr(n.Condition(), 0); err != nil {
return err
}
+6
View File
@@ -94,6 +94,9 @@ func (q *checker) tcheckStatement(n *a.Node) error {
case a.KIf:
for n := n.AsIf(); n != nil; n = n.ElseIf() {
cond := n.Condition()
if cond.Effect() != 0 {
return fmt.Errorf("check: internal error: if-condition is not effect-free")
}
if err := q.tcheckExpr(cond, 0); err != nil {
return err
}
@@ -224,6 +227,9 @@ func (q *checker) tcheckStatement(n *a.Node) error {
case a.KWhile:
n := n.AsWhile()
cond := n.Condition()
if cond.Effect() != 0 {
return fmt.Errorf("check: internal error: while-condition is not effect-free")
}
if err := q.tcheckExpr(cond, 0); err != nil {
return err
}
+8
View File
@@ -656,6 +656,10 @@ func (p *parser) parseStatement1() (*a.Node, error) {
if err != nil {
return nil, err
}
if condition.Effect() != 0 {
return nil, fmt.Errorf(`parse: while-condition %q is not effect-free at %s:%d`,
condition.Str(p.tm), p.filename, p.line())
}
asserts, err := p.parseAsserts()
if err != nil {
return nil, err
@@ -709,6 +713,10 @@ func (p *parser) parseIf() (*a.If, error) {
if err != nil {
return nil, err
}
if condition.Effect() != 0 {
return nil, fmt.Errorf(`parse: if-condition %q is not effect-free at %s:%d`,
condition.Str(p.tm), p.filename, p.line())
}
bodyIfTrue, err := p.parseBlock()
if err != nil {
return nil, err