-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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 report a better error message #48619
Comments
Smaller reproducer: package p
func f[P any](a, _ P) {
f(a, int(0))
} Compiling leads to an infinite recursion in type inference. |
Change https://golang.org/cl/352832 mentions this issue: |
Change https://golang.org/cl/353029 mentions this issue: |
This is an almost clean port of CL 352832 from types2 to go/types: The nest files and unify.go where copied verbatim; unify.go was adjusted with correct package name, a slightly different comment was restored to what it was. The test files got adjustments for error position. infer.go got a missing _Todo error code. For #48619. For #48656. Change-Id: Ia1a2d09e8bb37a85032b4b7e7c7a0b08e8c793a5 Reviewed-on: https://go-review.googlesource.com/c/go/+/353029 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
Change https://golang.org/cl/354690 mentions this issue: |
The previous fix attempt was incorrect. Re-opening. |
…"fix" The "fix" (CL 352832) for #48619 was incorrect and broke the unification algorithm in some cases (e.g., #48695). This CL reverts the changes made by CL 352832 to unify.go, and comments out code in corresponding tests. As a result, #48695 will be fixed, and we will re-open #48619. Fixes #48695. For #48619. For #48656. Change-Id: I91bc492062dbcc8dae7626f6b33f6dfabf48bcb8 Reviewed-on: https://go-review.googlesource.com/c/go/+/354690 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
Any update on this? Thanks. |
I de-duped #50273 to this issue. Once this is fixed we should confirm that it also resolves the repro there. |
Change https://golang.org/cl/377954 mentions this issue: |
This is a stop gap solution to avoid panics due to stack overflow during type unification. While this doesn't address the underlying issues (for which we are still investigating the correct approach), it prevents a panic during compilation and reports a (possibly not quite correct) error message. If the programs are correct in the first place, manually providing the desired type arguments is a viable work-around, resulting in code that will continue to work even when the issues here are fixed satisfactorily. For #48619. For #48656. Change-Id: I13bb14552b38b4170b5a1b820e3172d88ff656ec Reviewed-on: https://go-review.googlesource.com/c/go/+/377954 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
With the above CL this doesn't crash anymore. Still needs a proper fix but not a release blocker anymore. |
Due to golang/go#48619, constraints of quickSortOrdered and its related functions are not changed. So cast S to []E for now.
Like what we done in exp/maps, exp/slices would be better to accept a type whose underlying type is slice. Due to golang/go#48619, constraints of quickSortOrdered and its related functions are not changed. So cast S to []E for now.
Like what we done in exp/maps, exp/slices would be better to accept a type whose underlying type is slice. Due to golang/go#48619, constraints of quickSortOrdered and its related functions are not changed. So cast S to []E for now.
Change https://golang.org/cl/381274 mentions this issue: |
This produces an ok error message and doesn't crash. Leaving for 1.19. |
Change https://go.dev/cl/385494 mentions this issue: |
Type inference uses type parameter pointer identity to keep track of the correspondence between type parameters and type arguments. However, this technique can misidentify type parameters that are used in explicit type arguments or function arguments, as in the recursive instantiation below: func f[P *Q, Q any](p P, q Q) { f[P] } In this example, the fact that the P used in the instantation f[P] has the same pointer identity as the P we are trying to solve for via unification is coincidental: there is nothing special about recursive calls that should cause them to conflate the identity of type arguments with type parameters. To put it another way: any such self-recursive call is equivalent to a mutually recursive call, which does not run into any problems of type parameter identity. For example, the following code is equivalent to the code above. func f[P interface{*Q}, Q any](p P, q Q) { f2[P] } func f2[P interface{*Q}, Q any](p P, q Q) { f[P] } We can turn the first example into the second example by renaming type parameters in the original signature to give them a new identity. This CL does this for self-recursive instantiations. Fixes #51158 Fixes #48656 Updates #48619 Change-Id: I54fe37f2a79c9d98950cf6a3602335db2896dc24 Reviewed-on: https://go-review.googlesource.com/c/go/+/385494 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
The type inference algorithm doesn't run into endless recursions anymore since 1.18, and for 1.19 we have disabled the catch-all in case of an infinite recursion and haven't seen any problems so far. For the simple reproducer package p
func f[P any](a, _ P) {
f(a, int(0))
} the error message is:
which is exactly right (barring more information, the 2nd argument's type must be There's nothing else to do here for now. Closing. |
This is a stop gap solution to avoid panics due to stack overflow during type unification. While this doesn't address the underlying issues (for which we are still investigating the correct approach), it prevents a panic during compilation and reports a (possibly not quite correct) error message. If the programs are correct in the first place, manually providing the desired type arguments is a viable work-around, resulting in code that will continue to work even when the issues here are fixed satisfactorily. For golang#48619. For golang#48656. Change-Id: I13bb14552b38b4170b5a1b820e3172d88ff656ec Reviewed-on: https://go-review.googlesource.com/c/go/+/377954 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
What version of Go are you using (
go version
)?What did you do?
Trying to compile this sample: https://go2goplay.golang.org/p/MAplKhfhuKs
What did you expect to see?
The sample is incomplete (I tried to minimize it), so I don't expect it to compile. At the very least, there are calls to functions that don't exist.
The code is also incorrect. In the signature of
quickSort_gen_func
,a, b
should beint
, notElem
. Fixing this, the compiler produces an expected error message instead of going into infinite recursion.What did you see instead?
The text was updated successfully, but these errors were encountered: