1
0
mirror of https://github.com/golang/go synced 2024-11-20 01:04:40 -07:00

gofmt: leave nil nodes of the AST unchanged.

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
This commit is contained in:
Rémy Oudompheng 2011-11-11 14:11:30 -08:00 committed by Robert Griesemer
parent 5fc3771b3a
commit 2ba0798682
4 changed files with 27 additions and 2 deletions

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

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

12
src/cmd/gofmt/testdata/rewrite3.golden vendored Normal file
View File

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

12
src/cmd/gofmt/testdata/rewrite3.input vendored Normal file
View File

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