Currently go2go complains with confusing errors; the first declaration fails with Rec is not a generic type (it obviously is) and the equivalent type Rec[type T Rec] interface{ r(T) } fails with Rec is not an interface (it also obviously is).
I assume this shouldn't be allowed: to know if Rec[T] is a valid type for T to implement, we need to know if T implements Rec[T], which is circular. But I can't find anything in the draft design that would prohibit it.
We currently report a cycle error here which seems reasonable, but we should review if such code should actually be permitted.
griesemer
changed the title
cmd/go2go: recursive type constraint fails with confusing error
go/types, types2: recursive type constraint fails with confusing error
Mar 18, 2022
The error seems very reasonable to me, but it would be good to have the spec explicitly say what's disallowed (or maybe I just couldn't find it). I would assume something like "a type constraint in the definition of a type T may refer to T", parallel to "An interface type T may not embed any type element that is, contains, or embeds T, recursively." (but a bit broader, to ban e.g. type Rec[T []Rec[T]] ...).
testdata/manual.go:10:6: illegal cycle in declaration of Rec
testdata/manual.go:10:6: Rec refers to
testdata/manual.go:10:6: Rec
which seems pretty decent. We still need to document restrictions in the spec, though. Changed title accordingly.
griesemer
changed the title
go/types, types2: recursive type constraint fails with confusing error
spec: document that recursive type declarations through type parameters are not permitted
Jun 7, 2022
Here's a type:
Currently
go2go
complains with confusing errors; the first declaration fails withRec is not a generic type
(it obviously is) and the equivalenttype Rec[type T Rec] interface{ r(T) }
fails withRec is not an interface
(it also obviously is).I assume this shouldn't be allowed: to know if
Rec[T]
is a valid type forT
to implement, we need to know ifT
implementsRec[T]
, which is circular. But I can't find anything in the draft design that would prohibit it.(playground for convenience)
The text was updated successfully, but these errors were encountered: