]> Cypherpunks repositories - gostls13.git/commitdiff
gofmt: fix a couple of crashes, disallow rewrites for incomplete programs
authorRobert Griesemer <gri@golang.org>
Wed, 12 Oct 2011 04:49:53 +0000 (21:49 -0700)
committerRobert Griesemer <gri@golang.org>
Wed, 12 Oct 2011 04:49:53 +0000 (21:49 -0700)
The current implementation of formatting for incomplete programs
cannot tolerate program rewrites; ignore -rewrite in that case
with a warning message (temporary solution).

Fix a couple of crashes that were introduced recently.

Fixes #2348.

R=rsc
CC=golang-dev
https://golang.org/cl/5233054

src/cmd/gofmt/gofmt.go
src/cmd/gofmt/rewrite.go

index 1c0efb6db7f8dcc92aacfe5c429aee2a84363a77..6ce99113eddee18f584396122aa9de4ec8f196fa 100644 (file)
@@ -107,7 +107,11 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) os.Er
        }
 
        if rewrite != nil {
-               file = rewrite(file)
+               if adjust == nil {
+                       file = rewrite(file)
+               } else {
+                       fmt.Fprintf(os.Stderr, "warning: rewrite ignored for incomplete programs\n")
+               }
        }
 
        if *simplifyAST {
@@ -119,7 +123,10 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) os.Er
        if err != nil {
                return err
        }
-       res := adjust(src, buf.Bytes())
+       res := buf.Bytes()
+       if adjust != nil {
+               res = adjust(src, res)
+       }
 
        if !bytes.Equal(src, res) {
                // formatting has changed
@@ -252,8 +259,7 @@ func parse(filename string, src []byte, stdin bool) (*ast.File, func(orig, src [
        // Try as whole source file.
        file, err := parser.ParseFile(fset, filename, src, parserMode)
        if err == nil {
-               adjust := func(orig, src []byte) []byte { return src }
-               return file, adjust, nil
+               return file, nil, nil
        }
        // If the error is that the source file didn't begin with a
        // package line and this is standard input, fall through to
@@ -318,7 +324,10 @@ func cutSpace(b []byte) (before, middle, after []byte) {
        for j > 0 && (b[j-1] == ' ' || b[j-1] == '\t' || b[j-1] == '\n') {
                j--
        }
-       return b[:i], b[i:j], b[j:]
+       if i <= j {
+               return b[:i], b[i:j], b[j:]
+       }
+       return nil, nil, b[j:]
 }
 
 // matchSpace reformats src to use the same space context as orig.
index 3d74dea0f11959799816a15991583477b9ae67df..8f65ef1ff1e7ae4a6262babc74e45a4a43bfe53d 100644 (file)
@@ -85,7 +85,8 @@ func setValue(x, y reflect.Value) {
        }
        defer func() {
                if x := recover(); x != nil {
-                       if s, ok := x.(string); ok && strings.HasPrefix(s, "type mismatch") {
+                       if s, ok := x.(string); ok &&
+                               (strings.Contains(s, "type mismatch") || strings.Contains(s, "not assignable")) {
                                // x cannot be set to y - ignore this rewrite
                                return
                        }