-
Notifications
You must be signed in to change notification settings - Fork 17.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go/types, types2: correctly include comparable in type set intersection
The comparable bit was handled incorrectly. This CL establishes a clear invariant for a type set's terms and its comparable bit and correctly uses the bit when computing term intersections. Relevant changes: - Introduce a new function intersectTermLists that does the correct intersection computation. Minor: - Moved the comparable bit after terms in _TypeSet to make it clearer that they belong together. - Simplify and clarify _TypeSet.IsAll predicate. - Remove the IsTypeSet predicate which was only used for error reporting in union.go, and use the existing predicates instead. - Rename/introduce local variables in computeInterfaceTypeSet for consistency and to avoid confusion. - Update some tests whose output has changed because the comparable bit is now only set if we have have the set of all types. For instance, for interface{comparable; int} the type set doesn't set the comparable bit because the intersection of comparable and int is just int; etc. - Add many more comments to make the code clearer. Fixes #51472. Change-Id: I8a5661eb1693a41a17ce5f70d7e10774301f38ab Reviewed-on: https://go-review.googlesource.com/c/go/+/390025 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>
- Loading branch information
Showing
12 changed files
with
268 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/cmd/compile/internal/types2/testdata/fixedbugs/issue51472.go2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2022 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package p | ||
|
||
func _[T comparable](x T) { | ||
_ = x == x | ||
} | ||
|
||
func _[T interface{interface{comparable}}](x T) { | ||
_ = x == x | ||
} | ||
|
||
func _[T interface{comparable; interface{comparable}}](x T) { | ||
_ = x == x | ||
} | ||
|
||
func _[T interface{comparable; ~int}](x T) { | ||
_ = x == x | ||
} | ||
|
||
func _[T interface{comparable; ~[]byte}](x T) { | ||
_ = x /* ERROR cannot compare */ == x | ||
} | ||
|
||
// TODO(gri) The error message here should be better. See issue #51525. | ||
func _[T interface{comparable; ~int; ~string}](x T) { | ||
_ = x /* ERROR cannot compare */ == x | ||
} | ||
|
||
// TODO(gri) The error message here should be better. See issue #51525. | ||
func _[T interface{~int; ~string}](x T) { | ||
_ = x /* ERROR cannot compare */ == x | ||
} | ||
|
||
func _[T interface{comparable; interface{~int}; interface{int|float64}}](x T) { | ||
_ = x == x | ||
} | ||
|
||
func _[T interface{interface{comparable; ~int}; interface{~float64; comparable; m()}}](x T) { | ||
_ = x /* ERROR cannot compare */ == x | ||
} | ||
|
||
// test case from issue | ||
|
||
func f[T interface{comparable; []byte|string}](x T) { | ||
_ = x == x | ||
} | ||
|
||
func _(s []byte) { | ||
f( /* ERROR \[\]byte does not implement interface{comparable; \[\]byte\|string} */ s) | ||
_ = f[[ /* ERROR does not implement */ ]byte] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.