Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type mismatch in slices.SortFunc due to breaking change in golang.org/x/exp/slices #55733

Closed
dt8269 opened this issue Aug 28, 2024 · 2 comments · Fixed by #55738
Closed

Type mismatch in slices.SortFunc due to breaking change in golang.org/x/exp/slices #55733

dt8269 opened this issue Aug 28, 2024 · 2 comments · Fixed by #55738
Labels
severity/moderate type/bug The issue is confirmed as a bug.

Comments

@dt8269
Copy link

dt8269 commented Aug 28, 2024

Bug Report

1. Minimal reproduce step (Required)

  • Clone the project and navigate to the file charset/charset.go.
  • In the GetSupportedCharsets() function, the slices.SortFunc function is used to sort a slice of *Charset by the Name field.

2. What did you expect to see? (Required)

Expected the code to compile without errors and the sorting functionality using slices.SortFunc to work correctly with Charset objects.

3. What did you see instead? (Required)

The compilation failed with the following error message:

charset/charset.go:94:28: type func(i *Charset, j *Charset) int of func(i, j *Charset) int {…} does not match inferred type func(a *Charset, b *Charset) bool for func(a E, b E) bool external/com_github_go_mysql_org_go_mysql/replication/binlogsyncer.go:427

This error arises due to a breaking change in golang.org/x/exp/slices, where the signature of slices.SortFunc was changed from a less function (returning a boolean) to a comparison function (returning an integer). This change aligns the exp/slices package with the slices package introduced in Go 1.21.

4. Cause of the issue

The root cause of the issue is that the golang.org/x/exp/slices package was updated to match the Go 1.21 standard library slices package. This introduces a breaking change that affects projects using Go 1.19 (which lacks the slices package in the standard library) and relying on golang.org/x/exp/slices.

5. The fix (Suggested)

diff --git a/pkg/parser/charset/charset.go b/pkg/parser/charset/charset.go
index 2be7b9c21d..056c54ef9b 100644
--- a/pkg/parser/charset/charset.go
+++ b/pkg/parser/charset/charset.go
@@ -100,8 +100,8 @@ func GetSupportedCharsets() []*Charset {
 	}
 
 	// sort charset by name.
-	slices.SortFunc(charsets, func(i, j *Charset) int {
-		return strings.Compare(i.Name, j.Name)
+	slices.SortFunc(charsets, func(i, j *Charset) bool {
+		return strings.Compare(i.Name, j.Name) < 0
 	})
 	return charsets
 }

note: the golang.org/x/exp package was created to try experimentations and explicitly states that new versions may break existing code:

Warning: Packages here are experimental and unreliable. Some may one day be promoted to the main repository or other subrepository, or they may be modified arbitrarily or even disappear altogether.

6. What is your TiDB version? (Required)

N/A

@dt8269 dt8269 added the type/bug The issue is confirmed as a bug. label Aug 28, 2024
@lance6716
Copy link
Contributor

This introduces a breaking change that affects projects using Go 1.19

I prefer we should raise the version to 1.21 or 1.22

module github.com/pingcap/tidb/pkg/parser
go 1.19

cc @hawkingrei @xhebox

@xhebox
Copy link
Contributor

xhebox commented Aug 29, 2024

I prefer we should raise the version to 1.21 or 1.22

No blocking from my side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
severity/moderate type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants