]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix println()
authorKeith Randall <khr@golang.org>
Fri, 8 Sep 2017 18:47:16 +0000 (11:47 -0700)
committerKeith Randall <khr@golang.org>
Fri, 8 Sep 2017 20:10:48 +0000 (20:10 +0000)
println with no arguments accidentally doesn't print a newline.

Introduced at CL 55097

Fixes #21808

Change-Id: I9fc7b4271b9b31e4c9b6078f055195dc3907b62c
Reviewed-on: https://go-review.googlesource.com/62390
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/compile/internal/gc/walk.go
test/fixedbugs/issue21808.go [new file with mode: 0644]
test/fixedbugs/issue21808.out [new file with mode: 0644]

index 68cf12eed9c1538c8069d1f244930a39864b1201..3fb57e123a8619dcdb9537635be0e51f5dba575e 100644 (file)
@@ -2071,12 +2071,12 @@ func walkprint(nn *Node, init *Nodes) *Node {
                s := nn.List.Slice()
                t := make([]*Node, 0, len(s)*2)
                for i, n := range s {
-                       x := " "
-                       if len(s)-1 == i {
-                               x = "\n"
+                       t = append(t, n)
+                       if i != len(s)-1 {
+                               t = append(t, nodstr(" "))
                        }
-                       t = append(t, n, nodstr(x))
                }
+               t = append(t, nodstr("\n"))
                nn.List.Set(t)
        }
 
diff --git a/test/fixedbugs/issue21808.go b/test/fixedbugs/issue21808.go
new file mode 100644 (file)
index 0000000..d146200
--- /dev/null
@@ -0,0 +1,17 @@
+// run
+
+// Copyright 2017 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.
+
+// Make sure println() prints a blank line.
+
+package main
+
+import "fmt"
+
+func main() {
+       fmt.Println("A")
+       println()
+       fmt.Println("B")
+}
diff --git a/test/fixedbugs/issue21808.out b/test/fixedbugs/issue21808.out
new file mode 100644 (file)
index 0000000..655da03
--- /dev/null
@@ -0,0 +1,3 @@
+A
+
+B