-
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/internal/types2: incorrect recorded type for untyped values that convert to a type parameter #45096
Comments
cc: @findleyr |
/cc @danscales |
I believe this is fixed in go/types, where untyped conversion is handled differently. The implicit type for a type param should be the type param itself, and due to the refactoring of untyped conversion, types are not recorded eagerly when checking convertibility. |
See also https://golang.org/cl/284256 for more context on the decision in go/types. |
@findleyr Ok, I will check against |
Note that originally in that CL, I was recording the type as a Sum type, which is another option, but reverted to the target type: A Sum type could be another possibility, as it contains slightly more information than the type param itself. But that is probably unnecessary. I'd be happy to port something like this to types2. Feel free to assign to me. |
Change https://golang.org/cl/302754 mentions this issue: |
Change https://golang.org/cl/302755 mentions this issue: |
This verifies that issue #45096 is not an issue for go/types. Updates #45096. Change-Id: I4e987b5d4928f0c864d0d2c0379149443beb4d5c Reviewed-on: https://go-review.googlesource.com/c/go/+/302754 Trust: Robert Griesemer <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]>
Change https://golang.org/cl/318850 mentions this issue: |
Change https://golang.org/cl/318989 mentions this issue: |
Change https://golang.org/cl/319749 mentions this issue: |
Change https://golang.org/cl/319890 mentions this issue: |
Change https://golang.org/cl/320050 mentions this issue: |
Change https://golang.org/cl/333013 mentions this issue: |
When the type checker encounters an untyped value (
0
in this example) that needs to operate against a value of type parameter type (x
in this case, in the expressionx < 0
), it makes sure that the operation (x < 0
) is valid for any possible type in the type list of the type parameter. The existing algorithm assumed that it can update the type of the untyped operand to the type of the other operand (as there's just one, usually), and then record that type. But if there's a type parameter with a type list, there may possibly be many types in that list, and the algorithm is run against all of them. But because we record the "final" type as soon as we have a "match", the first entry in a type parameter list "wins" (if the program is correct in the first place).For this example, the reported type for
0
is the first entry in the type list, which isint8
. But the correct type to be reported should beT
(or possiblyuntyped int
in this case, but that would probably break assumptions about what is reported bygo/types
and we likely can't change that).There may be a simple solution that special-cases this type parameter case. Longer-term, a better approach would be to eliminate "state" in this algorithm and have a more functional approach. That would be much safer, and likely easier to understand.
The text was updated successfully, but these errors were encountered: