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: unnecessary eta abstraction while removing parameter #65217

Open
findleyr opened this issue Jan 22, 2024 · 1 comment
Open

x/tools/gopls: unnecessary eta abstraction while removing parameter #65217

findleyr opened this issue Jan 22, 2024 · 1 comment
Assignees
Labels
gopls Issues related to the Go language server, gopls. Refactoring Issues related to refactoring tools Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@findleyr
Copy link
Contributor

In CL 557496, we observed that removing an unused parameter resulted in an unnecessary eta abstraction:

From:

rewrites, err := getRewriteCodeActions(snapshot, pkg, pgf, fh, rng, snapshot.Options())

To:

rewrites, err := func() (_ []protocol.CodeAction, rerr error) {
	var options *settings.Options = snapshot.Options()
	return getRewriteCodeActions(pkg, pgf, fh, rng, options)
}()

In this case, I'm sure it's the effect detection that triggers the literalization, but that's not correct. There is no change in effects before and after the parameter is removed, since the unused parameter has no side effects.

This is probably an artifact of how parameter removal is implemented via inlined wrappers.

CC @adonovan @suzmue

@findleyr findleyr added this to the gopls/v0.16.0 milestone Jan 22, 2024
@gopherbot 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 Jan 22, 2024
@suzmue suzmue added the Refactoring Issues related to refactoring tools label Jan 25, 2024
@findleyr findleyr self-assigned this Apr 12, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/580835 mentions this issue: internal/refactor/inline: remove eta abstraction inlining assignments

gopherbot pushed a commit to golang/tools that referenced this issue Apr 26, 2024
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>
@findleyr findleyr modified the milestones: gopls/v0.16.0, gopls/v0.17.0 May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls Issues related to the Go language server, gopls. Refactoring Issues related to refactoring tools Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

3 participants