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

Lambdas use the variable reference instead of value #58602

Closed
andrzejj0 opened this issue Feb 20, 2023 · 1 comment
Closed

Lambdas use the variable reference instead of value #58602

andrzejj0 opened this issue Feb 20, 2023 · 1 comment

Comments

@andrzejj0
Copy link

andrzejj0 commented Feb 20, 2023

It looks like the compiler might be overoptimising lambda execution by using the variable reference instead of its value. Please see the test case below.

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

$ go version
go version go1.20.1 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/ajarmoniuk/Library/Caches/go-build"
GOENV="/Users/ajarmoniuk/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/ajarmoniuk/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/ajarmoniuk/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.20.1"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/ajarmoniuk/go-workspace/test/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/rt/jtr_5cp57zxg095dss3jjnd40000gq/T/go-build3709323530=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

The following test fails:

func TestFunctional(t *testing.T) {
	var v func() int
	for d := 0; d < 1; d++ {
		v = func() int { return d }
	}
	if got, want := v(), 0; got != want {
		t.Errorf("Wrong answer! Got %d, want %d.", got, want)
	}
}

What did you expect to see?

Test does not fail

What did you see instead?

Wrong answer! Got 1, want 0.

By the way, it does work correctly if I use an intermediate variable inside the loop, like so:

func TestFunctional(t *testing.T) {
	var v func() int
	for d := 0; d < 1; d++ {
                intermediate := d
		v = func() int { return intermediate }
	}
	if got, want := v(), 0; got != want {
		t.Errorf("Wrong answer! Got %d, want %d.", got, want)
	}
}
@andrzejj0
Copy link
Author

andrzejj0 commented Feb 20, 2023

Or is my expectation wrong and the variable used in the closure must be finalized in its context?

@andrzejj0 andrzejj0 closed this as not planned Won't fix, can't repro, duplicate, stale Feb 20, 2023
@golang golang locked and limited conversation to collaborators Feb 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants