mirror of
https://github.com/golang/go
synced 2024-11-18 08:14:41 -07:00
internal/lsp/source: fix panic in formatZeroValue for invalid type
formatZeroValue is currently only used when formatting return values for statement completion. Per golang/go#40956, it must be possible to hit this codepath with an invalid type. In this case, the empty string seems like a reasonable value. Perhaps we could do better, but fix the panic for now. Fixes golang/go#40956 Change-Id: I45b559d41001c857cef34aea2a5ac4a9096fe950 Reviewed-on: https://go-review.googlesource.com/c/tools/+/249818 Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
parent
74543c4034
commit
daa6538899
@ -582,7 +582,8 @@ func prevStmt(pos token.Pos, path []ast.Node) ast.Stmt {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// formatZeroValue produces Go code representing the zero value of T.
|
// formatZeroValue produces Go code representing the zero value of T. It
|
||||||
|
// returns the empty string if T is invalid.
|
||||||
func formatZeroValue(T types.Type, qf types.Qualifier) string {
|
func formatZeroValue(T types.Type, qf types.Qualifier) string {
|
||||||
switch u := T.Underlying().(type) {
|
switch u := T.Underlying().(type) {
|
||||||
case *types.Basic:
|
case *types.Basic:
|
||||||
@ -594,7 +595,7 @@ func formatZeroValue(T types.Type, qf types.Qualifier) string {
|
|||||||
case u.Info()&types.IsBoolean > 0:
|
case u.Info()&types.IsBoolean > 0:
|
||||||
return "false"
|
return "false"
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("unhandled basic type: %v", u))
|
return ""
|
||||||
}
|
}
|
||||||
case *types.Pointer, *types.Interface, *types.Chan, *types.Map, *types.Slice, *types.Signature:
|
case *types.Pointer, *types.Interface, *types.Chan, *types.Map, *types.Slice, *types.Signature:
|
||||||
return "nil"
|
return "nil"
|
||||||
|
28
internal/lsp/source/util_test.go
Normal file
28
internal/lsp/source/util_test.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
// Copyright 2020 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package source
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go/types"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFormatZeroValue(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
typ types.Type
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{types.Typ[types.String], `""`},
|
||||||
|
{types.Typ[types.Byte], "0"},
|
||||||
|
{types.Typ[types.Invalid], ""},
|
||||||
|
{types.Universe.Lookup("error").Type(), "nil"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
if got := formatZeroValue(test.typ, nil); got != test.want {
|
||||||
|
t.Errorf("formatZeroValue(%v) = %q, want %q", test.typ, got, test.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user