Skip to content

cmd/compile: incorrect result for recursion with mutated global variable #74438

Closed
@Yeaseen

Description

@Yeaseen

Go version

go version go1.24.1 linux/amd64

Output of go env in your module/workspace:

go env
AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/yeaseen/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/yeaseen/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1042317791=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/home/yeaseen/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/yeaseen/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/yeaseen/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.1'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

C code:

package main
import "fmt"

var count int = 10

func func1() int {
	if count <= 0 {
		return 0
	} else {
		count--
		return count + func1()
	}
}

func main() {
	res := func1()
	fmt.Println(res)
}

I ran this C code on Go playground using Go 1.24, Go 1.23, Go dev branch, locally go1.24.1 linux/amd64 and go1.23.1 linux/amd64

$ go run test.go
$ 0

What did you see happen?

Surprisingly, it prints 0 everytime

https://go.dev/play/p/729aTiEl-fC

I then run the same Go code using TinyGo (tinygo version 0.36.0 linux/amd64)
TinyGo prints 45

What did you expect to see?

It should print 45 (9+8+.....+1)

If it's a duplicate, I will close this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions