-
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: type inference should unify interface types #51593
Comments
CC @griesemer |
Conservatively marking as a release blocker. |
Slightly simplified reproducer: package p
func f[P interface{ m() R }, R any](P) {}
func _() {
var x interface { m() int }
f(x)
} |
I've been experimenting a bit, and made the following discoveries (using a modified version of
package main
type BasicAnon = interface {
M() int
}
func InferAnon[I []interface{ M() R }, R any](arg I) {
}
func main() {
var a []BasicAnon
InferAnon(a)
_ = b
} So, it seems that the implementation already kind-of supports type inference via interface methods, probably as an artifact of the unification algorithm mirroring the type equality/assignability algorithm. |
The assertion is clearly wrong and needs to be removed - we can see signatures of interface methods in that code. The constraint type inference fails at the moment for In any case, thanks for this bug report. At the very least we will address the crash for 1.18. |
Change https://go.dev/cl/391615 mentions this issue: |
As mentioned above, I'd caution that it seems that the generics type checking code currently is a bit inconsistent in that it allows unification of some constructs (e.g. How bad this is with respect to the release depends on whether such "structural inference" (for the lack of a better word) for interfaces is intended to be allowed in the future for all cases, or forbidden for all cases. In the former case, one should probably also allow unification of I've played around a bit at https://github.com/cr7pt0gr4ph7/go-types-experiments and https://gotipplay.golang.org/p/W-HYHRcmIXD and got the extended inference to work quite trivially, but that is definitely out of scope for this issue. |
The removed assertion was never incorrect, as signatures may be from methods in interfaces, and (some) interfaces set the receivers of their methods (so we have a position for error reporting). This CL changes the issue below from a release blocker to an issue for Go 1.19. For #51593. Change-Id: I0c5f2913b397b9ab557ed74a80cc7a715e840412 Reviewed-on: https://go-review.googlesource.com/c/go/+/391615 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
The spec section on type inference will eventually need to be refined/rewritten. We're not going to change anything else here besides fixing the crash for 1.18. As long as we are not inferring incorrect types, we're ok. Eventually we would like to infer these "obvious" cases. As a consequence, code that may not be valid now (inference fails) may become valid in the future (which is ok), but hopefully not the other war around (which would not be ok). |
Change https://go.dev/cl/391796 mentions this issue: |
Once this is cherry-picked into the 1.18 release branch, this is not a release blocker anymore and can be marked for Go 1.19. |
…(don't crash) The removed assertion was never incorrect, as signatures may be from methods in interfaces, and (some) interfaces set the receivers of their methods (so we have a position for error reporting). This CL changes the issue below from a release blocker to an issue for Go 1.19. For #51593. Change-Id: I0c5f2913b397b9ab557ed74a80cc7a715e840412 Reviewed-on: https://go-review.googlesource.com/c/go/+/391615 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> (cherry picked from commit 914195c) Reviewed-on: https://go-review.googlesource.com/c/go/+/391796 Trust: Dmitri Shuralyov <dmitshur@golang.org> Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
The cherry-pick commit 8706c09 (CL 391796) has landed on release-branch.go1.18. |
Moving to 1.21 as it likely requires a proposal. |
Change https://go.dev/cl/472298 mentions this issue: |
Change https://go.dev/cl/499282 mentions this issue: |
For #39661. For #41176. For #51593. For #52397. For #57192. For #58645. For #58650. For #58671. For #59338. For #59750. For #60353. Change-Id: Ib731c9f2879beb541f44cb10e40c36a8677d3ad4 Reviewed-on: https://go-review.googlesource.com/c/go/+/499282 TryBot-Bypass: Robert Griesemer <gri@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Robert Griesemer <gri@google.com>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Probably also with 1.18rc1, haven't had a chance to test it yet.
What operating system and processor architecture are you using (
go env
)?Gotip Playground.
What did you do?
Experimenting with type inference and anonymous interface types as constraints and/or inference inputs.
https://gotipplay.golang.org/p/lJOnm9LexpD
What did you expect to see?
Either a compiler error, or a successful compilation.
What did you see instead?
A compiler crash due to an assertion failure in
(*cycleFinder).typ
on this line:The assumption made in the comment is obviously not true with respect to interface method types, for which
(*Signature).Recv()
is set to the declaring interface's type.The text was updated successfully, but these errors were encountered: