]> Cypherpunks.ru repositories - gostls13.git/commit
go/types, types2: correctly consider ~ (tilde) in constraint type inference
authorRobert Griesemer <gri@golang.org>
Fri, 25 Feb 2022 06:11:40 +0000 (22:11 -0800)
committerRobert Griesemer <gri@golang.org>
Tue, 1 Mar 2022 23:48:58 +0000 (23:48 +0000)
commit0807986fe6bf6040bafa9c415ab72e4cc8e519a4
treee6bb077c1d040f916649c8847a1bcd9b9f291187
parentaaa3d39f270d7b9957f34f3d2a68decba63beffe
go/types, types2: correctly consider ~ (tilde) in constraint type inference

When doing constraint type inference, we must consider whether the
constraint's core type is precise (no tilde) or imprecise (tilde,
or not a single specific type). In the latter case, we cannot infer
an unknown type argument from the (imprecise) core type because there
are infinitely many possible types. For instance, given

        [E ~byte]

if we don't know E, we cannot infer that E must be byte (it could be
myByte, etc.). On the other hand, if we do know the type argument,
say for S in this example:

        [S ~[]E, E any]

we must consider the underlying type of S when matching against ~[]E
because we have a tilde.

Because constraint type inference may infer type arguments that were
not eligible initially (because they were unknown and the core type
is imprecise), we must iterate the process until nothing changes any-
more. For instance, given

        [S ~[]E, M ~map[string]S, E any]

where we initially only know the type argument for M, we must ignore
S (and E) at first. After one iteration of constraint type inference,
S is known at which point we can infer E as well.

The change is large-ish but the actual functional changes are small:

- There's a new method "unknowns" to determine the number of as of yet
  unknown type arguments.

- The adjCoreType function has been adjusted to also return tilde
  and single-type information. This is now conveniently returned
  as (*term, bool), and the function has been renamed to coreTerm.

- The original constraint type inference loop has been adjusted to
  consider tilde information.

- This adjusted original constraint type inference loop has been
  nested in another loop for iteration, together with some minimal
  logic to control termination.

The remaining changes are modifications to tests:

- There's a substantial new test for this issue.

- Several existing test cases were adjusted to accomodate the
  fact that they inferred incorrect types: tildes have been
  removed throughout. Most of these tests are for pathological
  cases.

- A couple of tests were adjusted where there was a difference
  between the go/types and types2 version.

Fixes #51229.

Change-Id: If0bf5fb70ec22913b5a2da89adbf8a27fbc921d9
Reviewed-on: https://go-review.googlesource.com/c/go/+/387977
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>
21 files changed:
src/cmd/compile/internal/types2/api_test.go
src/cmd/compile/internal/types2/infer.go
src/cmd/compile/internal/types2/testdata/check/funcinference.go2
src/cmd/compile/internal/types2/testdata/check/typeinference.go2
src/cmd/compile/internal/types2/testdata/examples/inference.go2
src/cmd/compile/internal/types2/testdata/examples/typesets.go2
src/cmd/compile/internal/types2/testdata/fixedbugs/issue45548.go2
src/cmd/compile/internal/types2/testdata/fixedbugs/issue51229.go2 [new file with mode: 0644]
src/cmd/compile/internal/types2/unify.go
src/go/types/api_test.go
src/go/types/infer.go
src/go/types/testdata/check/funcinference.go2
src/go/types/testdata/check/typeinference.go2
src/go/types/testdata/examples/inference.go2
src/go/types/testdata/examples/typesets.go2
src/go/types/testdata/fixedbugs/issue45548.go2
src/go/types/testdata/fixedbugs/issue51229.go2 [new file with mode: 0644]
src/go/types/unify.go
test/typeparam/issue48424.go
test/typeparam/settable.go
test/typeparam/typelist.go