mirror of
https://github.com/golang/go
synced 2024-11-25 06:57:58 -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:
parent
187c3536a8
commit
e9ba607bf5
@ -107,7 +107,11 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) os.Er
|
|||||||
}
|
}
|
||||||
|
|
||||||
if rewrite != nil {
|
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 {
|
if *simplifyAST {
|
||||||
@ -119,7 +123,10 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) os.Er
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
res := adjust(src, buf.Bytes())
|
res := buf.Bytes()
|
||||||
|
if adjust != nil {
|
||||||
|
res = adjust(src, res)
|
||||||
|
}
|
||||||
|
|
||||||
if !bytes.Equal(src, res) {
|
if !bytes.Equal(src, res) {
|
||||||
// formatting has changed
|
// 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.
|
// Try as whole source file.
|
||||||
file, err := parser.ParseFile(fset, filename, src, parserMode)
|
file, err := parser.ParseFile(fset, filename, src, parserMode)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
adjust := func(orig, src []byte) []byte { return src }
|
return file, nil, nil
|
||||||
return file, adjust, nil
|
|
||||||
}
|
}
|
||||||
// If the error is that the source file didn't begin with a
|
// If the error is that the source file didn't begin with a
|
||||||
// package line and this is standard input, fall through to
|
// 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') {
|
for j > 0 && (b[j-1] == ' ' || b[j-1] == '\t' || b[j-1] == '\n') {
|
||||||
j--
|
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.
|
// matchSpace reformats src to use the same space context as orig.
|
||||||
|
@ -85,7 +85,8 @@ func setValue(x, y reflect.Value) {
|
|||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
if x := recover(); x != nil {
|
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
|
// x cannot be set to y - ignore this rewrite
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user