From: Robert Griesemer
Date: Tue, 13 Dec 2022 00:30:42 +0000 (-0800)
Subject: spec: introduce notion of strict comparability
X-Git-Tag: go1.20rc2~1^2~31
X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=ffefcd360b64e197131d6dc37a7ed015a2643c1b;p=gostls13.git
spec: introduce notion of strict comparability
- Rephrase the notion of "comparability" from a property
of values (operands) to a property of types and adjust
dependent prose.
- Introduce the notion of "strict comparability".
- Fix the definitions of comparability for type interfaces
and type parameters.
- Define the predeclared identifier "comparable" as stricly
comparable.
These changes address existing problems in the spec as outlined
in the section on "Related spec issues" in issue #56548.
For #56548.
Change-Id: Ibc8c2f36d92857a5134eadc18358624803d3dd21
Reviewed-on: https://go-review.googlesource.com/c/go/+/457095
Reviewed-by: Ian Lance Taylor
Reviewed-by: Matthew Dempsky
TryBot-Bypass: Robert Griesemer
Reviewed-by: Robert Findley
Reviewed-by: Robert Griesemer
---
diff --git a/doc/go_spec.html b/doc/go_spec.html
index 6d7f90e98d..7b4bde0fe0 100644
--- a/doc/go_spec.html
+++ b/doc/go_spec.html
@@ -1,6 +1,6 @@
@@ -2684,18 +2684,17 @@ other interfaces based on their type sets. But this should get us going for now.
The predeclared
interface type comparable
denotes the set of all non-interface types that are
-comparable. Specifically,
+strictly comparable. Specifically,
a type T
implements comparable
if:
-
-
T
is not an interface type and T
supports the operations
- ==
and !=
; or
+ T
is not an interface type and T
is strictly comparable; or
-
T
is an interface type and each type in T
's
- type set implements comparable
.
+ type set is strictly comparable.
@@ -2707,12 +2706,13 @@ Even though interfaces that are not type parameters can be
-int // implements comparable
+int // implements comparable (int is strictly comparable)
[]byte // does not implement comparable (slices cannot be compared)
interface{} // does not implement comparable (see above)
-interface{ ~int | ~string } // type parameter only: implements comparable
-interface{ comparable } // type parameter only: implements comparable
-interface{ ~int | ~[]byte } // type parameter only: does not implement comparable (not all types in the type set are comparable)
+interface{ ~int | ~string } // type parameter only: implements comparable (int, string types are stricly comparable)
+interface{ comparable } // type parameter only: implements comparable (comparable implements itself)
+interface{ ~int | ~[]byte } // type parameter only: does not implement comparable (slices are not comparable)
+interface{ ~struct{ any } } // type parameter only: does not implement comparable (field any is not strictly comparable)
@@ -5019,69 +5019,71 @@ to the type of the second operand, or vice versa.
The equality operators ==
and !=
apply
-to operands that are comparable.
+to operands of comparable types.
The ordering operators <
, <=
, >
, and >=
-apply to operands that are ordered.
+apply to operands of ordered types.
These terms and the result of the comparisons are defined as follows:
-
- Boolean values are comparable.
+ Boolean types are comparable.
Two boolean values are equal if they are either both
true
or both false
.
-
- Integer values are comparable and ordered, in the usual way.
+ Integer types are comparable and ordered.
+ Two integer values are compared in the usual way.
-
- Floating-point values are comparable and ordered,
- as defined by the IEEE-754 standard.
+ Floating-point types are comparable and ordered.
+ Two floating-point values are compared as defined by the IEEE-754 standard.
-
- Complex values are comparable.
+ Complex types are comparable.
Two complex values
u
and v
are
equal if both real(u) == real(v)
and
imag(u) == imag(v)
.
-
- String values are comparable and ordered, lexically byte-wise.
+ String types are comparable and ordered.
+ Two string values are compared lexically byte-wise.
-
- Pointer values are comparable.
+ Pointer types are comparable.
Two pointer values are equal if they point to the same variable or if both have value
nil
.
Pointers to distinct zero-size variables may or may not be equal.
-
- Channel values are comparable.
+ Channel types are comparable.
Two channel values are equal if they were created by the same call to
make
or if both have value nil
.
-
- Interface values are comparable.
+ Interface types that are not type parameters are comparable.
Two interface values are equal if they have identical dynamic types
and equal dynamic values or if both have value
nil
.
-
A value
x
of non-interface type X
and
- a value t
of interface type T
are comparable when values
- of type X
are comparable and
+ a value t
of interface type T
can be compared
+ if type X
is comparable and
X
implements T
.
They are equal if t
's dynamic type is identical to X
and t
's dynamic value is equal to x
.
-
- Struct values are comparable if all their fields are comparable.
+ Struct types are comparable if all their field types are comparable.
Two struct values are equal if their corresponding
non-blank field values are equal.
The fields are compared in source order, and comparison stops as
@@ -5089,23 +5091,27 @@ These terms and the result of the comparisons are defined as follows:
-
- Array values are comparable if values of the array element type are comparable.
+ Array types are comparable if their array element types are comparable.
Two array values are equal if their corresponding element values are equal.
The elements are compared in ascending index order, and comparison stops
as soon as two element values differ (or all elements have been compared).
+
+ -
+ Type parameters are comparable if they are strictly comparable (see below).
+
A comparison of two interface values with identical dynamic types
-causes a run-time panic if values
-of that type are not comparable. This behavior applies not only to direct interface
+causes a run-time panic if that type
+is not comparable. This behavior applies not only to direct interface
value comparisons but also when comparing arrays of interface values
or structs with interface-valued fields.
-Slice, map, and function values are not comparable.
+Slice, map, and function types are not comparable.
However, as a special case, a slice, map, or function value may
be compared to the predeclared identifier nil
.
Comparison of pointer, channel, and interface values to nil
@@ -5126,6 +5132,30 @@ var (
)
+
+A type is strictly comparable if it is comparable and not an interface
+type nor composed of interface types.
+Specifically:
+
+
+
+ -
+ Boolean, numeric, string, pointer, and channel types are strictly comparable.
+
+
+ -
+ Struct types are strictly comparable if all their field types are strictly comparable.
+
+
+ -
+ Array types are strictly comparable if their array element types are strictly comparable.
+
+
+ -
+ Type parameters are strictly comparable if all types in their type set are strictly comparable.
+
+
+
Logical operators