1
0
mirror of https://github.com/golang/go synced 2024-09-24 03:20:12 -06:00

exp/template: fix action variable declarations inside range

R=r
CC=golang-dev
https://golang.org/cl/4807043
This commit is contained in:
Roger Peppe 2011-07-22 09:53:25 +10:00 committed by Rob Pike
parent e109a2bb8c
commit 47647b9865
2 changed files with 5 additions and 0 deletions

View File

@ -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:

View File

@ -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},