]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: make gotype continue after syntax errors
authorRobert Griesemer <gri@golang.org>
Wed, 14 Feb 2018 00:37:51 +0000 (16:37 -0800)
committerRobert Griesemer <gri@golang.org>
Wed, 14 Feb 2018 21:23:56 +0000 (21:23 +0000)
This avoids odd behavior where sometimes a lot of useful
errors are not reported simply because of a small syntax
error.

Tested manually with non-existing files. (We cannot easily
add an automatic test because this is a stand-alone binary
in this directory that must be built manually.)

Fixes #23593.

Change-Id: Iff90f95413bed7d1023fa0a5c9eb0414144428a9
Reviewed-on: https://go-review.googlesource.com/93815
Reviewed-by: Alan Donovan <adonovan@google.com>
src/go/types/gotype.go

index 196fc9bbd604162de61eb3dff70149a0ae40797b..2efb4c0ac9ef48772891bf423d3b51a77fbfeb35 100644 (file)
@@ -209,14 +209,30 @@ func parseFiles(dir string, filenames []string) ([]*ast.File, error) {
        }
        wg.Wait()
 
-       // if there are errors, return the first one for deterministic results
+       // If there are errors, return the first one for deterministic results.
+       var first error
        for _, err := range errors {
                if err != nil {
-                       return nil, err
+                       first = err
+                       // If we have an error, some files may be nil.
+                       // Remove them. (The go/parser always returns
+                       // a possibly partial AST even in the presence
+                       // of errors, except if the file doesn't exist
+                       // in the first place, in which case it cannot
+                       // matter.)
+                       i := 0
+                       for _, f := range files {
+                               if f != nil {
+                                       files[i] = f
+                                       i++
+                               }
+                       }
+                       files = files[:i]
+                       break
                }
        }
 
-       return files, nil
+       return files, first
 }
 
 func parseDir(dir string) ([]*ast.File, error) {
@@ -318,7 +334,7 @@ func main() {
        files, err := getPkgFiles(flag.Args())
        if err != nil {
                report(err)
-               os.Exit(2)
+               // ok to continue (files may be empty, but not nil)
        }
 
        checkPkgFiles(files)