Commit 5d1addf4 authored by griesemer's avatar griesemer Committed by Robert Griesemer

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: default avatarMatthew Dempsky <mdempsky@google.com>
parent 11f494f3
...@@ -8,5 +8,6 @@ import "C" ...@@ -8,5 +8,6 @@ import "C"
//export FromPkg //export FromPkg
func FromPkg() int32 { return 1024 } func FromPkg() int32 { return 1024 }
//export Divu //export Divu
func Divu(a, b uint32) uint32 { return a / b } func Divu(a, b uint32) uint32 { return a / b }
...@@ -574,7 +574,7 @@ Outer: ...@@ -574,7 +574,7 @@ Outer:
if !ok { if !ok {
// First entry for this hash. // First entry for this hash.
nn = append(nn, c.node) nn = append(nn, c.node)
seen[c.hash] = nn[len(nn)-1 : len(nn):len(nn)] seen[c.hash] = nn[len(nn)-1 : len(nn) : len(nn)]
continue continue
} }
for _, n := range prev { for _, n := range prev {
......
...@@ -774,20 +774,35 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) { ...@@ -774,20 +774,35 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
if x.Max != nil { if x.Max != nil {
indices = append(indices, x.Max) 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 { if i > 0 {
// blanks around ":" if both sides exist and either side is a binary expression if indices[i-1] != nil && needsBlanks {
// TODO(gri) once we have committed a variant of a[i:j:k] we may want to fine- p.print(blank)
// 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 {
p.print(token.COLON) p.print(token.COLON)
if x != nil && needsBlanks {
p.print(blank)
} }
} }
if y != nil { if x != nil {
p.expr0(y, depth+1) p.expr0(x, depth+1)
} }
} }
p.print(x.Rbrack, token.RBRACK) p.print(x.Rbrack, token.RBRACK)
......
...@@ -122,18 +122,47 @@ func _() { ...@@ -122,18 +122,47 @@ func _() {
// slice expressions with cap // slice expressions with cap
func _() { func _() {
_ = x[a:b:c] _ = x[a:b:c]
_ = x[a:b : c+d] _ = x[a : b : c+d]
_ = x[a : b+d : c] _ = x[a : b+d : c]
_ = x[a : b+d : c+d] _ = x[a : b+d : c+d]
_ = x[a+d : b:c] _ = x[a+d : b : c]
_ = x[a+d : b : c+d] _ = x[a+d : b : c+d]
_ = x[a+d : b+d : c] _ = x[a+d : b+d : c]
_ = x[a+d : b+d : c+d] _ = x[a+d : b+d : c+d]
_ = x[:b:c] _ = x[:b:c]
_ = x[:b : c+d] _ = x[: b : c+d]
_ = x[:b+d : c] _ = x[: b+d : c]
_ = x[:b+d : c+d] _ = 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 _() { func _() {
......
...@@ -138,6 +138,35 @@ func _() { ...@@ -138,6 +138,35 @@ func _() {
_ = x[:b+d:c+d] _ = 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 _() { func _() {
_ = a+b _ = a+b
_ = a+b+c _ = a+b+c
......
...@@ -122,18 +122,47 @@ func _() { ...@@ -122,18 +122,47 @@ func _() {
// slice expressions with cap // slice expressions with cap
func _() { func _() {
_ = x[a:b:c] _ = x[a:b:c]
_ = x[a:b : c+d] _ = x[a : b : c+d]
_ = x[a : b+d : c] _ = x[a : b+d : c]
_ = x[a : b+d : c+d] _ = x[a : b+d : c+d]
_ = x[a+d : b:c] _ = x[a+d : b : c]
_ = x[a+d : b : c+d] _ = x[a+d : b : c+d]
_ = x[a+d : b+d : c] _ = x[a+d : b+d : c]
_ = x[a+d : b+d : c+d] _ = x[a+d : b+d : c+d]
_ = x[:b:c] _ = x[:b:c]
_ = x[:b : c+d] _ = x[: b : c+d]
_ = x[:b+d : c] _ = x[: b+d : c]
_ = x[:b+d : c+d] _ = 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 _() { func _() {
......
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