x/tools/gopls/internal/analysis/modernize: append([]T{}, x...) -> slices.Clone(x) fix may unsoundly return nil · Issue #73557 · golang/go · GitHub
More Web Proxy on the site http://driver.im/
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
aronatkins opened this issue
Apr 30, 2025
· 3 comments
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.
I expected the result of the program to be unchanged.
Workaround: Detect when m or s is nil and use a []string{} value rather than the slices.Clone() result.
The text was updated successfully, but these errors were encountered:
gopherbot
added
Tools
This label describes issues relating to any tools in the x/tools repository.
gopls
Issues related to the Go language server, gopls.
labels
Apr 30, 2025
Here's a recap of the behavior of the relevant operations wrt nil:
[]T(nil) is nil
T{} is non-nil
x[:i:i] has the nilness of x.
append(x, y...) preserves the nilness of x if the result is empty.
slices.Clip(x) preserves the nilness of x (undocumented).
slices.Clone(s) preserves the nilness of x (undocumented).
slices.Concat(slices...) is nil if the result is empty (undocumented).
The inconsistency of Concat is frustrating, but perhaps too late to change.
Given that the nilness behavior of the slices package is undocumented, I wonder whether this modernizer's fixes can ever be safely offered, since they could change in future (though perhaps any such change to the slices package would be rejected as too risky). Assuming the current semantics of the slices package are here to stay, we should not offer the slices.Concat fix if we cannot prove its result is non-empty.
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.
Go version
go version go1.24.2 darwin/arm64, golang.org/x/tools/gopls v0.18.1
Output of
go env
in your module/workspace:What did you do?
Original code:
Prior to any modification, running the program:
go run ./main.go #> []string{}
What did you see happen?
Adjusting this code with the modernizer modifies its behavior.
After modification, running the program:
go run ./main.go #> []string(nil)
The modified contents:
What did you expect to see?
I expected the result of the program to be unchanged.
Workaround: Detect when
m
ors
isnil
and use a[]string{}
value rather than theslices.Clone()
result.The text was updated successfully, but these errors were encountered: