-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
cmd/compile: valid type using type aliases reported as invalid #18640
Comments
CL https://golang.org/cl/35129 mentions this issue. |
Why do you say that this code is actually correct? According to "Type Cycles" in https://github.com/golang/proposal/blob/master/design/18130-type-alias.md this is forbidden. |
@ianlancetaylor It's correct because
is the same as
because |
…ng type aliases Known issue: #18640 (requires a bit more work, I believe). For #18130. Change-Id: I53dc26012070e0c79f63b7c76266732190a83d47 Reviewed-on: https://go-review.googlesource.com/35129 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Interesting. So you are saying that in |
The current spec as in the proposal may well be unclear. I think one way to think about it is: A type containing alias type names is valid if and only if it is valid after all the alias type names are substituted with the type the alias type names refer to, recursively. For the above examples:
becomes
after type alias
would be invalid because we end up in an endless expansion for
(which one might represent inside the compiler via a pointer cycle, but since it's not going through a named (new) type, one cannot print it w/o introducing artificial names). |
CL https://golang.org/cl/35831 mentions this issue. |
Intuitively: any legitimate type cycle must involve a non-alias type, otherwise it would infinitely recursively expand. Also, it's always safe to start typechecking at a non-alias type, because they use TFORW placeholder Types to resolve cycles. So I think this issue is fixed by CL 35831, which simply skips over top-level type alias declarations during phase 1. The result is we start typechecking from |
On Thu, Jan 26, 2017 at 9:25 AM, Matthew Dempsky ***@***.***> wrote:
So I think this issue is fixed by CL 35831, which simply skips over
top-level type alias declarations during phase 1. The result is we start
typechecking from b instead of a, which works okay.
This is a compelling argument and very elegant solution.
Thanks!
- gri
|
Unfortunately the fix (CL 35831) is incorrect: Even if we ignore type aliases in a first pass, it's sufficient for a type to refer to a type alias containing a valid cycle to get an incorrect cycle error: type (
b = c
c struct{ *b }
) is valid. But type (
a struct{ *b }
b = c
c struct{ *b }
) reports an error for
even though we have just established that |
cc: @mdempsky |
Change https://golang.org/cl/115455 mentions this issue: |
Another test case (from #23055): package p
type a struct { b }
type b = c
type c struct { *b } |
Change https://golang.org/cl/118078 mentions this issue: |
The original fix (https://go-review.googlesource.com/c/go/+/35831) for this issue was incorrect as it reported cycles in cases where it shouldn't. Instead, use a different approach: A type cycle containing aliases is only a cycle if there are no type definitions. As soon as there is a type definition, alias expansion terminates and there is no cycle. Approach: Split sprint_depchain into two non-recursive and more easily understandable functions (cycleFor and cycleTrace), and use those instead for cycle reporting. Analyze the cycle returned by cycleFor before issueing an alias cycle error. Also: Removed original fix (main.go) which introduced a separate crash (golang#23823). Fixes golang#18640. Fixes golang#23823. Fixes golang#24939. Change-Id: Ic3707a9dec40a71dc928a3e49b4868c5fac3d3b7 Reviewed-on: https://go-review.googlesource.com/118078 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Change https://golang.org/cl/147286 mentions this issue: |
Change https://golang.org/cl/151339 mentions this issue: |
In Go1.9, with type alias support (currently on dev.typealias branch):
produces:
There are a couple of issues:
<T>
)The text was updated successfully, but these errors were encountered: