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/internal/extract: extract L-value should replace its uses with dereferenced access #70784

Open
xzbdmw opened this issue Dec 11, 2024 · 0 comments
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

@xzbdmw
Copy link

xzbdmw commented Dec 11, 2024

gopls version

.

go env

.

What did you do?

    type foo struct {
        bar int
    }
    f := foo{bar: 1}
    f.bar = 2 // extract f.bar

What did you see happen?

Currently the produced code has some type error, this messy output happens in any LHS of an assignment:

	type foo struct {
		bar int
	}
	f := foo{bar: 1}
	xf.bar
	f.bar = 2 // extract f.bar

But even we fix the indent/newline format bug, the result would be:

    type foo struct {
        bar int
    }
    f := foo{bar: 1}
    x := f.bar
    x = 2 // semantic different, f.bar remains 1

What did you expect to see?

    type foo struct {
        bar int
    }
    f := foo{bar: 1}
    x := &f.bar
    *x = 2

Reference https://go-review.googlesource.com/c/tools/+/624035/comments/51ffbf6c_61124c11

The crucial point is that any time we extract an addressable expression to a new variable, we may need to extract its address, and replace all uses by *newVar.

Editor and settings

No response

Logs

No response

@xzbdmw xzbdmw 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 Dec 11, 2024
@gopherbot gopherbot added this to the Unreleased milestone Dec 11, 2024
@xzbdmw xzbdmw changed the title x/tools/internal/extract: extract L-value should replace its uses with dereferenced access gopls/internal/extract: extract L-value should replace its uses with dereferenced access Dec 11, 2024
@findleyr findleyr added the Refactoring Issues related to refactoring tools label Dec 11, 2024
@seankhliao seankhliao changed the title gopls/internal/extract: extract L-value should replace its uses with dereferenced access x/tools/gopls/internal/extract: extract L-value should replace its uses with dereferenced access Dec 13, 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