go/printer: fix formatting of three-index slice expression

Apply gofmt to src, misc.

Fixes #22111.

Change-Id: Ib1bda0caaf2c1787a8137b7a61bbef7a341cc68c
Reviewed-on: https://go-review.googlesource.com/67633
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
griesemer 2017-10-02 16:23:25 -07:00 committed by Robert Griesemer
parent 11f494f37e
commit 5d1addf45d
8 changed files with 150 additions and 47 deletions

View File

@ -8,5 +8,6 @@ import "C"
//export FromPkg
func FromPkg() int32 { return 1024 }
//export Divu
func Divu(a, b uint32) uint32 { return a / b }

View File

@ -774,20 +774,35 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
if x.Max != nil {
indices = append(indices, x.Max)
}
for i, y := range indices {
// determine if we need extra blanks around ':'
var needsBlanks bool
if depth <= 1 {
var indexCount int
var hasBinaries bool
for _, x := range indices {
if x != nil {
indexCount++
if isBinary(x) {
hasBinaries = true
}
}
}
if indexCount > 1 && hasBinaries {
needsBlanks = true
}
}
for i, x := range indices {
if i > 0 {
// blanks around ":" if both sides exist and either side is a binary expression
// TODO(gri) once we have committed a variant of a[i:j:k] we may want to fine-
// tune the formatting here
x := indices[i-1]
if depth <= 1 && x != nil && y != nil && (isBinary(x) || isBinary(y)) {
p.print(blank, token.COLON, blank)
} else {
if indices[i-1] != nil && needsBlanks {
p.print(blank)
}
p.print(token.COLON)
if x != nil && needsBlanks {
p.print(blank)
}
}
if y != nil {
p.expr0(y, depth+1)
if x != nil {
p.expr0(x, depth+1)
}
}
p.print(x.Rbrack, token.RBRACK)

View File

@ -136,6 +136,35 @@ func _() {
_ = x[: b+d : c+d]
}
func issue22111() {
_ = x[:]
_ = x[:b]
_ = x[:b+1]
_ = x[a:]
_ = x[a+1:]
_ = x[a:b]
_ = x[a+1 : b]
_ = x[a : b+1]
_ = x[a+1 : b+1]
_ = x[:b:c]
_ = x[: b+1 : c]
_ = x[: b : c+1]
_ = x[: b+1 : c+1]
_ = x[a:b:c]
_ = x[a+1 : b : c]
_ = x[a : b+1 : c]
_ = x[a+1 : b+1 : c]
_ = x[a : b : c+1]
_ = x[a+1 : b : c+1]
_ = x[a : b+1 : c+1]
_ = x[a+1 : b+1 : c+1]
}
func _() {
_ = a + b
_ = a + b + c

View File

@ -138,6 +138,35 @@ func _() {
_ = x[:b+d:c+d]
}
func issue22111() {
_ = x[:]
_ = x[:b]
_ = x[:b+1]
_ = x[a:]
_ = x[a+1:]
_ = x[a:b]
_ = x[a+1:b]
_ = x[a:b+1]
_ = x[a+1:b+1]
_ = x[:b:c]
_ = x[:b+1:c]
_ = x[:b:c+1]
_ = x[:b+1:c+1]
_ = x[a:b:c]
_ = x[a+1:b:c]
_ = x[a:b+1:c]
_ = x[a+1:b+1:c]
_ = x[a:b:c+1]
_ = x[a+1:b:c+1]
_ = x[a:b+1:c+1]
_ = x[a+1:b+1:c+1]
}
func _() {
_ = a+b
_ = a+b+c

View File

@ -136,6 +136,35 @@ func _() {
_ = x[: b+d : c+d]
}
func issue22111() {
_ = x[:]
_ = x[:b]
_ = x[:b+1]
_ = x[a:]
_ = x[a+1:]
_ = x[a:b]
_ = x[a+1 : b]
_ = x[a : b+1]
_ = x[a+1 : b+1]
_ = x[:b:c]
_ = x[: b+1 : c]
_ = x[: b : c+1]
_ = x[: b+1 : c+1]
_ = x[a:b:c]
_ = x[a+1 : b : c]
_ = x[a : b+1 : c]
_ = x[a+1 : b+1 : c]
_ = x[a : b : c+1]
_ = x[a+1 : b : c+1]
_ = x[a : b+1 : c+1]
_ = x[a+1 : b+1 : c+1]
}
func _() {
_ = a + b
_ = a + b + c