Skip to content

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

Closed

Description

@Neightly

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

Activity

  1. davecheney commented on Jan 19, 2021

    @davecheney
    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
    
  2. Neightly commented on Jan 19, 2021

    @Neightly
    Author

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

  3. locked and limited conversation to collaborators on Jan 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions