]> Cypherpunks repositories - gostls13.git/commit
go/types, types2: use new type inference algorithm exclusively
authorRobert Griesemer <gri@golang.org>
Thu, 23 Feb 2023 22:54:47 +0000 (14:54 -0800)
committerGopher Robot <gobot@golang.org>
Wed, 1 Mar 2023 20:55:32 +0000 (20:55 +0000)
commit969c3ba83934e4d9253140b67c4d4ca35095039d
treeb226630dc83558100df610b97e6f386c6f1de117
parent37a2004b431df6cdd3260cbfe2ddb7673e94b9ef
go/types, types2: use new type inference algorithm exclusively

The primary change is that type inference now always reports
an error if a unification step fails (rather than ignoring that
case, see infer2.go). This brings the implementation closely to
the description in #58650; but the implementation is more direct
by always maintaining a simple (type parameter => type) mapping.

To make this work, there are two small but subtle changes in the
unifier:

1) When deciding whether to proceed with the underlying type of
   a defined type, we also use the underlying type if the other
   type is a basic type (switch from !hasName(x) to isTypeLit(x)
   in unifier.go). This makes the case in issue #53650 work out.
   See the comment in the code for a detailed explanation of this
   change.

2) When we unify against an unbound type parameter, we always
   proceed with its core type (if any).
   Again, see the comment in the code for a detailed explanation
   of this change.

The remaining changes are comment and test adjustments. Because
the new logic now results in failing type inference where it
succeeded before or vice versa, and then instatiation or parameter
passing failed, a handful of error messages changed.
As desired, we still have the same number of errors for the same
programs.

Also, because type inference now produces different results, we
cannot easily compare against infer1 anymore (also infer1 won't
work correctly anymore due to the changes in the unifier). This
comparison (together with infer1) is now disabled.

Because some errors and their positions have changed, we need a
slightly larger error position tolerance for types2 (which produces
less accurate error positions than go/types). Hence the change in
types2/check_test.go.

Finally, because type inference is now slightly more relaxed,
issue #51139 doesn't produce a type unification failure anymore
for a (previously correctly) inferred type argument.

Fixes #51139.

Change-Id: Id796eea42f1b706a248843ad855d9d429d077bd1
Reviewed-on: https://go-review.googlesource.com/c/go/+/470916
Reviewed-by: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
src/cmd/compile/internal/types2/check_test.go
src/cmd/compile/internal/types2/infer2.go
src/cmd/compile/internal/types2/unify.go
src/go/types/infer2.go
src/go/types/unify.go
src/internal/types/testdata/examples/functions.go
src/internal/types/testdata/examples/inference.go
src/internal/types/testdata/fixedbugs/issue45985.go
src/internal/types/testdata/fixedbugs/issue49112.go
src/internal/types/testdata/fixedbugs/issue51139.go [new file with mode: 0644]
src/internal/types/testdata/fixedbugs/issue51472.go