From 02deb77f6db19f5894d836790a64bd4dea86feb0 Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Fri, 8 Sep 2017 11:47:16 -0700 Subject: [PATCH] cmd/compile: fix println() 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 Reviewed-by: Emmanuel Odeke TryBot-Result: Gobot Gobot --- src/cmd/compile/internal/gc/walk.go | 8 ++++---- test/fixedbugs/issue21808.go | 17 +++++++++++++++++ test/fixedbugs/issue21808.out | 3 +++ 3 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 test/fixedbugs/issue21808.go create mode 100644 test/fixedbugs/issue21808.out diff --git a/src/cmd/compile/internal/gc/walk.go b/src/cmd/compile/internal/gc/walk.go index 68cf12eed9..3fb57e123a 100644 --- a/src/cmd/compile/internal/gc/walk.go +++ b/src/cmd/compile/internal/gc/walk.go @@ -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 index 0000000000..d146200eae --- /dev/null +++ b/test/fixedbugs/issue21808.go @@ -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 index 0000000000..655da036b2 --- /dev/null +++ b/test/fixedbugs/issue21808.out @@ -0,0 +1,3 @@ +A + +B -- 2.50.0