1
0
mirror of https://github.com/golang/go synced 2024-11-07 08:46:19 -07:00
The Go programming language
Go to file
Robert Griesemer f03ab0e014 go/types, types2: unify core types for unbound type parameters
NOTE: Should this change cause problems, the new functionality
can be disabled by setting the flag enableCoreTypeUnification
in unify.go to false.

In the code

func f1[M1 map[K1]int, K1 comparable](m1 M1) {}

func f2[M2 map[K2]int, K2 comparable](m2 M2) {
	f1(m2)
}

type inference attempts to unify the types of m1 and m2. This leads
to the unification attempt of M1 and M2. The result is that the type
argument for M1 is inferred to be M2. Since there is no furter function
argument to use, constraint type inference attempts to infer the type
for K1 which is still missing. Constraint type inference (inferB in
the trace below) compares the inferred type for M1 (i.e., M2) against
map[K1]int. M2 is bound to f2, not f1; with the existing algorithm
that means M2 is simply a named type without further information.
Unification fails and with that type inference, and the type checker
reports an error.

-- inferA [M1₁, K1₂] ➞ []
M1₁ ≡ M2₃
.  M1₁ ➞ M2₃
-- inferB [M1₁, K1₂] ➞ [M2₃, <nil>]
M1₁ ➞ M2₃
M1₁ ≡ map[K1₂]int
.  M2₃ ≡ map[K1₂]int
.  M2₃ ≢ map[K1₂]int
M1₁ ≢ map[K1₂]int
=> inferB [M1₁, K1₂] ➞ []
=> inferA [M1₁, K1₂] ➞ []

With this change, when attempting to unify M2 with map[K1]int,
rather than failing, the unifier now considers the core type of
M2 which is map[K2]int. This leads to the unification of K1 and
K2; so type inference successfully infers M2 for M1 and K2 for K1.

-- inferA [M1₁, K1₂] ➞ []
M1₁ ≡ M2₃
.  M1₁ ➞ M2₃
-- inferB [M1₁, K1₂] ➞ [M2₃, <nil>]
M1₁ ➞ M2₃
M1₁ ≡ map[K1₂]int
.  M2₃ ≡ map[K1₂]int
.  .  core M2₃ ≡ map[K1₂]int
.  .  map[K2₄]int ≡ map[K1₂]int
.  .  .  K2₄ ≡ K1₂
.  .  .  .  K1₂ ➞ K2₄
.  .  .  int ≡ int
=> inferB [M1₁, K1₂] ➞ [M2₃, K2₄]
=> inferA [M1₁, K1₂] ➞ [M2₃, K2₄]

The fix for this issue was provided by Rob Findley in CL 380375;
this change is a copy of that fix with some additional changes:

- Constraint type inference doesn't simply use a type parameter's
  core type. Instead, if the type parameter type set consists of
  a single, possibly named type, it uses that type. Factor out the
  existing code into a new function adjCoreType. This change is not
  strictly needed but makes it easier to think about the code.

- Tracing code is added for debugging type inference. All tracing
  code is guarded with the flag traceEnabled which is set to false
  by default.

- The change to the unification algorithm is guarded with the flag
  enableCoreTypeUnification.

- The sprintf function has a new type switch case for lists of
  type parameters. This is used for tracing output (and was also
  missing for a panic that was printing type parameter lists).

Fixes #50755.

Change-Id: Ie50c8f4540fcd446a71b07e2b451a95339b530ce
Reviewed-on: https://go-review.googlesource.com/c/go/+/385354
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>
2022-02-12 18:53:12 +00:00
.github .github: remove duplicate security link 2022-01-07 17:55:09 +00:00
api runtime/debug: replace (*BuildInfo).Marshal methods with Parse and String 2022-02-09 19:44:03 +00:00
doc spec: combine section on type parameters and type parameter lists 2022-02-11 16:26:49 +00:00
lib/time lib/time, time/tzdata: update to 2021e 2021-11-11 05:16:39 +00:00
misc misc/reboot: don't use symlinks when copying GOROOT/src 2022-02-08 15:30:12 +00:00
src go/types, types2: unify core types for unbound type parameters 2022-02-12 18:53:12 +00:00
test go/types, types2: better error message for invalid array length 2022-02-11 22:01:05 +00:00
.gitattributes all: treat all files as binary, but check in .bat with CRLF 2020-06-08 15:31:43 +00:00
.gitignore internal/buildcfg: move build configuration out of cmd/internal/objabi 2021-04-16 19:20:53 +00:00
AUTHORS A+C: add Bharath Kumar Uppala (individual CLA) 2021-11-29 00:57:09 +00:00
codereview.cfg codereview.cfg: add codereview.cfg for master branch 2021-02-19 18:44:53 +00:00
CONTRIBUTING.md
CONTRIBUTORS A+C: add Bharath Kumar Uppala (individual CLA) 2021-11-29 00:57:09 +00:00
LICENSE
PATENTS
README.md README.md: update contribute URL 2021-09-30 13:33:21 +00:00
SECURITY.md SECURITY.md: update go versions 2019-09-26 15:34:57 +00:00

The Go Programming Language

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.

Gopher image Gopher image by Renee French, licensed under Creative Commons 3.0 Attributions license.

Our canonical Git repository is located at https://go.googlesource.com/go. There is a mirror of the repository at https://github.com/golang/go.

Unless otherwise noted, the Go source files are distributed under the BSD-style license found in the LICENSE file.

Download and Install

Binary Distributions

Official binary distributions are available at https://golang.org/dl/.

After downloading a binary release, visit https://golang.org/doc/install for installation instructions.

Install From Source

If a binary distribution is not available for your combination of operating system and architecture, visit https://golang.org/doc/install/source for source installation instructions.

Contributing

Go is the work of thousands of contributors. We appreciate your help!

To contribute, please read the contribution guidelines at https://golang.org/doc/contribute.

Note that the Go project uses the issue tracker for bug reports and proposals only. See https://golang.org/wiki/Questions for a list of places to ask questions about the Go language.