Skip to content

Commit

Permalink
Test that `next' is only used inside a decorator.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Wilkinson committed Aug 27, 2019
1 parent 6808de1 commit 728fa84
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion internal/vm/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,18 @@ func (c *checker) VisitAfter(node ast.Node) ast.Node {
return n

case *ast.NextStmt:
// The last element in this list will be the empty stack created by the DecoDecl on the way in.
// The last element in this list will be the empty stack created by the
// DecoDecl on the way in. If there's no last element, then we can't
// have entered a DecoDecl yet.
last := len(c.decoScopes) - 1
if last < 0 {
c.errors.Add(n.Pos(), fmt.Sprintf("Can't use `next' outside of a decorator."))
return n
}
decoScope := c.decoScopes[last]
if len(decoScope.Symbols) > 0 {
c.errors.Add(n.Pos(), fmt.Sprintf("Can't use `next' statement twice in a decorator."))
return n
}
// Merge the current scope into it.
decoScope.CopyFrom(c.scope)
Expand Down
10 changes: 10 additions & 0 deletions internal/vm/checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ const ID /bar/
foo = $1
}`,
[]string{"counter with buckets:1:9-11: Can't specify buckets for non-histogram metric `foo'."}},

{"next outside of decorator",
`def x{
next
}
@x {
next
}
`,
[]string{"next outside of decorator:5:1-4: Can't use `next' outside of a decorator."}},
}

func TestCheckInvalidPrograms(t *testing.T) {
Expand Down

0 comments on commit 728fa84

Please sign in to comment.