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: understand that defined integer types may be slice indexes when suggesting completions #43375

Closed
cespare opened this issue Dec 25, 2020 · 2 comments
Labels
FeatureRequest FrozenDueToAge gopls Issues related to the Go language server, gopls. help wanted Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@cespare
Copy link
Contributor

cespare commented Dec 25, 2020

What version of Go are you using (go version)?

$ go version
go version go1.15.6 linux/amd64

Does this issue reproduce with the latest release?

Yes, and with the current tip of gopls as well.

What operating system and processor architecture are you using (go env)?

linux/amd64

What did you do?

I used a variable of a defined integer type to index into a slice. Specifically, I tried to use autocompletion to fill in the variable in the index expression.

Here's some example code in a single-file package:

package main

type myInt int

func main() {
	nnn := myInt(3)
	mmm := 3
	x := make([]string, 5)
	x[|] = "hi"
}

The | is where my cursor was when I invoked autocompletion.

What did you expect to see?

I expected to see nnn and mmm equally suggested as possible completions.

If I selected nnn from the list, the completed code should be

x[nnn] = "hi"

What did you see instead?

nnn is ranked much lower than mmm -- it comes after some irrelevant entries from other packages (I filed #43374 for that).

If I nevertheless scroll down to nnn and select it, nnn is wrapped in an unnecessary int conversion:

x[int(nnn)] = "hi"

I noticed this in my usual environment of vim + govim but I can reproduce this with vscode as well. Here are some screenshots.

screen_20201225000158

screen_20201225000018

@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 Dec 25, 2020
@gopherbot gopherbot added this to the Unreleased milestone Dec 25, 2020
@muirdm
Copy link

muirdm commented Jan 2, 2021

The issue seems to be gopls infers the expected type at x[<>] as int instead of untyped int (i.e. types.Typ[types.Int] instead of types.Typ[types.UntypedInt]). This issue appears in a few more places around the completion code.

@gopherbot
Copy link

Change https://golang.org/cl/289272 mentions this issue: internal/lsp/completion: fix untyped int type inference

@golang golang locked and limited conversation to collaborators Feb 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FeatureRequest FrozenDueToAge gopls Issues related to the Go language server, gopls. help wanted Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants