]> Cypherpunks repositories - gostls13.git/commitdiff
gofmt: leave nil nodes of the AST unchanged.
authorRémy Oudompheng <oudomphe@phare.normalesup.org>
Fri, 11 Nov 2011 22:11:30 +0000 (14:11 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 11 Nov 2011 22:11:30 +0000 (14:11 -0800)
Without this check, gofmt panics when trying to apply
the identity transformation on "item.field" expressions.
Fixes #2410.

R=rsc, gri
CC=golang-dev, remy
https://golang.org/cl/5376061

src/cmd/gofmt/gofmt_test.go
src/cmd/gofmt/rewrite.go
src/cmd/gofmt/testdata/rewrite3.golden [new file with mode: 0644]
src/cmd/gofmt/testdata/rewrite3.input [new file with mode: 0644]

index 4432a178bcf5e075deef54ddce29c848e336055a..303c4f1e1c2abb290505560dd3cd1294dc47850e 100644 (file)
@@ -76,6 +76,7 @@ var tests = []struct {
        {"testdata/old.input", ""},
        {"testdata/rewrite1.input", "-r=Foo->Bar"},
        {"testdata/rewrite2.input", "-r=int->bool"},
+       {"testdata/rewrite3.input", "-r=x->x"},
        {"testdata/stdin*.input", "-stdin"},
        {"testdata/comments.input", ""},
        {"testdata/import.input", ""},
index 25049f8f8c076454ba318f2c5a5db6c3065b502e..60a4a7b49f95cfdfcf977ab6d6514375026955eb 100644 (file)
@@ -159,8 +159,8 @@ func match(m map[string]reflect.Value, pattern, val reflect.Value) bool {
        if m != nil && pattern.IsValid() && pattern.Type() == identType {
                name := pattern.Interface().(*ast.Ident).Name
                if isWildcard(name) && val.IsValid() {
-                       // wildcards only match expressions
-                       if _, ok := val.Interface().(ast.Expr); ok {
+                       // wildcards only match valid (non-nil) expressions.
+                       if _, ok := val.Interface().(ast.Expr); ok && !val.IsNil() {
                                if old, ok := m[name]; ok {
                                        return match(nil, old, val)
                                }
diff --git a/src/cmd/gofmt/testdata/rewrite3.golden b/src/cmd/gofmt/testdata/rewrite3.golden
new file mode 100644 (file)
index 0000000..0d16d16
--- /dev/null
@@ -0,0 +1,12 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+// Field tags are *ast.BasicLit nodes that are nil when the tag is
+// absent. These nil nodes must not be mistaken for expressions,
+// the rewriter should not try to dereference them. Was issue 2410.
+type Foo struct {
+       Field int
+}
diff --git a/src/cmd/gofmt/testdata/rewrite3.input b/src/cmd/gofmt/testdata/rewrite3.input
new file mode 100644 (file)
index 0000000..0d16d16
--- /dev/null
@@ -0,0 +1,12 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+// Field tags are *ast.BasicLit nodes that are nil when the tag is
+// absent. These nil nodes must not be mistaken for expressions,
+// the rewriter should not try to dereference them. Was issue 2410.
+type Foo struct {
+       Field int
+}