]> Cypherpunks repositories - gostls13.git/commitdiff
text/template/parse: fix handling of assignment/declaration in PipeNode.String
authorRob Pike <r@golang.org>
Thu, 13 Jun 2024 23:48:09 +0000 (09:48 +1000)
committerGopher Robot <gobot@golang.org>
Fri, 21 Jun 2024 18:12:29 +0000 (18:12 +0000)
The String method for Pipes assumed all variables were declared. Easy fix:
check the IsAssign bit.

Fixes #65382

Change-Id: I58f2760c1a8bb2821c3538645d893f58fd76ae73
Reviewed-on: https://go-review.googlesource.com/c/go/+/592695
Run-TryBot: Rob Pike <r@golang.org>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Rob Pike <r@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>

src/text/template/parse/node.go
src/text/template/parse/parse_test.go

index 23ba9aec2be8412d196ea74ebe117ca74129580e..a31309874d9da319961c80ded9edd6e21809ea59 100644 (file)
@@ -217,7 +217,11 @@ func (p *PipeNode) writeTo(sb *strings.Builder) {
                        }
                        v.writeTo(sb)
                }
-               sb.WriteString(" := ")
+               if p.IsAssign {
+                       sb.WriteString(" = ")
+               } else {
+                       sb.WriteString(" := ")
+               }
        }
        for i, c := range p.Cmds {
                if i > 0 {
index faf226d1c3cac26a7ceb4dd8b1d167d38c0d3a96..26aff330fe8a2716f0f08f8ef66ee48a63f85f3b 100644 (file)
@@ -306,6 +306,9 @@ var parseTests = []parseTest{
        {"bug1a", "{{$x:=.}}{{$x!2}}", hasError, ""},                     // ! is just illegal here.
        {"bug1b", "{{$x:=.}}{{$x+2}}", hasError, ""},                     // $x+2 should not parse as ($x) (+2).
        {"bug1c", "{{$x:=.}}{{$x +2}}", noError, "{{$x := .}}{{$x +2}}"}, // It's OK with a space.
+       // Check the range handles assignment vs. declaration properly.
+       {"bug2a", "{{range $x := 0}}{{$x}}{{end}}", noError, "{{range $x := 0}}{{$x}}{{end}}"},
+       {"bug2b", "{{range $x = 0}}{{$x}}{{end}}", noError, "{{range $x = 0}}{{$x}}{{end}}"},
        // dot following a literal value
        {"dot after integer", "{{1.E}}", hasError, ""},
        {"dot after float", "{{0.1.E}}", hasError, ""},