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

Open
cespare opened this issue Dec 25, 2020 · 1 comment

Comments

@cespare
Copy link
Contributor

@cespare 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

@muirdm
Copy link

@muirdm 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.