-
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
spec: are local types in different instantiations of a generic function identical? #58573
Comments
I think the argument here is that each distinct instantiation of a generic function produces a distinct non-generic function. Each of those functions has its own distinct local types, very much like any other non-generic function. |
I agree that each distinct instantiation should produce a distinct defined type, regardless of which type parameters the underlying type references. Users can always manually hoist the local defined type to package scope and explicitly add type parameters if they want finer-grained control over which type parameters affect its identity. |
https://go.dev/play/p/ARWTgThVoLC shows that the current implementation can have an exponential number of distinct local types. None of these require type parameters. This is a silly example, but it does worry me a bit. Could requiring this behavior in the spec cause future problems? |
@timothy-king I'm personally not concerned. You could construct an exponential number of type instantiations without local types: https://go.dev/play/p/2JZSLoIS9if?v=goprev The type checker includes logic to make sure that instantiations are always finite. Beyond that, I think it's up to users to make sure that finite number fits within their available build resources. E.g., we don't place any limitations on the number of unique type or function declarations a user can explicitly write in a source file. |
Change https://go.dev/cl/467995 mentions this issue: |
ssa.subster now supports substituting type parameters that are within local Named types. Generalizes the definition of isParameterized to support local Named types that contain type parameters that are not instantiated. Fixes golang/go#58491 Updates golang/go#58573 Change-Id: Id7a4e45c41126056105e483b59eb057c05786d66 Reviewed-on: https://go-review.googlesource.com/c/tools/+/467995 TryBot-Result: Gopher Robot <gobot@golang.org> gopls-CI: kokoro <noreply+kokoro@google.com> Reviewed-by: Robert Findley <rfindley@google.com> Reviewed-by: Alan Donovan <adonovan@google.com> Run-TryBot: Tim King <taking@google.com>
There is a situation preferring identical instantiation: a non-generic function is changed to a generic one for some reason, but the author doesn't realize the result. |
Yes, either choice is possibly surprising to someone. Treating type identity specially for nfreetypevars(T)=0 is a bit like treating new(T) specially where sizeof(T)=0. |
This has effectively been decided and implemented as discussed in the beginning. Leaving #65152 open for documentation and closing this one. |
Are local types within generic functions distinct for each instantiation, even when they don't reference the type parameters? The current implementation says yes, but I wonder whether this is required.
See https://go.dev/play/p/3taZi53yieT for example. Each instantiation of
f[T]
with a different type creates a distinctlocal
type:I think the spec should take a position one way or the other since the effect is so readily observable (unlike function equality, which we can prevent users from asking questions about). The simplest solution would be to require that the local types are distinct for each instantiation, so that "we don't have to define what it means for a local type to depend on a type parameter", as @ianlancetaylor says, noting also that "making the types distinct leads to implementation complexity if we can otherwise treat
F[int]
andF[MyInt]
as being the same".@ianlancetaylor @griesemer
The text was updated successfully, but these errors were encountered: