From ef80cdb6ec6d94b8e8d05f986b368bf925fa330e Mon Sep 17 00:00:00 2001 From: Muir Manders Date: Wed, 3 Feb 2021 13:24:46 -0800 Subject: [PATCH] internal/lsp/completion: fix untyped int type inference For index expressions, optional "make" args, and composite literal slice/array keys, we were inferring an expected type of int instead of untyped int. This caused candidate rankings to not be quite right in general, and in particular, after support for automatic type conversions was added, the issue manifested as: var foo []int var bar int32 foo[ba<>] // completed to "int(bar)" instead of "bar" Fixes golang/go#43375. Change-Id: I6daef7d23b767f296bdbbc8f47f5b2c972ad9b80 Reviewed-on: https://go-review.googlesource.com/c/tools/+/289272 Run-TryBot: Rebecca Stambler gopls-CI: kokoro TryBot-Result: Go Bot Reviewed-by: Rebecca Stambler Trust: Rebecca Stambler Trust: Hyang-Ah Hana Kim --- internal/lsp/source/completion/builtin.go | 2 +- internal/lsp/source/completion/completion.go | 8 ++++---- internal/lsp/testdata/builtins/builtin_args.go | 4 ++++ internal/lsp/testdata/index/index.go | 4 ++++ internal/lsp/testdata/summary.txt.golden | 2 +- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/internal/lsp/source/completion/builtin.go b/internal/lsp/source/completion/builtin.go index f4137b3a86..2b59a92037 100644 --- a/internal/lsp/source/completion/builtin.go +++ b/internal/lsp/source/completion/builtin.go @@ -127,7 +127,7 @@ func (c *completer) builtinArgType(obj types.Object, call *ast.CallExpr, parentI inf.typeName.wantTypeName = true inf.objType = parentInf.objType } else { - inf.objType = types.Typ[types.Int] + inf.objType = types.Typ[types.UntypedInt] } } diff --git a/internal/lsp/source/completion/completion.go b/internal/lsp/source/completion/completion.go index 20f30dad6c..9ebdc1c137 100644 --- a/internal/lsp/source/completion/completion.go +++ b/internal/lsp/source/completion/completion.go @@ -1690,12 +1690,12 @@ func (c *completer) expectedCompositeLiteralType() types.Type { switch t := clInfo.clType.(type) { case *types.Slice: if clInfo.inKey { - return types.Typ[types.Int] + return types.Typ[types.UntypedInt] } return t.Elem() case *types.Array: if clInfo.inKey { - return types.Typ[types.Int] + return types.Typ[types.UntypedInt] } return t.Elem() case *types.Map: @@ -2038,7 +2038,7 @@ Nodes: case *ast.SliceExpr: // Make sure position falls within the brackets (e.g. "foo[a:<>]"). if node.Lbrack < c.pos && c.pos <= node.Rbrack { - inf.objType = types.Typ[types.Int] + inf.objType = types.Typ[types.UntypedInt] } return inf case *ast.IndexExpr: @@ -2049,7 +2049,7 @@ Nodes: case *types.Map: inf.objType = t.Key() case *types.Slice, *types.Array: - inf.objType = types.Typ[types.Int] + inf.objType = types.Typ[types.UntypedInt] } } } diff --git a/internal/lsp/testdata/builtins/builtin_args.go b/internal/lsp/testdata/builtins/builtin_args.go index 4556021760..052777fe90 100644 --- a/internal/lsp/testdata/builtins/builtin_args.go +++ b/internal/lsp/testdata/builtins/builtin_args.go @@ -43,6 +43,10 @@ func _() { make() //@rank(")", builtinMapType, int),rank(")", builtinChanType, int),rank(")", builtinSliceType, int),rank(")", builtinMapType, int) make(aSliceType, a) //@rank(")", builtinInt, builtinSlice) + type myInt int + var mi myInt //@item(builtinMyInt, "mi", "myInt", "var") + make(aSliceType, m) //@snippet(")", builtinMyInt, "mi", "mi") + var _ []int = make() //@rank(")", builtinSliceType, builtinMapType) type myStruct struct{} //@item(builtinStructType, "myStruct", "struct{...}", "struct") diff --git a/internal/lsp/testdata/index/index.go b/internal/lsp/testdata/index/index.go index 7e56b51128..a2656893c9 100644 --- a/internal/lsp/testdata/index/index.go +++ b/internal/lsp/testdata/index/index.go @@ -18,4 +18,8 @@ func _() { type myMap map[string]int var baz myMap baz[a] //@complete("]", indexAA, indexAB) + + type myInt int + var mi myInt //@item(indexMyInt, "mi", "myInt", "var") + foo[m] //@snippet("]", indexMyInt, "mi", "mi") } diff --git a/internal/lsp/testdata/summary.txt.golden b/internal/lsp/testdata/summary.txt.golden index 1ac9c9b7a4..f9b6c383e8 100644 --- a/internal/lsp/testdata/summary.txt.golden +++ b/internal/lsp/testdata/summary.txt.golden @@ -2,7 +2,7 @@ CallHierarchyCount = 2 CodeLensCount = 5 CompletionsCount = 258 -CompletionSnippetCount = 90 +CompletionSnippetCount = 92 UnimportedCompletionsCount = 5 DeepCompletionsCount = 5 FuzzyCompletionsCount = 8