Commit 47647b98 authored by Roger Peppe's avatar Roger Peppe Committed by Rob Pike

exp/template: fix action variable declarations inside range

R=r
CC=golang-dev
https://golang.org/cl/4807043
parent e109a2bb
......@@ -172,6 +172,8 @@ func isTrue(val reflect.Value) (truth, ok bool) {
func (s *state) walkRange(dot reflect.Value, r *rangeNode) {
defer s.pop(s.mark())
val, _ := indirect(s.evalPipeline(dot, r.pipe))
// mark top of stack before any variables in the body are pushed.
mark := s.mark()
switch val.Kind() {
case reflect.Array, reflect.Slice:
if val.Len() == 0 {
......@@ -188,6 +190,7 @@ func (s *state) walkRange(dot reflect.Value, r *rangeNode) {
s.setVar(2, reflect.ValueOf(i))
}
s.walk(elem, r.list)
s.pop(mark)
}
return
case reflect.Map:
......@@ -205,6 +208,7 @@ func (s *state) walkRange(dot reflect.Value, r *rangeNode) {
s.setVar(2, key)
}
s.walk(elem, r.list)
s.pop(mark)
}
return
default:
......
......@@ -331,6 +331,7 @@ var execTests = []execTest{
{"range $x MSIone", "{{range $x := .MSIone}}<{{$x}}>{{end}}", "<1>", tVal, true},
{"range $x $y MSIone", "{{range $x, $y := .MSIone}}<{{$x}}={{$y}}>{{end}}", "<one=1>", tVal, true},
{"range $x PSI", "{{range $x := .PSI}}<{{$x}}>{{end}}", "<21><22><23>", tVal, true},
{"declare in range", "{{range $x := .PSI}}<{{$foo:=$x}}>{{end}}", "<21><22><23>", tVal, true},
// Cute examples.
{"or as if true", `{{or .SI "slice is empty"}}`, "[3 4 5]", tVal, true},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment