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

cmd/compile: incosistent behaviour when assigning slices from a base slice with append #56999

Closed
sipwarriper opened this issue Nov 30, 2022 · 2 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge

Comments

@sipwarriper
Copy link

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

$ go version
go version go1.19.2 darwin/arm64

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="arm64"
GOBIN=""
GOCACHE="~/Library/Caches/go-build"
GOENV="~/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="~/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="~/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.19.2"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
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 -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/sq/szsnndwj6qz7ynp902xhg9rr0000gn/T/go-build4130374379=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I have a base slice with some elements. And I need 2 slices with the same base elements, and some different elements at the end of them, so I used append to add those elements to the base array 2 times, assigning each of them to a new different variable. Something like this:

a := append(base, elem1)
b := append(base, elem2)

The content of a and b after assigning b is the same in both slices. This changes depending on the number .

Here's an example reproducing the error (with a link to the playground with the same code).

package main

import "fmt"

func main() {
	lim := 3
	a := []int{}
	for i := 0; i < lim; i++ {
		a = append(a, i)
	}

	b := append(a, 20)
	fmt.Println(b)
	c := append(a, 30)
	fmt.Println(b)
	fmt.Println(c)
}

https://go.dev/play/p/5IeuvhEOM13

when changing lim value to 2, the behavior is different, a and b have the values I would expect:

What did you expect to see?

the same behavior when assigning the new variables, regardless of the number of elements in the base slice.

with lim:= 2

[0 1 20]
[0 1 20]
[0 1 30]

with lim:=3

[0 1 2 20]
[0 1 2 20]
[0 1 2 30]

What did you see instead?

with lim:= 2

[0 1 20]
[0 1 20]
[0 1 30]

with lim:=3

[0 1 2 20]
[0 1 2 30]
[0 1 2 30]

depending on the number of elements, the first variable I assign takes the value I appended first or second. This is confusing to me. Is this behaviour expected?

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Nov 30, 2022
@sipwarriper sipwarriper changed the title cmd/compile: incosistent behaviour when assigning slices from a base slice. cmd/compile: incosistent behaviour when assigning slices from a base slice with append Nov 30, 2022
@fzipp
Copy link
Contributor

fzipp commented Nov 30, 2022

This is normal slice behavior. They initially share the same backing array in memory and thus affect each other, but if the data exceeds the capacity, append allocates a new backing array with twice the capacity and copies the data. Now the slices have different backing arrays in different memory locations and do no longer affect each other.

You can see this by logging the memory addresses and capacities of the slices:

@ianlancetaylor
Copy link
Contributor

Please see https://go.dev/blog/slices.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Nov 30, 2022
@golang golang locked and limited conversation to collaborators Nov 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge
Projects
None yet
Development

No branches or pull requests

4 participants