go/analysis: avoid panic in ifaceassert

Presently ifaceassert.go panics if given asserts whose
types reduce to
[]interface{}.(string)
which is illegal Go. Its tests won't even run on such code, but
gopls will happily invoke it. This CL adds a test for nil.

See golang/go#42857

Change-Id: I2791f4bd0b58559e65e6590822ac8f4123989273
Reviewed-on: https://go-review.googlesource.com/c/tools/+/273766
Reviewed-by: Michael Matloob <matloob@golang.org>
Trust: Peter Weinberger <pjw@google.com>
This commit is contained in:
Peter Weinbergr 2020-11-27 07:51:10 -05:00 committed by Peter Weinberger
parent 0e8b1ee181
commit fd5f293690
1 changed files with 4 additions and 0 deletions

View File

@ -41,6 +41,10 @@ var Analyzer = &analysis.Analyzer{
// assertableTo checks whether interface v can be asserted into t. It returns
// nil on success, or the first conflicting method on failure.
func assertableTo(v, t types.Type) *types.Func {
if t == nil || v == nil {
// not assertable to, but there is no missing method
return nil
}
// ensure that v and t are interfaces
V, _ := v.Underlying().(*types.Interface)
T, _ := t.Underlying().(*types.Interface)