-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
go/types, types2: explore "interleaved" type inference by combining function argument type inference with constraint type inference #51139
Comments
I don't think we should do anything here for Go 1.18 (except maybe try to get a better error message, that error message is pretty bad). In 1.18 this works fine: _ = Append(x, MyPtr(new(int))) and I don't have any problem telling people to write that. |
It looks even better if you define a generic Array type a bit like this: https://gotipplay.golang.org/p/tIsplqcunlC |
Completely agree about not doing anything for Go 1.18. |
Agree about doing nothing for Go 1.18.
|
My primary concern here is that we might be accepting code with the current algorithm that might not be accepted anymore if we change to an interleaving version, which would make a backward-compatible change difficult if not impossible. But I think that may not be the case after all. Here's a rough outline of an argument that perhaps can be proven more rigorously: The reason the Given any two arguments, suitably parameterized, we have the general situation func f[P Cp[P, Q], Q Cq[P, Q], ...](x Tx[P, Q], y Ty[P, Q], ...) ... Note that Cp and Cq cannot be a simple type parameter (as in With the current algorithm, if If With the interleaved algorithm, a named type for Thus, it seems at least plausible that the interleaved approach is strictly more powerful than the current approach. This would mean that we should be able to adopt if, should we choose so, without causing existing programs to fail. |
append
This issue is currently labeled as early-in-cycle for Go 1.19. |
This issue is currently labeled as early-in-cycle for Go 1.20. |
Too late for 1.20. |
Change https://go.dev/cl/470916 mentions this issue: |
This is extracted from an old comment by @rsc on the behavior of type inference as described in the spec (and as currently implemented). Given the following code:
Type inference passes in separate phases of function argument type inference and constraint type inference: In the first phase (function arguments) the following matches are established:
S -> []MyPtr
andT -> *int
. At this point we have all type arguments and we're done. After instantiation ofAppend
we have the non-generic function:and now
[]MyPtr
doesn't implement~[]*int
(MyPtr
is not identical to*int
).Per the comment by @rsc, this could work if function argument type inference and constraint type inference were interleaved (and inference using typed function arguments would stop as soon as all type arguments are known).
In such an interleaved world, as soon as we have established the mapping
S -> []MyPtr
constraint type inference would match[]MyPtr
against[]T
and establish the mappingT -> MyPtr
. At this point all type arguments are inferred and upon instantiation ofAppend
we would get:Calling this version of
Append
would work because the unnamed type*int
can be assigned to the named typeMyPtr
.It might even be possible to apply constraint type inference to constraints that don't have a single specific type: As soon as we have a type argument inferred from a function argument it could be matched against every specific type in the constraint.
The interleaved type inference behavior is more powerful in this specific case (and thus we could change this in a backward-compatible way).
@rsc, @ianlancetaylor, @findleyr for comments.
The text was updated successfully, but these errors were encountered: