From: Ian Lance Taylor Date: Tue, 10 Apr 2018 14:08:45 +0000 (-0700) Subject: text/template: copy Decl field when copying PipeNode X-Git-Tag: go1.11beta1~905 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=d01322826e38fb42d4cf14188164fc46d90e25ae;p=gostls13.git text/template: copy Decl field when copying PipeNode Fixes #24791 Change-Id: I62ac17313e6e09796586911d88191a36d67f9aa1 Reviewed-on: https://go-review.googlesource.com/106115 Run-TryBot: Ian Lance Taylor Reviewed-by: Daniel Martí TryBot-Result: Gobot Gobot --- diff --git a/src/html/template/clone_test.go b/src/html/template/clone_test.go index b500715ac6..e292321d93 100644 --- a/src/html/template/clone_test.go +++ b/src/html/template/clone_test.go @@ -9,6 +9,7 @@ import ( "errors" "fmt" "io/ioutil" + "strings" "sync" "testing" "text/template/parse" @@ -262,3 +263,17 @@ func TestCloneRedefinedName(t *testing.T) { } } } + +// Issue 24791. +func TestClonePipe(t *testing.T) { + a := Must(New("a").Parse(`{{define "a"}}{{range $v := .A}}{{$v}}{{end}}{{end}}`)) + data := struct{ A []string }{A: []string{"hi"}} + b := Must(a.Clone()) + var buf strings.Builder + if err := b.Execute(&buf, &data); err != nil { + t.Fatal(err) + } + if got, want := buf.String(), "hi"; got != want { + t.Errorf("got %q want %q", got, want) + } +} diff --git a/src/text/template/parse/node.go b/src/text/template/parse/node.go index 737172dfdd..0bb96fc2e9 100644 --- a/src/text/template/parse/node.go +++ b/src/text/template/parse/node.go @@ -192,6 +192,7 @@ func (p *PipeNode) CopyPipe() *PipeNode { vars = append(vars, d.Copy().(*AssignNode)) } n := p.tr.newPipeline(p.Pos, p.Line, vars) + n.Decl = p.Decl for _, c := range p.Cmds { n.append(c.Copy().(*CommandNode)) }