diff --git a/lang/ast/ast.go b/lang/ast/ast.go index ef3184b6..363c9a7d 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -257,8 +257,17 @@ type Loop interface { 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) + return true } func (s *LoopStack) Pop() Loop { diff --git a/lang/parse/parse.go b/lang/parse/parse.go index 8b98dc6f..ad1a2276 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -797,7 +797,10 @@ func (p *parser) parseStatement1() (*a.Node, error) { } 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) if err != nil { 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) // 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) if err != nil { return nil, err