Skip to content

x/tools/gopls/internal/extract: extract L-value should replace its uses with dereferenced access #70784

Open
@xzbdmw

Description

@xzbdmw

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    RefactoringIssues related to refactoring toolsToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions