1
0
mirror of https://github.com/golang/go synced 2024-11-19 10:24:43 -07:00
go/internal/lsp/testdata/rank/convert_rank.go.in
Muir Manders 9d59f9e855 internal/lsp: improve completion support for untyped constants
Add some extra smarts when evaluating untyped constants as completion
candidates. Previously we called types.Default() on the expected type
and candidate type, but this loses the untypedness of an untyped
constant which prevents it from being assignable to any type or named
type other than the untyped constant's default type.

Note that the added logic does not take into account the untyped
constant's value, so you will still get some false positive
completions (e.g. suggesting an untyped negative integer constant when
only a uint would do). Unfortunately go/types doesn't provide a way of
answering the question "is this *types.Const assignable to this
types.Type" since types.AssignableTo only considers a constant's type,
not its value.

Change-Id: If7075642e928f712b127256ae7706a5190e2f42c
GitHub-Last-Rev: 124d2f05b0aec09c9d7004d9da0d900524185b92
GitHub-Pull-Request: golang/tools#128
Reviewed-on: https://go-review.googlesource.com/c/tools/+/184477
Reviewed-by: Suzy Mueller <suzmue@golang.org>
2019-07-03 16:30:32 +00:00

40 lines
1.1 KiB
Go

package rank
func _() {
type strList []string
wantsStrList := func(strList) {}
var (
convA string //@item(convertA, "convA", "string", "var")
convB []string //@item(convertB, "convB", "[]string", "var")
)
wantsStrList(strList(conv)) //@complete("))", convertB, convertA)
}
func _() {
const (
convC = "hi" //@item(convertC, "convC", "string", "const")
convD = 123 //@item(convertD, "convD", "int", "const")
convE int = 123 //@item(convertE, "convE", "int", "const")
convF string = "there" //@item(convertF, "convF", "string", "const")
)
var foo int
foo = conv //@complete(" //", convertD, convertE, convertC, convertF)
type myInt int
var mi myInt
mi = conv //@complete(" //", convertD, convertC, convertE, convertF)
mi + conv //@complete(" //", convertD, convertC, convertE, convertF)
1 + conv //@complete(" //", convertD, convertE, convertC, convertF)
type myString string
var ms myString
ms = conv //@complete(" //", convertC, convertD, convertE, convertF)
type myUint uint32
var mu myUint
mu = conv //@complete(" //", convertD, convertC, convertE, convertF)
}