-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/compile: inconsistent typechecking with self-referential type parameter constraints #65714
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
Comments
@golang/compiler |
In triage, we're wondering if @griesemer or @mdempsky can take a look? Thanks. |
@mknyszek I will take this on. |
#65711 has been fixed and is unrelated to this issue. |
This is clearly a bug in the implementation. Cycles are poorly defined in the spec. |
Too late for 1.23. Moving to 1.24. |
This issue is currently labeled as early-in-cycle for Go 1.24. |
Too late for 1.24. Moving to 1.25. |
This issue is currently labeled as early-in-cycle for Go 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 #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: