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: calling min/max with float literals and an int var causes an assertion failure #61002

Closed
cursork opened this issue Jun 26, 2023 · 1 comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.

Comments

@cursork
Copy link

cursork commented Jun 26, 2023

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

$ go version
go version go1.21rc2 darwin/amd64

Does this issue reproduce with the latest release?

1.21 feature, so only on release candidate.

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

go env Output
$ go env

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/Users/nk/Library/Caches/go-build'
GOENV='/Users/nk/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/nk/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/nk/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/nk/sdk/go1.21rc2'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/nk/sdk/go1.21rc2/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='go1.21rc2'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/f4/r8t1f_3n01q47zbmm7__f14r0000gn/T/go-build4228260291=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

When assigning to an int using min/max and number literals alone, it's possible to determine that max(7.1, 10) is always assignable to int at compile time, and that max(5, 7.1) is not. This is what the compiler indeed seems to do.

So I experimented mixing that in various combinations with an int var, as below:

func main() {
	var someNum int = 50
	// Normal mixing of number literals
	// var m int = max(7.1, 10) // Expected: 10
	// var m int = max(5, 7.1) // Expected error: cannot use max(5, 7.1) (untyped float constant 7.1) as int value in variable declaration (truncated)
	// Expected errors:
	// var m int = min(7.1, 10, someNum) // 7.1 (untyped float constant) truncated to int
	// var m int = max(5, 7.1, someNum) // 7.1 (untyped float constant) truncated to int
	// Compile assertion failures:
	// var m int = min(5, 7.1, someNum) // internal compiler error: assertion failed
	var m int = max(7.1, 10, someNum) // internal compiler error: assertion failed
	fmt.Printf("%v %T\n", m, m)
}

( https://go.dev/play/p/4iSQEEboZFq?v=gotip )

What did you expect to see?

The compiler recognising that e.g. min(5, 7.1, someNum) is equivalent to min(5, someNum) and successfully compiling or some form of truncated to int error because this is not code you'd expect to actually see.

What did you see instead?

The internal compiler error.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jun 26, 2023
@cuonglm
Copy link
Member

cuonglm commented Jun 26, 2023

Duplicated of #60991

@cuonglm cuonglm closed this as completed Jun 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.
Projects
None yet
Development

No branches or pull requests

3 participants