Skip to content

x/tools/gopls: renaming a type is not allowed when it has the same name as a parameter #66150

@DylanSp

Description

@DylanSp

gopls version

golang.org/x/tools/gopls v0.14.2

go env

$ go env
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/codespace/.cache/go-build'
GOENV='/home/codespace/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.7'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2156371313=/tmp/go-build -gno-record-gcc-switches'

What did you do?

Attempted to rename a struct type to a new name that was the same as existing parameter names, using VSCode's "Rename Symbol" action.

type myData struct {
	someField int
}

func standaloneFunc(data myData) {
	// intentional no-op
}

I attempted to rename myData to data.

https://github.com/DylanSp/gopls-rename-issue-repro/blob/main/main.go demonstrates the issue; it also happens with method receivers.

What did you see happen?

The following error message:

gopls-rename-issue-repro/main.go:3:6: renaming this type "myData" to "data" gopls-rename-issue-repro/main.go:7:13: would cause this reference to become shadowed gopls-rename-issue-repro/main.go:7:7: by this intervening var definition

What did you expect to see?

The myData type to be renamed to data. I think this should always be valid, but I'm not 100% positive; if I manually rename myData to data in the example code, it compiles.

Editor and settings

VSCode version 1.87.0.
VSCode Go extension v0.41.1.

Logs

[Info  - 11:19:56 PM] 2024/03/06 23:19:56 go info for /workspaces/gopls-rename-issue-repro
(go dir /workspaces/gopls-rename-issue-repro)
(go version go version go1.21.7 linux/amd64)
(valid build configuration = false)
(build flags: [])
(selected go env: [GO111MODULE=, GOCACHE=/home/codespace/.cache/go-build, GOFLAGS=, GOMODCACHE=/go/pkg/mod, GOPATH=/go, GOPRIVATE=, GOROOT=/usr/local/go, GOWORK=])


[Info  - 11:19:56 PM] 2024/03/06 23:19:56 go/packages.Load #1
	snapshot=0
	directory=file:///workspaces/gopls-rename-issue-repro
	query=[./ builtin]
	packages=2

[Info  - 11:19:56 PM] 2024/03/06 23:19:56 go/packages.Load #1: updating metadata for 1 packages

[Error - 11:19:56 PM] Request textDocument/semanticTokens/range failed.
  Message: semantictokens are disabled
  Code: 0 
[Error - 11:19:57 PM] Request textDocument/semanticTokens/full failed.
  Message: semantictokens are disabled
  Code: 0 
[Error - 11:20:21 PM] Request textDocument/rename failed.
  Message: gopls-rename-issue-repro/main.go:3:6: renaming this type "myData" to "data"
gopls-rename-issue-repro/main.go:7:13:	would cause this reference to become shadowed
gopls-rename-issue-repro/main.go:7:7:	by this intervening var definition
  Code: 0 

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeToolsThis 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

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions