-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/refactor/inline: remove eta abstraction inlining assignments
Remove the unnecessary eta abstraction reported in golang/go#65217, by introducing a new strategy for rewriting assignments. This strategy involves analyzing both LHS and RHS of an assignment, and choosing between three substrategies: - spread the result expressions in cases where types are unambiguous - predeclare LHS variables in cases where the return is itself a spread call - convert RHS expressions if types involve implicit conversions Doing this involved some fixes to the logic for detecting trivial conversions, and tracking some additional information about untyped nils in return expressions. Since this strategy avoids literalization by modifying assignments in place, it must be able to avoid nested blocks, and so it explicitly records that braces may be elided. There is more work to be done here, both improving the writeType helper, and by variable declarations, but this CL lays a foundation for later expansion. For golang/go#65217 Change-Id: I9b3b595f7f678ab9b86ef7cf19936fd818b45426 Reviewed-on: https://go-review.googlesource.com/c/tools/+/580835 Reviewed-by: Alan Donovan <adonovan@google.com> Auto-Submit: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
- Loading branch information
Showing
6 changed files
with
781 additions
and
125 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
gopls/internal/test/marker/testdata/codeaction/removeparam_issue65217.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
This test reproduces condition of golang/go#65217, where the inliner created an | ||
unnecessary eta abstraction. | ||
|
||
-- go.mod -- | ||
module unused.mod | ||
|
||
go 1.18 | ||
|
||
-- a/a.go -- | ||
package a | ||
|
||
type S struct{} | ||
|
||
func (S) Int() int { return 0 } | ||
|
||
func _() { | ||
var s S | ||
_ = f(s, s.Int()) | ||
var j int | ||
j = f(s, s.Int()) | ||
_ = j | ||
} | ||
|
||
func _() { | ||
var s S | ||
i := f(s, s.Int()) | ||
_ = i | ||
} | ||
|
||
func f(unused S, i int) int { //@codeaction("unused", "unused", "refactor.rewrite", rewrite, "Refactor: remove unused parameter"), diag("unused", re`unused`) | ||
return i | ||
} | ||
|
||
-- @rewrite/a/a.go -- | ||
package a | ||
|
||
type S struct{} | ||
|
||
func (S) Int() int { return 0 } | ||
|
||
func _() { | ||
var s S | ||
_ = f(s.Int()) | ||
var j int | ||
j = f(s.Int()) | ||
_ = j | ||
} | ||
|
||
func _() { | ||
var s S | ||
var _ S = s | ||
i := f(s.Int()) | ||
_ = i | ||
} | ||
|
||
func f(i int) int { //@codeaction("unused", "unused", "refactor.rewrite", rewrite, "Refactor: remove unused parameter"), diag("unused", re`unused`) | ||
return i | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.