-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
cmd/compile: found illegal assignment FUNC-func(int) error -> main.Fn[int]; #50177
Comments
Thanks. Can you please include a short program that triggers the error? |
Sorry, accidentally hit |
(Compilation succeeds with
|
I don't think this will work currently. We don't allow local type declarations (or functions for that matter) with generic parameters. Somewhat related to #47631 except it's a generic type declaration in a non-generic function, instead of a non-generic type declaration in a generic function. |
Thanks, @randall77 . More than happy to be told not to do this as I didn't really like the way the test was looking anyway; but |
@gri @ianlancetaylor @findleyr @danscales I guess we need to decide whether we support local declarations of generic types in non-generic functions. Maybe we could? I'm not sure if this doesn't work because we never tried it, or because it's not supposed to work, or because it should work but doesn't. Opinions welcome. I have a CL ready to officially make this an error like for #47631, but that's only if we decide it is the right thing to do. |
Change https://golang.org/cl/372154 mentions this issue: |
It looks like we are confusing the two local named types X incorrectly. If you rename the second local type to Y, things work fine. We don't have the same confusion for non-generic local types, so must be some generic-only state. I will take a further look. However, I would say, for this release, it is probably less confusing to the user and reduces the number of possible unusual cases if we disallow local generic type declarations in non-generic functions. |
I think that eventually we should support local declarations of generic types in non-generic functions, but I think it's fine if we don't support that in 1.18. |
Change https://golang.org/cl/372654 mentions this issue: |
The problem is that instantiations of local types need to be popped from the symbol table when leaving the scope of the local type, else they might get used for an instantiation of a later same-name local type. I have the relatively simple fix above, but still need to think about other possible issues, since local generic types was not a focus for the code and we have no tests. |
Since we use existing instantiations from the symbol table when possible (to make sure each instantiation is unique), we need to pop instantiations of local types when leaving the containing scope. g.stmts() now pushes and pops scope, and we do a Pushdcl() in g.typ0() when creating an instantiation of a local type. Non-instantiated local types (generic or not) are translated directly from types2, so they don't need to be pushed/popped. We don't export function bodies with local types, so there is no issue during import. We still don't support local types in generic functions/methods. Fixes golang#50177 Change-Id: If2d2fe71aec003d13f0338565c7a0da2c9580a14 Reviewed-on: https://go-review.googlesource.com/c/go/+/372654 Reviewed-by: Keith Randall <khr@golang.org> Trust: Dan Scales <danscales@google.com> Run-TryBot: Dan Scales <danscales@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes, assuming the latest version is
gotip
.What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
This came up as I was trying to write some table driven unit tests for some functions that allow for generics. I've reduced the problem to this:
https://gotipplay.golang.org/p/0FYmUq0bzvr
The issue appears only if the a type is declared with the same name within multiple functions of a package file, and there is a field on the types with the same name, but different signature.
If I rename one of the types in the
caller*
functions, I do not see the issue.What did you expect to see?
Compiled code.
What did you see instead?
The text was updated successfully, but these errors were encountered: