1
0
mirror of https://github.com/golang/go synced 2024-11-22 03:34:40 -07:00

gofmt: fix a couple of crashes, disallow rewrites for incomplete programs

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
This commit is contained in:
Robert Griesemer 2011-10-11 21:49:53 -07:00
parent 187c3536a8
commit e9ba607bf5
2 changed files with 16 additions and 6 deletions

View File

@ -107,7 +107,11 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) os.Er
}
if rewrite != nil {
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,8 +324,11 @@ func cutSpace(b []byte) (before, middle, after []byte) {
for j > 0 && (b[j-1] == ' ' || b[j-1] == '\t' || b[j-1] == '\n') {
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.
// 1) If orig begins with blank lines, matchSpace inserts them at the beginning of src.

View 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
}