go/types: add missing nil check in Info.TypeOf

Also use type assertions in a more defensive
way (check for != nil rather than ok).

LGTM=dsymonds, adonovan
R=adonovan, dsymonds
CC=golang-codereviews
https://golang.org/cl/169480043
This commit is contained in:
Robert Griesemer 2014-11-12 15:39:43 -08:00
parent b8a5fcfcec
commit 932b62a02f
1 changed files with 5 additions and 3 deletions

View File

@ -215,8 +215,10 @@ func (info *Info) TypeOf(e ast.Expr) Type {
if t, ok := info.Types[e]; ok { if t, ok := info.Types[e]; ok {
return t.Type return t.Type
} }
if id, ok := e.(*ast.Ident); ok { if id, _ := e.(*ast.Ident); id != nil {
return info.ObjectOf(id).Type() if obj := info.ObjectOf(id); obj != nil {
return obj.Type()
}
} }
return nil return nil
} }
@ -230,7 +232,7 @@ func (info *Info) TypeOf(e ast.Expr) Type {
// Precondition: the Uses and Defs maps are populated. // Precondition: the Uses and Defs maps are populated.
// //
func (info *Info) ObjectOf(id *ast.Ident) Object { func (info *Info) ObjectOf(id *ast.Ident) Object {
if obj, ok := info.Defs[id]; ok { if obj, _ := info.Defs[id]; obj != nil {
return obj return obj
} }
return info.Uses[id] return info.Uses[id]