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: when cast to bool, use 1 for true and 0 for false. #43766

Closed
Neightly opened this issue Jan 19, 2021 · 2 comments
Closed

cmd/compile: when cast to bool, use 1 for true and 0 for false. #43766

Neightly opened this issue Jan 19, 2021 · 2 comments

Comments

@Neightly
Copy link

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

$ go version
go version go1.15.4 windows/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
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=D:\DEVPATH\GOBIN
set GOCACHE=C:\Users\Administrator\AppData\Local\go-build
set GOENV=C:\Users\Administrator\AppData\Roaming\go\env  
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\DEVPATH\GOMOD
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\DEVPATH\GOPATH
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=E:\DEVELOPMENT\GO
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=E:\DEVELOPMENT\GO\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=D:\DEVPATH\GOPATH\src\playground\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fmessage-length=0 -fdebug-prefix-map=C:\Users\ADMINI~1\AppData\Local\Temp\go-build673745918=/tmp/go-build -gno-record-gcc-switches

What did you do?

func main() {
	var j1 int8 = 1
	var b1 bool = *(*bool)(unsafe.Pointer(&j1))

	var j2 int8 = 2
	var b2 bool = *(*bool)(unsafe.Pointer(&j2))

	println(b1 == b2)
}

What did you expect to see?

true

What did you see instead?

false

@davecheney
Copy link
Contributor

@Neightly thank your for raising this issue.

There are a few things to clarify.

  1. Go does not have casts -- people may occasionally say "cast" when they are speaking informally but there is no notion of casting in Go.
  2. Your example is a conversion, making a copy of its value and giving it a new type.
  3. Your example uses the unsafe package, when you use that package the protections of the language are disabled

As such, you've created a bool value with a bit pattern of 0b00000010, that doesn't match the other bit pattern, 0b00000001 so the comparison is false.

Without using unsafe the compiler is free to assume that all true booleans have one representation and all false booleans have another. The spec doesn't define what they are so you cannot reliably create a bool using unsafe.Pointer conversions. If you want to create a boolean, this is the correct approach

var j2 int8 = 2
var b2 bool = j2 > 0

@Neightly
Copy link
Author

I read the .s file.
b1==b2 will be translated to CMPB but b1==true will be translated to TESTB.

@golang golang locked and limited conversation to collaborators Jan 19, 2022
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

3 participants