Skip to content

cmd/compile: declaration order of types affects cycle detection #68025

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

Open
richardwilkes opened this issue Jun 16, 2024 · 7 comments
Open

cmd/compile: declaration order of types affects cycle detection #68025

richardwilkes opened this issue Jun 16, 2024 · 7 comments
Assignees
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. early-in-cycle A change that should be done early in the 3 month dev cycle. NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@richardwilkes
Copy link
Contributor

Go version

go version go1.22.3 darwin/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/Users/rich/Library/Caches/go-build'
GOENV='/Users/rich/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/rich/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/rich/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/rich/go/pkg/mod/golang.org/[email protected]'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/rich/go/pkg/mod/golang.org/[email protected]/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='go1.22.3'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/rich/code/gcs/go.mod'
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/s9/9s3fk2mx5f936dkcp66ltwsc0000gn/T/go-build2527957728=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

I found that this will not compile: https://go.dev/play/p/X-_09aV9wYj
While just moving the declaration of the interface to after the struct definitions causes it to compile fine: https://go.dev/play/p/xic2fqhfmTi

What did you see happen?

./prog.go:3:6: invalid recursive type A
	./prog.go:3:6: A refers to
	./prog.go:7:6: B refers to
	./prog.go:11:6: C refers to
	./prog.go:3:6: A

Go build failed.

What did you expect to see?

I expected both versions of the code to compile and run without error.

@seankhliao seankhliao changed the title Order of generic type constraint placement determines whether code compiles or not cmd/compile: declaration order of types affects cycle detection Jun 16, 2024
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jun 16, 2024
@richardwilkes
Copy link
Contributor Author

I should note that while rearranging the code fixes the problem, if the definitions are in different files (which happens to be the case for my real code, since a bunch of different types are used in the interface definition, i.e. something more like:

type A interface {
    *B | *D | *E | *F | *G | *H
}

then it sometimes fails and sometimes doesn't. I've yet to figure out exactly what is determining the order in that case.

@ianlancetaylor
Copy link
Contributor

CC @griesemer

@griesemer
Copy link
Contributor

Thanks for the report. Our cycle detection algorithm could use some love.
cc: @timothy-king

@griesemer
Copy link
Contributor

Didn't get to this. For 1.25.

@griesemer griesemer added the early-in-cycle A change that should be done early in the 3 month dev cycle. label Nov 20, 2024
@griesemer griesemer modified the milestones: Go1.24, Go1.25 Nov 20, 2024
@timothy-king timothy-king removed their assignment Jan 21, 2025
@gopherbot
Copy link
Contributor

This issue is currently labeled as early-in-cycle for Go 1.25.
That time is now, so a friendly reminder to look at it again.

@mrkfrmn
Copy link
Contributor

mrkfrmn commented Apr 18, 2025

Came across this while looking at some other issues — here is another playground that also reproduces.

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. early-in-cycle A change that should be done early in the 3 month dev cycle. NeedsFix The path to resolution is known, but the work has not been done.
Projects
Development

No branches or pull requests

7 participants