go/src/internal
Robert Griesemer ee402e9882 go/types, types2: use exact unification for component types
This change defines two unification modes used to control unification:

- assign  set when unifying types involved in an assignment
- exact   if set, types unify if they can be made identical

Currently, unification is inexact: when a defined type is compared
against a type literal, the underlying type of the defined type is
considered. When channel types are compared, the channel direction
is ignored. And when defined types are compared where one (or both)
are interfaces, interface unification is used.

By contrast, exact unification requires types to match exactly:
if they can be unified, the types must be identical (with suitable
type arguments).

Exact unification is required when comparing component types.
For instance, when unifying func(x P) with func(x Q), the two
signatures unify only if P is identical to Q per Go's assignment
rules.

Until now we have ignored exact unification and made due with inexact
unification everywhere, even for component types. In some cases this
led to infinite recursions in the unifier, which we guarded against
with a depth limit (and unification failure).

Go's assignmemt rules allow inexact matching at the top-level but
require exact matching for element types.

This change passes 'assign' to the unifier when unifying parameter
against argument types because those follow assignment rules.
When comparing constraints, inexact unification is used as before.

In 'assign' mode, when comparing element types, the unifyier is
called recursively, this time with the 'exact' mode set, causing
element types to be compared exactly. If unification succeeds for
element types, they are identical (with suitable type arguments).

This change fixes #60460. It also fixes a bug in the test for
issue #60377. We also don't need to rely anymore on the recursion
depth limit (a temporary fix) for #59740. Finally, because we use
exact unification when comparing element types which are channels,
errors caused by assignment failures (due to inexact inference which
succeeded when it shouldn't have) now produce the correct inference
error.

Fixes #60460.
For #60377.
For #59740.

Change-Id: Icb6a9b4dbd34294f99328a06d52135cb499cab85
Reviewed-on: https://go-review.googlesource.com/c/go/+/498895
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2023-05-30 20:19:38 +00:00
..
abi cmd/link, internal/abi: minor follow-up cleanups 2023-05-17 18:24:13 +00:00
bisect internal/bisect: copy parser changes from CL 494177 2023-05-11 01:52:24 +00:00
buildcfg all: replace fmt.Sprintf("%d") with strconv.Itoa 2023-03-31 18:41:24 +00:00
bytealg internal/bytealg: fix alignment code in compare_riscv64.s 2023-05-30 16:05:30 +00:00
cfg cmd/go: abstract build cache, support implementations via child process 2023-05-25 00:49:37 +00:00
coverage internal/coverage: fix comment dupword, error typo 2023-05-24 20:23:21 +00:00
cpu internal/cpu: add a detection for Neoverse(N2, V2) cores 2023-04-25 14:08:20 +00:00
dag runtime: generate the lock ranking from a DAG description 2022-08-04 15:31:44 +00:00
diff all: gofmt main repo 2022-05-19 15:49:05 +00:00
fmtsort all: remove unnecessary allocations from w.WriteString(fmt.Sprint*(...)) by fmt.Fprint*(w, ...) 2022-09-14 16:11:21 +00:00
fuzz cmd/go: add check for unknown godebug setting 2023-04-18 13:19:19 +00:00
goarch internal/goarch, internal/goos: update generators for syslist.go 2022-06-15 21:31:23 +00:00
godebug internal/godebug: make test godebug calls not vary 2023-05-10 20:40:05 +00:00
godebugs cmd/go/internal/cache: use internal/godebug for three GODEBUGs 2023-05-24 22:05:27 +00:00
goexperiment cmd/go: abstract build cache, support implementations via child process 2023-05-25 00:49:37 +00:00
goos all: add wasip1 definitions 2023-03-30 18:50:57 +00:00
goroot internal/testenv: avoid rebuilding all of std in WriteImportcfg 2023-02-02 19:27:33 +00:00
goversion internal/goversion: update Version to 1.21 2023-01-17 19:47:10 +00:00
intern cmd/go: add check for unknown godebug setting 2023-04-18 13:19:19 +00:00
itoa
lazyregexp
lazytemplate all: gofmt -w -r 'interface{} -> any' src 2021-12-13 18:45:54 +00:00
nettrace all: gofmt main repo 2022-04-11 16:34:30 +00:00
obscuretestdata crypto/x509: allow BoringCrypto to use 4096-bit keys 2022-11-09 14:47:58 +00:00
oserror
pkgbits all: re-run stringer 2023-04-11 20:24:07 +00:00
platform cmd/dist: refine test conditions and enable more cgo tests on Android, iOS 2023-05-19 01:37:31 +00:00
poll net: implement wasip1 FileListener and FileConn 2023-05-25 00:12:41 +00:00
profile internal/profile: use internal/lazyregexp for the legacy parser 2023-01-20 09:27:47 +00:00
race
reflectlite internal/reflectlite: common up types, remove code 2023-05-11 13:45:58 +00:00
safefilepath internal/safefilepath: fix TestFromFS on Plan 9 2022-12-07 23:52:46 +00:00
saferio internal/saferio: handle potential total size overflow in SliceCap 2022-09-26 20:36:02 +00:00
singleflight internal/singleflight: move Done call in TestForgetUnshared 2023-03-29 22:21:50 +00:00
syscall syscall: implement wasip1 Fcntl 2023-05-26 17:59:52 +00:00
sysinfo
testenv cmd/dist: refine test conditions and enable more cgo tests on Android, iOS 2023-05-19 01:37:31 +00:00
testlog
testpty internal/testpty: fix error handling 2023-01-25 18:38:54 +00:00
trace runtime: emit STW events for all pauses, not just those for the GC 2023-05-19 17:06:45 +00:00
txtar all: replace package ioutil with os and io in src 2022-09-20 02:13:02 +00:00
types go/types, types2: use exact unification for component types 2023-05-30 20:19:38 +00:00
unsafeheader all: gofmt -w -r 'interface{} -> any' src 2021-12-13 18:45:54 +00:00
xcoff all: fix misuses of "a" vs "an" 2023-04-04 14:20:53 +00:00
zstd internal/zstd: new internal package for zstd decompression 2023-04-18 20:34:13 +00:00