Ban duplicate loop labels

This commit is contained in:
Nigel Tao committed 2020-04-03 10:33:05 +11:00
1 parent ee23f8b3d9
commit 4a89913a9b
2 files changed
+18 -3

No files matched your search

+10 -1
View File
@@ -257,8 +257,17 @@ type Loop interface {
type LoopStack []Loop type LoopStack []Loop
func (s *LoopStack) Push(l Loop) { // Push returns false if an existing Loop has the same non-zero label.
func (s *LoopStack) Push(l Loop) (ok bool) {
if label := l.Label(); label != 0 {
for _, o := range *s {
if label == o.Label() {
return false
}
}
}
*s = append(*s, l) *s = append(*s, l)
return true
} }
func (s *LoopStack) Pop() Loop { func (s *LoopStack) Pop() Loop {
+8 -2
View File
@@ -797,7 +797,10 @@ func (p *parser) parseStatement1() (*a.Node, error) {
} }
n := a.NewWhile(label, condition, asserts) n := a.NewWhile(label, condition, asserts)
p.loops.Push(n) if !p.loops.Push(n) {
return nil, fmt.Errorf(`parse: duplicate loop label %s at %s:%d`,
label.Str(p.tm), p.filename, p.line())
}
body, err := p.parseBlock(doubleCurly) body, err := p.parseBlock(doubleCurly)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -1114,7 +1117,10 @@ func (p *parser) parseIterateBlock(label t.ID, assigns []*a.Node) (*a.Iterate, e
} }
n := a.NewIterate(label, assigns, length, unroll, asserts) n := a.NewIterate(label, assigns, length, unroll, asserts)
// TODO: decide how break/continue work with iterate loops. // TODO: decide how break/continue work with iterate loops.
p.loops.Push(n) if !p.loops.Push(n) {
return nil, fmt.Errorf(`parse: duplicate loop label %s at %s:%d`,
label.Str(p.tm), p.filename, p.line())
}
body, err := p.parseBlock(false) body, err := p.parseBlock(false)
if err != nil { if err != nil {
return nil, err return nil, err