[go/release-branch.go1.25] [release-branch.go1.25] cmd/compile: fix loopvar version detection with line directives

0 views
Skip to first unread message

Michael Pratt (Gerrit)

unread,
Apr 29, 2026, 4:06:32 PM (21 hours ago) Apr 29
to Michael Pratt, Xie Yuchen, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, David Chase, golang...@luci-project-accounts.iam.gserviceaccount.com, Keith Randall, Gopher Robot, golang-co...@googlegroups.com

Michael Pratt submitted the change

Change information

Commit message:
[release-branch.go1.25] cmd/compile: fix loopvar version detection with line directives

The Go loop variable semantics changed in Go 1.22: loop variables are now
created per-iteration instead of per-loop. The compiler decides which
semantics to use based on the Go version in go.mod.

When go.mod specifies go 1.21 and the code is built with a Go 1.22+
compiler, the per-loop(compatible behavior) semantics should be used.

However, when a line directive is present in the source file,
go.mod 1.21 and go1.22+ compiler outputs a per-iteration semantics.

For example, the file below wants output 333 but got 012.

-- go.mod --
module test
go 1.21
-- main.go --
//line main.go:1
func main() {
var fns []func()
for i := 0; i < 3; i++ {
fns = append(fns, func() { fmt.Print(i) })
}
for _, fn := range fns {
fn()
}
}

The distinctVars function uses stmt.Pos().Base() to look up the file
version in FileVersions. Base() returns the file name after line
directives are applied (e.g., "main.go" for "//line main.go:1"), not
the actual source file path. This causes the version lookup to fail
for files with line directives.

This CL fixes the bug by using stmt.Pos().FileBase() instead. FileBase()
returns the actual file path before line directives are applied, ensuring
the correct version information is retrieved from the original source file.

Fixes: #77298

Change-Id: Idacc0816d112ee393089262468a02acfe40e4b72
Reviewed-on: https://go-review.googlesource.com/c/go/+/737820
Reviewed-by: Keith Randall <k...@golang.org>
LUCI-TryBot-Result: Go LUCI <golang...@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <k...@google.com>
Reviewed-by: Carlos Amedee <car...@golang.org>
(cherry picked from commit b408256be720c51ff1730d7a97389a975744a312)
Reviewed-on: https://go-review.googlesource.com/c/go/+/739020
Reviewed-by: Michael Pratt <mpr...@google.com>
LUCI-TryBot-Result: golang...@luci-project-accounts.iam.gserviceaccount.com <golang...@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drc...@google.com>
Files:
  • M src/cmd/compile/internal/loopvar/loopvar_test.go
  • A src/cmd/compile/internal/loopvar/testdata/range_esc_closure_linedir.go
  • M src/cmd/compile/internal/noder/writer.go
Change size: M
Delta: 3 files changed, 85 insertions(+), 1 deletion(-)
Branch: refs/heads/release-branch.go1.25
Submit Requirements:
Open in Gerrit
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: merged
Gerrit-Project: go
Gerrit-Branch: release-branch.go1.25
Gerrit-Change-Id: Idacc0816d112ee393089262468a02acfe40e4b72
Gerrit-Change-Number: 739020
Gerrit-PatchSet: 3
Gerrit-Owner: Xie Yuchen <xieyu...@gmail.com>
Gerrit-Reviewer: David Chase <drc...@google.com>
Gerrit-Reviewer: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
open
diffy
satisfied_requirement

Michael Pratt (Gerrit)

unread,
Apr 29, 2026, 4:06:34 PM (21 hours ago) Apr 29
to Michael Pratt, Xie Yuchen, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, David Chase, golang...@luci-project-accounts.iam.gserviceaccount.com, Keith Randall, golang-co...@googlegroups.com

Michael Pratt submitted the change

Change information

Commit message:
[release-branch.go1.26] cmd/compile: fix loopvar version detection with line directives
For #77248
Fixes #77297


Change-Id: Idacc0816d112ee393089262468a02acfe40e4b72
Reviewed-on: https://go-review.googlesource.com/c/go/+/737820
Reviewed-by: Keith Randall <k...@golang.org>
LUCI-TryBot-Result: Go LUCI <golang...@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <k...@google.com>
Reviewed-by: Carlos Amedee <car...@golang.org>
(cherry picked from commit b408256be720c51ff1730d7a97389a975744a312)
Files:
  • M src/cmd/compile/internal/loopvar/loopvar_test.go
  • A src/cmd/compile/internal/loopvar/testdata/range_esc_closure_linedir.go
  • M src/cmd/compile/internal/noder/writer.go
Change size: M
Delta: 3 files changed, 85 insertions(+), 1 deletion(-)
Branch: refs/heads/release-branch.go1.26
Submit Requirements:
    • requirement satisfiedCode-Review: +2 by David Chase
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: merged
    Gerrit-Project: go
    Gerrit-Branch: release-branch.go1.26
    Gerrit-Change-Id: Idacc0816d112ee393089262468a02acfe40e4b72
    Gerrit-Change-Number: 772104
    Gerrit-PatchSet: 2
    Gerrit-Owner: Michael Pratt <mpr...@google.com>
    Gerrit-Reviewer: David Chase <drc...@google.com>
    Gerrit-Reviewer: Keith Randall <k...@golang.org>
    Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
    Gerrit-CC: Xie Yuchen <xieyu...@gmail.com>
    open
    diffy
    satisfied_requirement
    Reply all
    Reply to author
    Forward
    0 new messages