aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/gofmt
diff options
context:
space:
mode:
authorDominik Honnef <dominik@honnef.co>2019-07-28 15:19:43 +0200
committerRobert Griesemer <gri@golang.org>2019-09-11 20:34:54 +0000
commitd5df4d61ce87e6ba9ba0bad06b835e0b100660e7 (patch)
tree041e7864794787f0ae254cc517b274920402c7ce /src/cmd/gofmt
parent8875fb97c5cadbc6f02e4ce89efa586023c0a777 (diff)
downloadgo-d5df4d61ce87e6ba9ba0bad06b835e0b100660e7.tar.gz
go-d5df4d61ce87e6ba9ba0bad06b835e0b100660e7.zip
cmd/gofmt: don't turn nil slices into empty slices during rewriting
The go/ast package uses and guarantees nil slices for optional elements that weren't present in the parsed source code, such as the list of return values of a function. Packages using go/ast rely on this attribute and check for nils explicitly. One such package is go/printer. In the presence of empty slices instead of nil slices, it generates invalid code, such as "case :" instead of "default:". The issues that this CL fixes are all manifestations of that problem, each for a different syntactic element. Fixes #33103 Fixes #33104 Fixes #33105 Change-Id: I219f95a7da820eaf697a4ee227d458ab6e4a80bd Reviewed-on: https://go-review.googlesource.com/c/go/+/187917 Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Robert Griesemer <gri@golang.org> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/cmd/gofmt')
-rw-r--r--src/cmd/gofmt/rewrite.go6
-rw-r--r--src/cmd/gofmt/testdata/rewrite10.golden19
-rw-r--r--src/cmd/gofmt/testdata/rewrite10.input19
3 files changed, 44 insertions, 0 deletions
diff --git a/src/cmd/gofmt/rewrite.go b/src/cmd/gofmt/rewrite.go
index 79b7858a5a..bab22e04cd 100644
--- a/src/cmd/gofmt/rewrite.go
+++ b/src/cmd/gofmt/rewrite.go
@@ -271,6 +271,12 @@ func subst(m map[string]reflect.Value, pattern reflect.Value, pos reflect.Value)
// Otherwise copy.
switch p := pattern; p.Kind() {
case reflect.Slice:
+ if p.IsNil() {
+ // Do not turn nil slices into empty slices. go/ast
+ // guarantees that certain lists will be nil if not
+ // populated.
+ return reflect.Zero(p.Type())
+ }
v := reflect.MakeSlice(p.Type(), p.Len(), p.Len())
for i := 0; i < p.Len(); i++ {
v.Index(i).Set(subst(m, p.Index(i), pos))
diff --git a/src/cmd/gofmt/testdata/rewrite10.golden b/src/cmd/gofmt/testdata/rewrite10.golden
new file mode 100644
index 0000000000..1dd781fbb0
--- /dev/null
+++ b/src/cmd/gofmt/testdata/rewrite10.golden
@@ -0,0 +1,19 @@
+//gofmt -r=a->a
+
+// Copyright 2019 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.
+
+// Issue 33103, 33104, and 33105.
+
+package pkg
+
+func fn() {
+ _ = func() {
+ switch {
+ default:
+ }
+ }
+ _ = func() string {}
+ _ = func() { var ptr *string; println(ptr) }
+}
diff --git a/src/cmd/gofmt/testdata/rewrite10.input b/src/cmd/gofmt/testdata/rewrite10.input
new file mode 100644
index 0000000000..1dd781fbb0
--- /dev/null
+++ b/src/cmd/gofmt/testdata/rewrite10.input
@@ -0,0 +1,19 @@
+//gofmt -r=a->a
+
+// Copyright 2019 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.
+
+// Issue 33103, 33104, and 33105.
+
+package pkg
+
+func fn() {
+ _ = func() {
+ switch {
+ default:
+ }
+ }
+ _ = func() string {}
+ _ = func() { var ptr *string; println(ptr) }
+}