From f9379e5b1f88898c255ae48ea60dfa733831dd8c Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Fri, 12 Jun 2015 18:23:47 -0700 Subject: [PATCH] go/types: be robust in the presence of incorrect/missing position info Fixes go/loader test crash. TBR: adonovan Change-Id: I91dba5e001afa0ee188ccea4db904a6ce744c4d0 Reviewed-on: https://go-review.googlesource.com/11042 Reviewed-by: Robert Griesemer --- go/types/resolver.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/go/types/resolver.go b/go/types/resolver.go index 628da4a655d..374ffc28005 100644 --- a/go/types/resolver.go +++ b/go/types/resolver.go @@ -155,8 +155,12 @@ func (check *Checker) collectObjects() { // Use the actual source file extent rather than *ast.File extent since the // latter doesn't include comments which appear at the start or end of the file. - f := check.fset.File(file.Pos()) - fileScope := NewScope(check.pkg.scope, token.Pos(f.Base()), token.Pos(f.Base()+f.Size()), check.filename(fileNo)) + // Be conservative and use the *ast.File extent if we don't have a *token.File. + pos, end := file.Pos(), file.End() + if f := check.fset.File(file.Pos()); f != nil { + pos, end = token.Pos(f.Base()), token.Pos(f.Base()+f.Size()) + } + fileScope := NewScope(check.pkg.scope, pos, end, check.filename(fileNo)) check.recordScope(file, fileScope) for _, decl := range file.Decls {