Skip to content

Commit 6470379

Browse files
new
Change-Id: I8ed2b28225874104f9e78f66d3f2f4018bac81af
1 parent fd4a8c2 commit 6470379

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/text/template/exec.go

+18-9
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,13 @@ func (s *state) walkRange(dot reflect.Value, r *parse.RangeNode) {
360360
val, _ := indirect(s.evalPipeline(dot, r.Pipe))
361361
// mark top of stack before any variables in the body are pushed.
362362
mark := s.mark()
363-
oneIteration := func(index, elem reflect.Value, rangefunc bool) {
363+
oneIteration := func(index, elem reflect.Value) {
364364
if len(r.Pipe.Decl) > 0 {
365-
if r.Pipe.IsAssign || rangefunc {
365+
if r.Pipe.IsAssign {
366366
// With two variables index comes first in all cases.
367367
// With one variable, we use the element for most cases,
368368
// but not for range over a function.
369-
if len(r.Pipe.Decl) > 1 || rangefunc {
369+
if len(r.Pipe.Decl) > 1 {
370370
s.setVar(r.Pipe.Decl[0].Ident[0], index)
371371
} else {
372372
s.setVar(r.Pipe.Decl[0].Ident[0], elem)
@@ -378,7 +378,7 @@ func (s *state) walkRange(dot reflect.Value, r *parse.RangeNode) {
378378
}
379379
}
380380
if len(r.Pipe.Decl) > 1 {
381-
if r.Pipe.IsAssign || rangefunc {
381+
if r.Pipe.IsAssign {
382382
s.setVar(r.Pipe.Decl[1].Ident[0], elem)
383383
} else {
384384
// Set next var (lexically the first if there
@@ -401,7 +401,7 @@ func (s *state) walkRange(dot reflect.Value, r *parse.RangeNode) {
401401
break
402402
}
403403
for i := 0; i < val.Len(); i++ {
404-
oneIteration(reflect.ValueOf(i), val.Index(i), false)
404+
oneIteration(reflect.ValueOf(i), val.Index(i))
405405
}
406406
return
407407
case reflect.Map:
@@ -410,7 +410,7 @@ func (s *state) walkRange(dot reflect.Value, r *parse.RangeNode) {
410410
}
411411
om := fmtsort.Sort(val)
412412
for _, m := range om {
413-
oneIteration(m.Key, m.Value, false)
413+
oneIteration(m.Key, m.Value)
414414
}
415415
return
416416
case reflect.Chan:
@@ -427,7 +427,7 @@ func (s *state) walkRange(dot reflect.Value, r *parse.RangeNode) {
427427
if !ok {
428428
break
429429
}
430-
oneIteration(reflect.ValueOf(i), elem, false)
430+
oneIteration(reflect.ValueOf(i), elem)
431431
}
432432
if i == 0 {
433433
break
@@ -444,7 +444,9 @@ func (s *state) walkRange(dot reflect.Value, r *parse.RangeNode) {
444444
run := false
445445
for v := range val.Seq() {
446446
run = true
447-
oneIteration(v, reflect.Value{}, true)
447+
// Pass element as second value,
448+
// as we do for channels.
449+
oneIteration(reflect.Value{}, v)
448450
}
449451
if !run {
450452
break
@@ -455,7 +457,14 @@ func (s *state) walkRange(dot reflect.Value, r *parse.RangeNode) {
455457
run := false
456458
for i, v := range val.Seq2() {
457459
run = true
458-
oneIteration(i, v, true)
460+
if len(r.Pipe.Decl) > 1 {
461+
oneIteration(i, v)
462+
} else {
463+
// If there is only one range variable,
464+
// oneIteration will use the
465+
// second value.
466+
oneIteration(reflect.Value{}, i)
467+
}
459468
}
460469
if !run {
461470
break

0 commit comments

Comments
 (0)