Skip to content

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

Closed
@sudo-bmitch

Description

@sudo-bmitch

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

Metadata

Metadata

Assignees

Labels

BugReportIssues describing a possible bug in the Go implementation.ToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions