mirror of
https://github.com/golang/go
synced 2024-11-21 19:24:45 -07:00
exp/template: fix action variable declarations inside range
R=r CC=golang-dev https://golang.org/cl/4807043
This commit is contained in:
parent
e109a2bb8c
commit
47647b9865
@ -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},
|
||||
|
Loading…
Reference in New Issue
Block a user