You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
gandrejczuk opened this issue
May 13, 2025
· 3 comments
Assignees
Labels
BugReportIssues describing a possible bug in the Go implementation.goplsIssues related to the Go language server, gopls.ToolsThis label describes issues relating to any tools in the x/tools repository.
$ cat test.go
// You can edit this code!
// Click here and start typing.
package main
import (
"fmt"
)
func p(v int) *int {
return&v
}
func main() {
fmt.Println("Hello, 世界")
x := []*int{p(1), p(2), p(3), p(4)}
for i, member := range x {
println(i)
if*member == 2 {
x = append(x[:i], x[i+1:]...)
// Modernize will generate following code which will panic
// when last elemeny will be accessed
// x = slices.Delete(x, i, i+1)
}
}
fmt.Printf("%v\n", x)
}
$ go run test.go
Hello, 世界
0
1
2
3
[0xc0000120f0 0xc000012100 0xc000012108]
$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix test.go
$ cat test.go
// You can edit this code!
// Click here and start typing.
package main
import (
"fmt""slices"
)
func p(v int) *int {
return&v
}
func main() {
fmt.Println("Hello, 世界")
x := []*int{p(1), p(2), p(3), p(4)}
for i, member := range x {
println(i)
if*member == 2 {
x = slices.Delete(x, i, i+1)
// Modernize will generate following code which will panic
// when last elemeny will be accessed
// x = slices.Delete(x, i, i+1)
}
}
fmt.Printf("%v\n", x)
}
$ go run test.go
Hello, 世界
0
1
2
3
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x4912d7]
goroutine 1 [running]:
main.main()
test.go:20 +0x1d7
exit status 2
The text was updated successfully, but these errors were encountered:
gandrejczuk
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
May 13, 2025
adonovan
changed the title
x/tools/gopls/internal/analysis/modernize: Modernize might generate panicking code
x/tools/gopls/internal/analysis/modernize: slices.Delete modernizer is unsound (clears out s[len:cap])
May 13, 2025
Thanks for the bug report and test case. The fundamental issue here is that slices.Delete clears out the array elements between len and cap (or the new length and the old), ostensibly to aid garbage collection, whereas append does not, and without further analysis--that is I think far beyond our scope--it is impossible to tell whether they are needed.
BugReportIssues describing a possible bug in the Go implementation.goplsIssues related to the Go language server, gopls.ToolsThis label describes issues relating to any tools in the x/tools repository.
gopls version
The issue is for latest modernize
go env
What did you do?
What did you see happen?
What did you expect to see?
Editor and settings
No response
Logs
No response
The text was updated successfully, but these errors were encountered: