From bd4139713dac599896ae86822707aae7d189993a Mon Sep 17 00:00:00 2001 From: SalvadorC Date: Fri, 21 Sep 2018 19:51:01 +0200 Subject: [PATCH] fix issue [#59] (#70) --- lint/package.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lint/package.go b/lint/package.go index 4dbaa03..7b6046f 100644 --- a/lint/package.go +++ b/lint/package.go @@ -77,7 +77,9 @@ func (p *Package) TypeCheck() error { anyFile = f astFiles = append(astFiles, f.AST) } - typesPkg, err := config.Check(anyFile.AST.Name.Name, p.fset, astFiles, info) + + typesPkg, err := check(config, anyFile.AST.Name.Name, p.fset, astFiles, info) + // Remember the typechecking info, even if config.Check failed, // since we will get partial information. p.TypesPkg = typesPkg @@ -86,6 +88,20 @@ func (p *Package) TypeCheck() error { return err } +// check function encapsulates the call to go/types.Config.Check method and +// recovers if the called method panics (see issue #59) +func check(config *types.Config, n string, fset *token.FileSet, astFiles []*ast.File, info *types.Info) (p *types.Package, err error) { + defer func() { + if r := recover(); r != nil { + err, _ = r.(error) + p = nil + return + } + }() + + return config.Check(n, fset, astFiles, info) +} + // TypeOf returns the type of an expression. func (p *Package) TypeOf(expr ast.Expr) types.Type { if p.TypesInfo == nil {