Skip to content
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

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

Open
benjaminjkraft opened this issue Aug 19, 2020 · 4 comments
Assignees
Milestone

Comments

@benjaminjkraft
Copy link

@benjaminjkraft 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 label Aug 19, 2020
@ianlancetaylor ianlancetaylor added this to the Unreleased milestone Aug 19, 2020
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Aug 19, 2020

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 removed this from the Unreleased milestone Mar 18, 2022
@griesemer griesemer added this to the Go1.19 milestone Mar 18, 2022
@griesemer
Copy link
Contributor

@griesemer griesemer commented Mar 18, 2022

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

@benjaminjkraft benjaminjkraft commented Mar 23, 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]] ...).

@griesemer
Copy link
Contributor

@griesemer griesemer commented Jun 7, 2022

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
@griesemer griesemer removed the NeedsInvestigation label Jun 7, 2022
@griesemer griesemer removed this from the Go1.19 milestone Jun 27, 2022
@griesemer griesemer added this to the Go1.20 milestone Jun 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

4 participants