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

x/tools/gopls/internal/analysis/modernize: Panic in modernize/sortslice.go #71786

Closed
sudo-bmitch opened this issue Feb 16, 2025 · 3 comments
Closed
Assignees
Labels
BugReport Issues describing a possible bug in the Go implementation. gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@sudo-bmitch
Copy link

sudo-bmitch commented Feb 16, 2025

modernize version

Installed modernize from commit 32ffaa3103522a0b05609e241d1b03b3b1abb9a6

$ modernize -V=full
/home/bmitch/data/golang/bin/modernize version devel comments-go-here buildID=45c8ca27715001da385a0579ed0e2a1f2def3a74abef28343cce339b6aa5032b

go env

$ go env
AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/bmitch/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/bmitch/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1555326513=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/bmitch/data/git/go.googlesource.com/tools/gopls/go.mod'
GOMODCACHE='/home/bmitch/data/golang/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/bmitch/data/golang'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/bmitch/sdk/go1.24.0'
GOSUMDB='sum.golang.org'
GOTELEMETRY='on'
GOTELEMETRYDIR='/home/bmitch/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/bmitch/sdk/go1.24.0/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.0'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

Running modernize from the master branch on my project github.com/regclient/regclient. I believe it is breaking on the following:

	sort.Slice(keys, func(a, b int) bool {
		if ip.entries[keys[a]].state != ip.entries[keys[b]].state {
			return ip.entries[keys[a]].state > ip.entries[keys[b]].state
		} else if ip.entries[keys[a]].state != types.CallbackActive {
			return ip.entries[keys[a]].last.Before(ip.entries[keys[b]].last)
		} else {
			return ip.entries[keys[a]].cur > ip.entries[keys[b]].cur
		}
	})

https://github.com/regclient/regclient/blob/486aedb6664f7a4e098054581347fd89acd97578/cmd/regctl/image.go#L1226-L1234

What did you see happen?

$ modernize ./...
panic: interface conversion: ast.Stmt is *ast.IfStmt, not *ast.ReturnStmt

goroutine 912 [running]:
golang.org/x/tools/gopls/internal/analysis/modernize.sortslice.func1(0xc0016d05a0, 0xc002cec400)
        /home/bmitch/data/git/go.googlesource.com/tools/gopls/internal/analysis/modernize/sortslice.go:60 +0x878
golang.org/x/tools/gopls/internal/analysis/modernize.sortslice.sortslice.filesUsing.func2.sortslice.sortslice.filesUsing.func2.Cursor.Children.func3.sortslice.sortslice.filesUsing.func2-range3.sortslice-range1-range4({0xc0039414b8?, 0x702aa5?})
        /home/bmitch/data/git/go.googlesource.com/tools/gopls/internal/analysis/modernize/sortslice.go:110 +0x62
golang.org/x/tools/internal/astutil/cursor.Cursor.Preorder.func1(0xc005cfd480)
        /home/bmitch/data/git/go.googlesource.com/tools/internal/astutil/cursor/cursor.go:98 +0xaf
golang.org/x/tools/gopls/internal/analysis/modernize.sortslice-range1(...)
        /home/bmitch/data/git/go.googlesource.com/tools/gopls/internal/analysis/modernize/sortslice.go:108
golang.org/x/tools/gopls/internal/analysis/modernize.sortslice.sortslice.filesUsing.func2-range3(...)
        /home/bmitch/data/git/go.googlesource.com/tools/gopls/internal/analysis/modernize/modernize.go:121
golang.org/x/tools/gopls/internal/analysis/modernize.sortslice.sortslice.filesUsing.func2.Cursor.Children.func3(...)
        /home/bmitch/data/git/go.googlesource.com/tools/internal/astutil/cursor/cursor.go:364
golang.org/x/tools/gopls/internal/analysis/modernize.sortslice.filesUsing.func2(...)
        /home/bmitch/data/git/go.googlesource.com/tools/gopls/internal/analysis/modernize/modernize.go:119
golang.org/x/tools/gopls/internal/analysis/modernize.sortslice(0xc0039487e0)
        /home/bmitch/data/git/go.googlesource.com/tools/gopls/internal/analysis/modernize/sortslice.go:105 +0x312
golang.org/x/tools/gopls/internal/analysis/modernize.run(0xc0039487e0)
        /home/bmitch/data/git/go.googlesource.com/tools/gopls/internal/analysis/modernize/modernize.go:74 +0x1a5
golang.org/x/tools/go/analysis/checker.(*Action).execOnce.func3(...)
        /home/bmitch/data/git/go.googlesource.com/tools/go/analysis/checker/checker.go:367
golang.org/x/tools/go/analysis/checker.(*Action).execOnce(0xc005da34a0)
        /home/bmitch/data/git/go.googlesource.com/tools/go/analysis/checker/checker.go:388 +0xad0
sync.(*Once).doSlow(0x0?, 0x0?)
        /home/bmitch/sdk/go1.24.0/src/sync/once.go:78 +0xab
sync.(*Once).Do(...)
        /home/bmitch/sdk/go1.24.0/src/sync/once.go:69
golang.org/x/tools/go/analysis/checker.(*Action).exec(...)
        /home/bmitch/data/git/go.googlesource.com/tools/go/analysis/checker/checker.go:265
golang.org/x/tools/go/analysis/checker.execAll.func1(0x0?)
        /home/bmitch/data/git/go.googlesource.com/tools/go/analysis/checker/checker.go:253 +0x45
created by golang.org/x/tools/go/analysis/checker.execAll in goroutine 1
        /home/bmitch/data/git/go.googlesource.com/tools/go/analysis/checker/checker.go:259 +0x147

This appears to be triggered by:

https://cs.opensource.google/go/x/tools/+/master:gopls/internal/analysis/modernize/sortslice.go;l=60;drc=a886a1c2ed0dee2af11f465e11bf48b11dda984c

What did you expect to see?

No panics.

Editor and settings

Run from the CLI.

Logs

n/a

@sudo-bmitch sudo-bmitch added gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository. labels Feb 16, 2025
@gopherbot gopherbot added this to the Unreleased milestone Feb 16, 2025
@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label Feb 16, 2025
@adonovan adonovan self-assigned this Feb 17, 2025
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/650215 mentions this issue: gopls/internal/analysis/modernize: sortslice: fix crash

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/650495 mentions this issue: [gopls-release-branch.0.18] gopls/internal/analysis/modernize: sortslice: fix crash

gopherbot pushed a commit to golang/tools that referenced this issue Feb 18, 2025
…ice: fix crash

The sole statement of a comparison func body is not necessarily
a return statement.

+ Test

Fixes golang/go#71786

Change-Id: Ic002035fc9fa303b62ed1828c13f3bdfb8bc6950
Reviewed-on: https://go-review.googlesource.com/c/tools/+/650215
Reviewed-by: Robert Findley <[email protected]>
Auto-Submit: Alan Donovan <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
(cherry picked from commit 776604a)
Reviewed-on: https://go-review.googlesource.com/c/tools/+/650495
Reviewed-by: Alan Donovan <[email protected]>
Auto-Submit: Robert Findley <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation. gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants