Skip to content

Commit 7d2b78b

Browse files
committed
Note that all interface types are considered comparable as of Go 1.20
1 parent 368ca6c commit 7d2b78b

File tree

4 files changed

+83
-91
lines changed

4 files changed

+83
-91
lines changed

go/ql/lib/semmle/go/Types.qll

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,10 @@ class Type extends @type {
112112
or
113113
u instanceof ArrayType and u.(ArrayType).getElementType().implementsComparable()
114114
or
115-
exists(InterfaceType uif | uif = u |
116-
not uif instanceof BasicInterfaceType and
117-
if exists(uif.getAnEmbeddedTypeSetLiteral())
118-
then
119-
// All types in the intersection of all the embedded type set
120-
// literals must implement comparable.
121-
forall(Type intersectionType |
122-
intersectionType = uif.getAnEmbeddedTypeSetLiteral().getATerm().getType() and
123-
forall(TypeSetLiteralType tslit | tslit = uif.getAnEmbeddedTypeSetLiteral() |
124-
intersectionType = tslit.getATerm().getType()
125-
)
126-
|
127-
intersectionType.implementsComparable()
128-
)
129-
else uif.isOrEmbedsComparable()
130-
)
115+
// As of Go 1.20, any interface type satisfies the `comparable` constraint, even though comparison
116+
// may panic at runtime depending on the actual object's concrete type.
117+
// Look at git history here if you need the old definition.
118+
u instanceof InterfaceType
131119
)
132120
}
133121

go/ql/test/library-tests/semmle/go/Types/QualifiedNames.expected

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,31 @@
5151
| interface.go:95:6:95:8 | i18 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.i18 |
5252
| interface.go:101:6:101:8 | i19 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.i19 |
5353
| interface.go:105:6:105:8 | i20 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.i20 |
54-
| interface.go:110:6:110:19 | testComparable | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable |
55-
| interface.go:111:6:111:20 | testComparable0 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable0 |
56-
| interface.go:112:6:112:20 | testComparable1 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable1 |
57-
| interface.go:113:6:113:20 | testComparable2 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable2 |
58-
| interface.go:114:6:114:20 | testComparable3 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable3 |
59-
| interface.go:115:6:115:20 | testComparable4 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable4 |
60-
| interface.go:116:6:116:20 | testComparable5 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable5 |
61-
| interface.go:117:6:117:20 | testComparable6 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable6 |
62-
| interface.go:118:6:118:20 | testComparable7 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable7 |
63-
| interface.go:119:6:119:20 | testComparable8 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable8 |
64-
| interface.go:120:6:120:20 | testComparable9 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable9 |
65-
| interface.go:121:6:121:21 | testComparable10 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable10 |
66-
| interface.go:122:6:122:21 | testComparable11 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable11 |
67-
| interface.go:123:6:123:21 | testComparable12 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable12 |
68-
| interface.go:124:6:124:21 | testComparable13 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable13 |
69-
| interface.go:125:6:125:21 | testComparable14 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable14 |
70-
| interface.go:126:6:126:21 | testComparable15 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable15 |
71-
| interface.go:127:6:127:21 | testComparable16 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable16 |
72-
| interface.go:128:6:128:21 | testComparable17 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable17 |
73-
| interface.go:129:6:129:21 | testComparable18 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable18 |
74-
| interface.go:130:6:130:21 | testComparable19 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable19 |
75-
| interface.go:131:6:131:21 | testComparable20 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable20 |
76-
| interface.go:132:6:132:21 | testComparable21 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable21 |
77-
| interface.go:133:6:133:21 | testComparable22 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable22 |
78-
| interface.go:134:6:134:21 | testComparable23 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable23 |
54+
| interface.go:114:6:114:19 | testComparable | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable |
55+
| interface.go:115:6:115:20 | testComparable0 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable0 |
56+
| interface.go:116:6:116:20 | testComparable1 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable1 |
57+
| interface.go:117:6:117:20 | testComparable2 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable2 |
58+
| interface.go:118:6:118:20 | testComparable3 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable3 |
59+
| interface.go:119:6:119:20 | testComparable4 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable4 |
60+
| interface.go:120:6:120:20 | testComparable5 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable5 |
61+
| interface.go:121:6:121:20 | testComparable6 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable6 |
62+
| interface.go:122:6:122:20 | testComparable7 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable7 |
63+
| interface.go:123:6:123:20 | testComparable8 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable8 |
64+
| interface.go:124:6:124:20 | testComparable9 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable9 |
65+
| interface.go:125:6:125:21 | testComparable10 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable10 |
66+
| interface.go:126:6:126:21 | testComparable11 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable11 |
67+
| interface.go:127:6:127:21 | testComparable12 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable12 |
68+
| interface.go:128:6:128:21 | testComparable13 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable13 |
69+
| interface.go:129:6:129:21 | testComparable14 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable14 |
70+
| interface.go:130:6:130:21 | testComparable15 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable15 |
71+
| interface.go:131:6:131:21 | testComparable16 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable16 |
72+
| interface.go:132:6:132:21 | testComparable17 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable17 |
73+
| interface.go:133:6:133:21 | testComparable18 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable18 |
74+
| interface.go:134:6:134:21 | testComparable19 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable19 |
75+
| interface.go:135:6:135:21 | testComparable20 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable20 |
76+
| interface.go:136:6:136:21 | testComparable21 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable21 |
77+
| interface.go:137:6:137:21 | testComparable22 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable22 |
78+
| interface.go:138:6:138:21 | testComparable23 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.testComparable23 |
7979
| pkg1/embedding.go:8:6:8:9 | base | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types/pkg1.base |
8080
| pkg1/embedding.go:19:6:19:13 | embedder | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types/pkg1.embedder |
8181
| pkg1/embedding.go:22:6:22:16 | ptrembedder | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types/pkg1.ptrembedder |

go/ql/test/library-tests/semmle/go/Types/Types.expected

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,31 @@
5151
| interface.go:95:6:95:8 | i18 | i18 |
5252
| interface.go:101:6:101:8 | i19 | i19 |
5353
| interface.go:105:6:105:8 | i20 | i20 |
54-
| interface.go:110:6:110:19 | testComparable | testComparable |
55-
| interface.go:111:6:111:20 | testComparable0 | testComparable0 |
56-
| interface.go:112:6:112:20 | testComparable1 | testComparable1 |
57-
| interface.go:113:6:113:20 | testComparable2 | testComparable2 |
58-
| interface.go:114:6:114:20 | testComparable3 | testComparable3 |
59-
| interface.go:115:6:115:20 | testComparable4 | testComparable4 |
60-
| interface.go:116:6:116:20 | testComparable5 | testComparable5 |
61-
| interface.go:117:6:117:20 | testComparable6 | testComparable6 |
62-
| interface.go:118:6:118:20 | testComparable7 | testComparable7 |
63-
| interface.go:119:6:119:20 | testComparable8 | testComparable8 |
64-
| interface.go:120:6:120:20 | testComparable9 | testComparable9 |
65-
| interface.go:121:6:121:21 | testComparable10 | testComparable10 |
66-
| interface.go:122:6:122:21 | testComparable11 | testComparable11 |
67-
| interface.go:123:6:123:21 | testComparable12 | testComparable12 |
68-
| interface.go:124:6:124:21 | testComparable13 | testComparable13 |
69-
| interface.go:125:6:125:21 | testComparable14 | testComparable14 |
70-
| interface.go:126:6:126:21 | testComparable15 | testComparable15 |
71-
| interface.go:127:6:127:21 | testComparable16 | testComparable16 |
72-
| interface.go:128:6:128:21 | testComparable17 | testComparable17 |
73-
| interface.go:129:6:129:21 | testComparable18 | testComparable18 |
74-
| interface.go:130:6:130:21 | testComparable19 | testComparable19 |
75-
| interface.go:131:6:131:21 | testComparable20 | testComparable20 |
76-
| interface.go:132:6:132:21 | testComparable21 | testComparable21 |
77-
| interface.go:133:6:133:21 | testComparable22 | testComparable22 |
78-
| interface.go:134:6:134:21 | testComparable23 | testComparable23 |
54+
| interface.go:114:6:114:19 | testComparable | testComparable |
55+
| interface.go:115:6:115:20 | testComparable0 | testComparable0 |
56+
| interface.go:116:6:116:20 | testComparable1 | testComparable1 |
57+
| interface.go:117:6:117:20 | testComparable2 | testComparable2 |
58+
| interface.go:118:6:118:20 | testComparable3 | testComparable3 |
59+
| interface.go:119:6:119:20 | testComparable4 | testComparable4 |
60+
| interface.go:120:6:120:20 | testComparable5 | testComparable5 |
61+
| interface.go:121:6:121:20 | testComparable6 | testComparable6 |
62+
| interface.go:122:6:122:20 | testComparable7 | testComparable7 |
63+
| interface.go:123:6:123:20 | testComparable8 | testComparable8 |
64+
| interface.go:124:6:124:20 | testComparable9 | testComparable9 |
65+
| interface.go:125:6:125:21 | testComparable10 | testComparable10 |
66+
| interface.go:126:6:126:21 | testComparable11 | testComparable11 |
67+
| interface.go:127:6:127:21 | testComparable12 | testComparable12 |
68+
| interface.go:128:6:128:21 | testComparable13 | testComparable13 |
69+
| interface.go:129:6:129:21 | testComparable14 | testComparable14 |
70+
| interface.go:130:6:130:21 | testComparable15 | testComparable15 |
71+
| interface.go:131:6:131:21 | testComparable16 | testComparable16 |
72+
| interface.go:132:6:132:21 | testComparable17 | testComparable17 |
73+
| interface.go:133:6:133:21 | testComparable18 | testComparable18 |
74+
| interface.go:134:6:134:21 | testComparable19 | testComparable19 |
75+
| interface.go:135:6:135:21 | testComparable20 | testComparable20 |
76+
| interface.go:136:6:136:21 | testComparable21 | testComparable21 |
77+
| interface.go:137:6:137:21 | testComparable22 | testComparable22 |
78+
| interface.go:138:6:138:21 | testComparable23 | testComparable23 |
7979
| pkg1/embedding.go:8:6:8:9 | base | base |
8080
| pkg1/embedding.go:19:6:19:13 | embedder | embedder |
8181
| pkg1/embedding.go:22:6:22:16 | ptrembedder | ptrembedder |

go/ql/test/library-tests/semmle/go/Types/interface.go

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -107,28 +107,32 @@ type i20 interface {
107107
StringB() string
108108
}
109109

110-
type testComparable[T comparable] struct{} // $ implementsComparable
111-
type testComparable0[T0 i0] struct{} // $ implementsComparable
112-
type testComparable1[T1 i1] struct{} // $ implementsComparable
113-
type testComparable2[T2 i2] struct{} // $ implementsComparable
114-
type testComparable3[T3 i3] struct{} // $ implementsComparable
115-
type testComparable4[T4 i4] struct{} // $ implementsComparable
116-
type testComparable5[T5 i5] struct{} // does not implement comparable
117-
type testComparable6[T6 i6] struct{} // does not implement comparable
118-
type testComparable7[T7 i7] struct{} // $ implementsComparable
119-
type testComparable8[T8 i8] struct{} // does not implement comparable
120-
type testComparable9[T9 i9] struct{} // does not implement comparable
121-
type testComparable10[T10 i10] struct{} // $ implementsComparable
122-
type testComparable11[T11 i11] struct{} // $ implementsComparable
123-
type testComparable12[T12 i12] struct{} // does not implement comparable
124-
type testComparable13[T13 i13] struct{} // does not implement comparable
125-
type testComparable14[T14 i14] struct{} // $ implementsComparable
126-
type testComparable15[T15 i15] struct{} // $ implementsComparable
127-
type testComparable16[T16 i16] struct{} // does not implement comparable
128-
type testComparable17[T17 i17] struct{} // does not implement comparable
129-
type testComparable18[T18 i18] struct{} // $ implementsComparable
130-
type testComparable19[T19 i19] struct{} // does not implement comparable
131-
type testComparable20[T20 i20] struct{} // $ implementsComparable
132-
type testComparable21[T21 ~[]byte | string] struct{} // does not implement comparable
133-
type testComparable22[T22 any] struct{} // does not implement comparable
134-
type testComparable23[T23 ~[5]byte | string] struct{} // $ implementsComparable
110+
// These used to distinguish strictly-comparable interfaces (i.e. those which will not panic at runtime on attempting a comparison),
111+
// which were required to satisfy the `comparable` type constraint in Go <1.20. Now they all match `comparable` as all interfaces
112+
// are accepted. I mark those which are also strictly comparable for the future in case we want to expose that concept in QL.
113+
114+
type testComparable[T comparable] struct{} // $ implementsComparable isStrictlyComparable
115+
type testComparable0[T0 i0] struct{} // $ implementsComparable isStrictlyComparable
116+
type testComparable1[T1 i1] struct{} // $ implementsComparable isStrictlyComparable
117+
type testComparable2[T2 i2] struct{} // $ implementsComparable isStrictlyComparable
118+
type testComparable3[T3 i3] struct{} // $ implementsComparable isStrictlyComparable
119+
type testComparable4[T4 i4] struct{} // $ implementsComparable isStrictlyComparable
120+
type testComparable5[T5 i5] struct{} // $ implementsComparable
121+
type testComparable6[T6 i6] struct{} // $ implementsComparable
122+
type testComparable7[T7 i7] struct{} // $ implementsComparable isStrictlyComparable
123+
type testComparable8[T8 i8] struct{} // $ implementsComparable
124+
type testComparable9[T9 i9] struct{} // $ implementsComparable
125+
type testComparable10[T10 i10] struct{} // $ implementsComparable isStrictlyComparable
126+
type testComparable11[T11 i11] struct{} // $ implementsComparable isStrictlyComparable
127+
type testComparable12[T12 i12] struct{} // $ implementsComparable
128+
type testComparable13[T13 i13] struct{} // $ implementsComparable
129+
type testComparable14[T14 i14] struct{} // $ implementsComparable isStrictlyComparable
130+
type testComparable15[T15 i15] struct{} // $ implementsComparable isStrictlyComparable
131+
type testComparable16[T16 i16] struct{} // $ implementsComparable
132+
type testComparable17[T17 i17] struct{} // $ implementsComparable
133+
type testComparable18[T18 i18] struct{} // $ implementsComparable isStrictlyComparable
134+
type testComparable19[T19 i19] struct{} // $ implementsComparable
135+
type testComparable20[T20 i20] struct{} // $ implementsComparable isStrictlyComparable
136+
type testComparable21[T21 ~[]byte | string] struct{} // $ implementsComparable
137+
type testComparable22[T22 any] struct{} // $ implementsComparable
138+
type testComparable23[T23 ~[5]byte | string] struct{} // $ implementsComparable isStrictlyComparable

0 commit comments

Comments
 (0)