Open dawidl022 opened 9 months ago
@golang/compiler
In triage, we're wondering if @griesemer or @mdempsky can take a look? Thanks.
@mknyszek I will take this on.
This is clearly a bug in the implementation. Cycles are poorly defined in the spec. Not sure we can fix this for 1.23, but probably not a release blocker because re-arranging things is possible, and this bug has been around (likely) since 1.18.
Too late for 1.23. Moving to 1.24.
This issue is currently labeled as early-in-cycle for Go 1.24. That time is now, so a friendly reminder to look at it again.
Too late for 1.24. Moving to 1.25.
This issue is similar to #65711. However, the behaviour of the compiler is different, so I am opening a separate issue.
What did you do?
While working on formal methods (in the style of Featherweight Go) and a prototype type checker and interpreter for https://github.com/golang/go/issues/65555, I came across a pattern of programs that either pass or fail type checking, depending on the order of the type declarations.
E.g. the following program passes the type checker (and the program successfully compiles), and it is possible to implement and use the
Bar
interface (Go playground):(note: this program looks silly because it is part of my interpreter's testing suite, explicitly meant to test various edge cases in the type checker)
However, if we swap the order of the type declaration, the compiler rejects the program (playground):
Output of
go build
for the latter example:As far as I'm aware, the order of type declarations should not matter in a Go program.
From the Go spec, it is not clear to me, which is the correct behaviour. The spec says:
and provides an example:
Foo
neither refers to itself directly via its own type parameter list (i.e. theBar
interface is not inlined in the type parameter list), nor is it referred through "the type parameter list of another generic type" (Foo
is the only generic type in the program).The issue is not exclusive to interface type declarations. The same behaviour can be observed through pairs of program using struct and array types.
Struct example
Compiles (playground):
Does not compile (playground):
Related issues
65711 is also affected by the ordering of the type declarations, which suggests that the two issues are related.
What did you see happen?
Type checker accepting or rejecting programs depending on the order of type declarations.
What did you expect to see?
Type checker consistently accepting or rejecting programs, regardless of the ordering of type declarations.
Go version
go version go1.22.0 darwin/arm64
Output of
go env
in your module/workspace: