Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text/template: Unexpected assignment #56490

Closed
jmooring opened this issue Oct 30, 2022 · 3 comments
Closed

text/template: Unexpected assignment #56490

jmooring opened this issue Oct 30, 2022 · 3 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@jmooring
Copy link

jmooring commented Oct 30, 2022

What version of Go are you using (go version)?

go version go1.19.2 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/jmooring/.cache/go-build"
GOENV="/home/jmooring/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/jmooring/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/jmooring/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org"
GOROOT="/snap/go/current"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/snap/go/current/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.2"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/jmooring/code/go-sandbox/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3041838949=/tmp/go-build -gno-record-gcc-switches"

What did you do?

This works as expected (stdout is 3):

import (
	"os"
	"text/template"
)

func main() {

	a := [3]int{1, 2, 3}

	t := `
		{{- $x := 0 }}
		{{- $i := 0 }}
		{{- range $i = . }}
		{{- end }}
		{{- $i }}
	`

	tmpl, err := template.New("test").Parse(t)
	if err != nil {
		panic(err)
	}

	err = tmpl.Execute(os.Stdout, a)
	if err != nil {
		panic(err)
	}
}

Now, reverse the order of the first two lines of the template:

import (
	"os"
	"text/template"
)

func main() {

	a := [3]int{1, 2, 3}

	t := `
		{{- $i := 0 }}
		{{- $x := 0 }}
		{{- range $i = . }}
		{{- end }}
		{{- $i }}
	`

	tmpl, err := template.New("test").Parse(t)
	if err != nil {
		panic(err)
	}

	err = tmpl.Execute(os.Stdout, a)
	if err != nil {
		panic(err)
	}
}

Stdout is [1 2 3], but it should be 3.

Same issue with html/template.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/446795 mentions this issue: text/template: correct assignment, not declaration, in range

@seankhliao seankhliao added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 31, 2022
romaindoumenc pushed a commit to TroutSoftware/go that referenced this issue Nov 3, 2022
We were mishandling {{range $i = .}}, treating it as though it were
{{range $i := .}}. That happened to work if $i were the most recently
declared variable, but not otherwise.

Fixes golang#56490

Change-Id: I222a009d671d86c06a980a54388e05f12101c00b
Reviewed-on: https://go-review.googlesource.com/c/go/+/446795
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/503575 mentions this issue: text/template: set variables correctly in range assignment

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/503576 mentions this issue: [release-branch.go1.20] text/template: set variables correctly in range assignment

gopherbot pushed a commit that referenced this issue Jun 15, 2023
I unintentionally flipped them in CL 446795.

For #56490
Fixes #60801

Change-Id: I57586bec052e1b2cc61513870ce24dd6ce17e56b
Reviewed-on: https://go-review.googlesource.com/c/go/+/503575
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
@dmitshur dmitshur added this to the Go1.20 milestone Jun 16, 2023
gopherbot pushed a commit that referenced this issue Jun 21, 2023
…ge 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>
@golang golang locked and limited conversation to collaborators Jun 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants