Skip to content

runtime: empty struct type pointer comparison varies #73201

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

Closed
jsageryd opened this issue Apr 7, 2025 · 3 comments
Closed

runtime: empty struct type pointer comparison varies #73201

jsageryd opened this issue Apr 7, 2025 · 3 comments
Labels
BugReport Issues describing a possible bug in the Go implementation. compiler/runtime Issues related to the Go compiler and/or runtime.

Comments

@jsageryd
Copy link
Contributor

jsageryd commented Apr 7, 2025

Go version

go version go1.24.2 darwin/arm64

Output of go env in your module/workspace:

AR='ar'
CC='cc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='c++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/j/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/j/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/0j/7fvjv4jn4qq5_44fpjn9cjzr0000gn/T/go-build138579852=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/Users/j/go/pkg/mod'
GOOS='darwin'
GOPATH='/Users/j/go'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.24.2/libexec'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/j/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.24.2/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.24.2'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

Two instances of pointers to an empty struct compare differently based on, as far as I can tell, whether they have been read before.

package main

import (
	"fmt"
)

type Foo struct{}

func main() {
	var a, b Foo

	pA := &a
	pB := &b

	// fmt.Printf("Pointer values: %p %p\n", pA, pB)

	fmt.Printf("pA == pB? %t\n", pA == pB)
}

https://go.dev/play/p/dVDP0x-H5WH

vs.

package main

import (
	"fmt"
)

type Foo struct{}

func main() {
	var a, b Foo

	pA := &a
	pB := &b

	fmt.Printf("Pointer values: %p %p\n", pA, pB)

	fmt.Printf("pA == pB? %t\n", pA == pB)
}

https://go.dev/play/p/T9ZMpBqgad8

What did you see happen?

In the first example the struct pointers compare false. In the second example they compare true.

What did you expect to see?

I would have expected the comparison to have the same result regardless of the print statement.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Apr 7, 2025
@jsageryd
Copy link
Contributor Author

jsageryd commented Apr 7, 2025

The spec says:

Pointers to distinct zero-size variables may or may not be equal.

So I suppose this could be dismissed as not a bug and working according to spec, but it is still surprising to me that the result of the comparison changes.

@jsageryd jsageryd changed the title runtime: empty struct type comparison varies runtime: empty struct type pointer comparison varies Apr 7, 2025
@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label Apr 7, 2025
@jsageryd
Copy link
Contributor Author

jsageryd commented Apr 7, 2025

Thank you bot; the responses in the linked issues answer my question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation. compiler/runtime Issues related to the Go compiler and/or runtime.
Projects
None yet
Development

No branches or pull requests

3 participants