oracle: describe: fix crash on bad ImportSpec

+ Test

Fixes issue #13380

Change-Id: I6d738756017f83a296bde6a04845cd985a351409
Reviewed-on: https://go-review.googlesource.com/18210
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Alan Donovan 2015-12-30 10:37:15 -05:00
parent d331899c12
commit 22501a76a1
3 changed files with 22 additions and 4 deletions

View File

@ -513,11 +513,15 @@ func describePackage(qpos *queryPos, path []ast.Node) (*describePackageResult, e
var pkg *types.Package
switch n := path[0].(type) {
case *ast.ImportSpec:
var pkgname *types.PkgName
var obj types.Object
if n.Name != nil {
pkgname = qpos.info.Defs[n.Name].(*types.PkgName)
} else if p := qpos.info.Implicits[n]; p != nil {
pkgname = p.(*types.PkgName)
obj = qpos.info.Defs[n.Name]
} else {
obj = qpos.info.Implicits[n]
}
pkgname, _ := obj.(*types.PkgName)
if pkgname == nil {
return nil, fmt.Errorf("can't import package %s", n.Path.Value)
}
pkg = pkgname.Imported()
description = fmt.Sprintf("import of package %q", pkg.Path())

View File

@ -6,6 +6,14 @@ package describe // @describe pkgdecl "describe"
// TODO(adonovan): more coverage of the (extensive) logic.
import (
"nosuchpkg" // @describe badimport1 "nosuchpkg"
nosuchpkg2 "nosuchpkg" // @describe badimport2 "nosuchpkg2"
)
var _ nosuchpkg.T
var _ nosuchpkg2.T
type cake float64 // @describe type-ref-builtin "float64"
const c = iota // @describe const-ref-iota "iota"

View File

@ -13,6 +13,12 @@ definition of package "describe"
const pi untyped float = 3141/1000
const pie cake = 1768225803696341/562949953421312
-------- @describe badimport1 --------
Error: can't import package "nosuchpkg"
-------- @describe badimport2 --------
Error: can't import package "nosuchpkg"
-------- @describe type-ref-builtin --------
reference to built-in type float64