Commit 2ba07986 authored by Rémy Oudompheng's avatar Rémy Oudompheng Committed by Robert Griesemer

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
parent 5fc3771b
...@@ -76,6 +76,7 @@ var tests = []struct { ...@@ -76,6 +76,7 @@ var tests = []struct {
{"testdata/old.input", ""}, {"testdata/old.input", ""},
{"testdata/rewrite1.input", "-r=Foo->Bar"}, {"testdata/rewrite1.input", "-r=Foo->Bar"},
{"testdata/rewrite2.input", "-r=int->bool"}, {"testdata/rewrite2.input", "-r=int->bool"},
{"testdata/rewrite3.input", "-r=x->x"},
{"testdata/stdin*.input", "-stdin"}, {"testdata/stdin*.input", "-stdin"},
{"testdata/comments.input", ""}, {"testdata/comments.input", ""},
{"testdata/import.input", ""}, {"testdata/import.input", ""},
......
...@@ -159,8 +159,8 @@ func match(m map[string]reflect.Value, pattern, val reflect.Value) bool { ...@@ -159,8 +159,8 @@ func match(m map[string]reflect.Value, pattern, val reflect.Value) bool {
if m != nil && pattern.IsValid() && pattern.Type() == identType { if m != nil && pattern.IsValid() && pattern.Type() == identType {
name := pattern.Interface().(*ast.Ident).Name name := pattern.Interface().(*ast.Ident).Name
if isWildcard(name) && val.IsValid() { if isWildcard(name) && val.IsValid() {
// wildcards only match expressions // wildcards only match valid (non-nil) expressions.
if _, ok := val.Interface().(ast.Expr); ok { if _, ok := val.Interface().(ast.Expr); ok && !val.IsNil() {
if old, ok := m[name]; ok { if old, ok := m[name]; ok {
return match(nil, old, val) return match(nil, old, val)
} }
......
// 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
}
// 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
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment