From 6ea17aa52c5f66c1fd72b74c36f8036e17ddde34 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 23 Nov 2021 18:11:07 -0800 Subject: [PATCH] spec: adjust type identity rules for type parameters Change-Id: I5ffc7f26236487070447eaa0f6b14d1fab44c3c7 Reviewed-on: https://go-review.googlesource.com/c/go/+/366794 Trust: Robert Griesemer Reviewed-by: Ian Lance Taylor --- doc/go_spec.html | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index 6f30ed7b91..0fc5b4590f 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1750,7 +1750,7 @@ Two types are either identical or different.

-A defined type is always different from any other type. +A named type is always different from any other type. Otherwise, two types are identical if their underlying type literals are structurally equivalent; that is, they have the same literal structure and corresponding components have identical types. In detail: @@ -1775,15 +1775,17 @@ components have identical types. In detail: identical, and either both functions are variadic or neither is. Parameter and result names are not required to match. -

  • Two interface types are identical if they have the same set of methods - with the same names and identical function types. - Non-exported method names from different - packages are always different. The order of the methods is irrelevant.
  • +
  • Two interface types are identical if they define the same type set. +
  • Two map types are identical if they have identical key and element types.
  • Two channel types are identical if they have identical element types and the same direction.
  • + +
  • Two instantiated types are identical if + their defined types and all type arguments are identical. +
  • @@ -1798,18 +1800,18 @@ type ( A3 = int A4 = func(A3, float64) *A0 A5 = func(x int, _ float64) *[]string -) -type ( B0 A0 B1 []string B2 struct{ a, b int } B3 struct{ a, c int } B4 func(int, float64) *B0 B5 func(x int, y float64) *A1 -) -type C0 = B0 + C0 = B0 + D0[P1, P2 any] struct{ x P1; y P2 } + E0 = D0[int, string] +)

    @@ -1823,6 +1825,7 @@ A3 and int A4, func(int, float64) *[]string, and A5 B0 and C0 +D0[int, string] and E0 []int and []int struct{ a, b *T5 } and struct{ a, b *T5 } func(x int, y float64) *[]string, func(int, float64) (result *[]string), and A5 @@ -1832,7 +1835,13 @@ func(x int, y float64) *[]string, func(int, float64) (result *[]string), and A5 B0 and B1 are different because they are new types created by distinct type definitions; func(int, float64) *B0 and func(x int, y float64) *[]string -are different because B0 is different from []string. +are different because B0 is different from []string; +and P1 and P2 are different because they are different +type parameters. +D0[int, string] and struct{ x int; y string } are +different because the former is an instantiated +defined type while the latter is a type literal +(but they are still assignable).

    Assignability