Skip to content

spec: document that recursive type declarations through type parameters are not permitted #40882

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

Closed
benjaminjkraft opened this issue Aug 19, 2020 · 5 comments
Assignees
Labels
Documentation Issues describing a change to documentation. FrozenDueToAge
Milestone

Comments

@benjaminjkraft
Copy link

benjaminjkraft commented Aug 19, 2020

Here's a type:

type Rec[type T Rec[T]] interface{ r(T) }

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.

(playground for convenience)

@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 19, 2020
@ianlancetaylor ianlancetaylor added this to the Unreleased milestone Aug 19, 2020
@ianlancetaylor
Copy link
Contributor

I don't think these cases should be permitted but it does seem that the errors generated by the type checker are not ideal.

@griesemer griesemer modified the milestones: Unreleased, Go1.19 Mar 18, 2022
@griesemer
Copy link
Contributor

We currently report a cycle error here which seems reasonable, but we should review if such code should actually be permitted.

@griesemer 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
@benjaminjkraft
Copy link
Author

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]] ...).

@griesemer
Copy link
Contributor

The type checker now reports

    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 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
@gopherbot gopherbot added the Documentation Issues describing a change to documentation. label Jun 7, 2022
@griesemer griesemer removed the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jun 7, 2022
@griesemer griesemer modified the milestones: Go1.19, Go1.20 Jun 27, 2022
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/457515 mentions this issue: spec: document illegal recursive type parameter lists

@gopherbot gopherbot moved this to Done in Release Blockers Dec 14, 2022
@golang golang locked and limited conversation to collaborators Dec 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation Issues describing a change to documentation. FrozenDueToAge
Projects
Status: Done
Development

No branches or pull requests

4 participants