From 2ba079868204837891e531eecb4215eecfee8ff7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Oudompheng?= Date: Fri, 11 Nov 2011 14:11:30 -0800 Subject: [PATCH] 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 --- src/cmd/gofmt/gofmt_test.go | 1 + src/cmd/gofmt/rewrite.go | 4 ++-- src/cmd/gofmt/testdata/rewrite3.golden | 12 ++++++++++++ src/cmd/gofmt/testdata/rewrite3.input | 12 ++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 src/cmd/gofmt/testdata/rewrite3.golden create mode 100644 src/cmd/gofmt/testdata/rewrite3.input diff --git a/src/cmd/gofmt/gofmt_test.go b/src/cmd/gofmt/gofmt_test.go index 4432a178bc..303c4f1e1c 100644 --- a/src/cmd/gofmt/gofmt_test.go +++ b/src/cmd/gofmt/gofmt_test.go @@ -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", ""}, diff --git a/src/cmd/gofmt/rewrite.go b/src/cmd/gofmt/rewrite.go index 25049f8f8c..60a4a7b49f 100644 --- a/src/cmd/gofmt/rewrite.go +++ b/src/cmd/gofmt/rewrite.go @@ -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 index 0000000000..0d16d16011 --- /dev/null +++ b/src/cmd/gofmt/testdata/rewrite3.golden @@ -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 index 0000000000..0d16d16011 --- /dev/null +++ b/src/cmd/gofmt/testdata/rewrite3.input @@ -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 +}