I was doing a bit of testing with regards to cycles through instantiated types. If we type check the below:
type a = b[int]
type b[_ any] = a
We get the below error message:
./prog.go:3:6: invalid recursive type a
./prog.go:3:6: a refers to b
./prog.go:4:6: b refers to a
./prog.go:3:10: cannot use generic type b[_ any] without instantiation
The first reported error seems reasonable, but the second seems like an erroneous follow-on error. From the user's perspective, the type has been instantiated (b[int]
) — it's a coincidence that a cycle error prevents said instantiation. Changing the definition of b
to the below solves both errors.
type a = b[int]
type b[_ any] = int
I think it's reasonable to remove the second error here.
Comment From: gabyhelp
Related Issues
- types2, go/types: missing errors for invalid parameterized type declarations #48962 (closed)
- go/types: improve recursive type error message #69574 (closed)
- go/types, cmd/compile: "invalid type loop" depending on declaration order #51244
- cmd/compile: "invalid recursive type" error is not as helpful as go/types #41575 (closed)
- gccgo: internal compiler error with simple recursive type declaration #25320 (closed)
- types2, go/types: need to detect invalid recursive type instantiation #48098 (closed)
- go/types: initialization cycle errors are non-deterministic #71254 (closed)
- cmd/compile: handle alias types in recursive types more consistently #50729 (closed)
- go/types: specious "invalid recursive type" error? #60130
Related Code Changes
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)