cmd/compile: inconsistent typechecking with self-referential type parameter constraints #65714
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.
generics
Issue is related to generics
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
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 #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:The text was updated successfully, but these errors were encountered: