Ban unlabeled break/continue for labeled loop

This commit is contained in:
Nigel Tao committed 2020-02-24 14:20:05 +11:00
1 parent c5b3a9e08e
commit 2d2a880c18
4 files changed
+10 -3

No files matched your search

+3
View File
@@ -240,6 +240,7 @@ type Loop interface {
AsNode() *Node
HasBreak() bool
HasContinue() bool
Keyword() t.ID
Label() t.ID
Asserts() []*Node
Body() []*Node
@@ -513,6 +514,7 @@ type Iterate Node
func (n *Iterate) AsNode() *Node { return (*Node)(n) }
func (n *Iterate) HasBreak() bool { return n.flags&FlagsHasBreak != 0 }
func (n *Iterate) HasContinue() bool { return n.flags&FlagsHasContinue != 0 }
func (n *Iterate) Keyword() t.ID { return t.IDIterate }
func (n *Iterate) Unroll() t.ID { return n.id0 }
func (n *Iterate) Label() t.ID { return n.id1 }
func (n *Iterate) Length() t.ID { return n.id2 }
@@ -551,6 +553,7 @@ type While Node
func (n *While) AsNode() *Node { return (*Node)(n) }
func (n *While) HasBreak() bool { return n.flags&FlagsHasBreak != 0 }
func (n *While) HasContinue() bool { return n.flags&FlagsHasContinue != 0 }
func (n *While) Keyword() t.ID { return t.IDWhile }
func (n *While) Label() t.ID { return n.id1 }
func (n *While) Condition() *Expr { return n.mhs.AsExpr() }
func (n *While) Asserts() []*Node { return n.list1 }
+5 -1
View File
@@ -151,13 +151,17 @@ func (q *checker) tcheckStatement(n *a.Node) error {
}
} else if nj := len(q.jumpTargets); nj > 0 {
jumpTarget = q.jumpTargets[nj-1]
if jumpTarget.Label() != 0 {
return fmt.Errorf("check: unlabeled %s for labeled %s.%s",
n.Keyword().Str(q.tm), jumpTarget.Keyword().Str(q.tm), jumpTarget.Label().Str(q.tm))
}
}
if jumpTarget == nil {
sepStr, labelStr := "", ""
if id := n.Label(); id != 0 {
sepStr, labelStr = ":", id.Str(q.tm)
}
return fmt.Errorf("no matching while/iterate statement for %s%s%s",
return fmt.Errorf("check: no matching while/iterate statement for %s%s%s",
n.Keyword().Str(q.tm), sepStr, labelStr)
}
if n.Keyword() == t.IDBreak {