diff options
author | Ian Lance Taylor <iant@golang.org> | 2023-06-14 16:17:31 -0700 |
---|---|---|
committer | Ian Lance Taylor <iant@google.com> | 2023-06-21 16:11:26 +0000 |
commit | 03063101a2b40e78a44bdc1da84900d41a49a3ec (patch) | |
tree | db58c57895667978244f699f7f23d0b2d9e1d090 | |
parent | d51e322a3f98e8ceddfd203afcbf0144121d5cc1 (diff) | |
download | go-03063101a2b40e78a44bdc1da84900d41a49a3ec.tar.gz go-03063101a2b40e78a44bdc1da84900d41a49a3ec.zip |
[release-branch.go1.20] text/template: set variables correctly in range assignment
I unintentionally flipped them in CL 446795.
For #56490
For #60801
Fixes #60802
Change-Id: I57586bec052e1b2cc61513870ce24dd6ce17e56b
Reviewed-on: https://go-review.googlesource.com/c/go/+/503576
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
-rw-r--r-- | src/text/template/exec.go | 16 | ||||
-rw-r--r-- | src/text/template/exec_test.go | 1 |
2 files changed, 13 insertions, 4 deletions
diff --git a/src/text/template/exec.go b/src/text/template/exec.go index fb60c17931..fd7db657d3 100644 --- a/src/text/template/exec.go +++ b/src/text/template/exec.go @@ -361,19 +361,27 @@ func (s *state) walkRange(dot reflect.Value, r *parse.RangeNode) { // mark top of stack before any variables in the body are pushed. mark := s.mark() oneIteration := func(index, elem reflect.Value) { - // Set top var (lexically the second if there are two) to the element. if len(r.Pipe.Decl) > 0 { if r.Pipe.IsAssign { - s.setVar(r.Pipe.Decl[0].Ident[0], elem) + // With two variables, index comes first. + // With one, we use the element. + if len(r.Pipe.Decl) > 1 { + s.setVar(r.Pipe.Decl[0].Ident[0], index) + } else { + s.setVar(r.Pipe.Decl[0].Ident[0], elem) + } } else { + // Set top var (lexically the second if there + // are two) to the element. s.setTopVar(1, elem) } } - // Set next var (lexically the first if there are two) to the index. if len(r.Pipe.Decl) > 1 { if r.Pipe.IsAssign { - s.setVar(r.Pipe.Decl[1].Ident[0], index) + s.setVar(r.Pipe.Decl[1].Ident[0], elem) } else { + // Set next var (lexically the first if there + // are two) to the index. s.setTopVar(2, index) } } diff --git a/src/text/template/exec_test.go b/src/text/template/exec_test.go index 6b163f0ae1..a7c010d683 100644 --- a/src/text/template/exec_test.go +++ b/src/text/template/exec_test.go @@ -694,6 +694,7 @@ var execTests = []execTest{ {"bug18c", "{{eq . 'P'}}", "true", 'P', true}, {"issue56490", "{{$i := 0}}{{$x := 0}}{{range $i = .AI}}{{end}}{{$i}}", "5", tVal, true}, + {"issue60801", "{{$k := 0}}{{$v := 0}}{{range $k, $v = .AI}}{{$k}}={{$v}} {{end}}", "0=3 1=4 2=5 ", tVal, true}, } func zeroArgs() string { |