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: casting byte to an int8 with a constant value greater than 127 throws an error #27939

Closed
jackwilsdon opened this issue Sep 29, 2018 · 3 comments

Comments

@jackwilsdon
Copy link

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

go version go1.11 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/jack/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/jack/Development/Go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.11/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.11/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/67/bwpvphcn1kx4xjjzhvxtt6yw0000gn/T/go-build917588304=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Tried running the following code;

package main

import "fmt"

func main() {
	fmt.Printf("value: %d\n", int8(byte(200)))
}

What did you expect to see?

value: -56

What did you see instead?

./main.go:6:30: constant 200 overflows int8

The following code seems to work fine, so there's something odd going on when using a constant value in byte(...);

package main

import "fmt"

func main() {
	i := 200
	fmt.Printf("value: %d\n", int8(byte(i)))
}
@cznic
Copy link
Contributor

cznic commented Sep 29, 2018

It works correctly. This behavior is defined in the language specification.

@go101
Copy link

go101 commented Sep 30, 2018

yes, it is well defined. Overflows are allowed at run time, but not at compile time.O

@robpike
Copy link
Contributor

robpike commented Sep 30, 2018

@robpike robpike closed this as completed Sep 30, 2018
@golang golang locked and limited conversation to collaborators Sep 30, 2019
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

5 participants