go/types, types2: handle recursive constraint type inference #48656
Reminder issue: when type checking recursive function instances that use constraint type inference, we need to either succeed or produce a useful message. For example:
Case 1 currently fails with stack overflow (see also #48619), and case 2 fails with a not-very-useful error message ("cannot infer P ([ ])").
The text was updated successfully, but these errors were encountered:
If the type T inferred for a type parameter P is P itself (or a derived type containing P), a subsequent unification step leads to infinite recursion: at each encounter of P with the already inferred type T (which is or contains P), P stands for that T and the recursive matching process continues with T, which inevitably contains P again and recursion never terminates. This CL introduces a set of masks, one for each type parameter. When a type parameter is encountered for which a type has already been inferred, the type parameter is "masked" for the recursive matching of the inferred type. Masking makes the type parameter "invisible" such that it will be handled like any other type and not unpacked further. Fixes #48619. For #48656. Change-Id: Ic1d938322be51fd44323ea14f925303f58b27c97 Reviewed-on: https://go-review.googlesource.com/c/go/+/352832 Trust: Robert Griesemer <email@example.com> Run-TryBot: Robert Griesemer <firstname.lastname@example.org> Reviewed-by: Robert Findley <email@example.com>
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 <firstname.lastname@example.org> Reviewed-by: Robert Findley <email@example.com>
…"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 <firstname.lastname@example.org> Run-TryBot: Robert Griesemer <email@example.com> TryBot-Result: Go Bot <firstname.lastname@example.org> Reviewed-by: Robert Findley <email@example.com>