1
0
mirror of https://github.com/golang/go synced 2024-11-26 18:06:55 -07:00

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

@ -17,29 +17,29 @@ import "C"
import "testing"
func test21809(t *testing.T) {
longVar := C.long(3)
typedefVar := C.MySigned_t(4)
typedefTypedefVar := C.MySigned2_t(5)
longVar := C.long(3)
typedefVar := C.MySigned_t(4)
typedefTypedefVar := C.MySigned2_t(5)
// all three should be considered identical to `long`
if ret := C.takes_long(longVar); ret != 9 {
t.Errorf("got %v but expected %v", ret, 9)
}
if ret := C.takes_long(typedefVar); ret != 16 {
t.Errorf("got %v but expected %v", ret, 16)
}
if ret := C.takes_long(typedefTypedefVar); ret != 25 {
t.Errorf("got %v but expected %v", ret, 25)
}
// all three should be considered identical to `long`
if ret := C.takes_long(longVar); ret != 9 {
t.Errorf("got %v but expected %v", ret, 9)
}
if ret := C.takes_long(typedefVar); ret != 16 {
t.Errorf("got %v but expected %v", ret, 16)
}
if ret := C.takes_long(typedefTypedefVar); ret != 25 {
t.Errorf("got %v but expected %v", ret, 25)
}
// They should also be identical to the typedef'd type
if ret := C.takes_typedef(longVar); ret != 9 {
t.Errorf("got %v but expected %v", ret, 9)
}
if ret := C.takes_typedef(typedefVar); ret != 16 {
t.Errorf("got %v but expected %v", ret, 16)
}
if ret := C.takes_typedef(typedefTypedefVar); ret != 25 {
t.Errorf("got %v but expected %v", ret, 25)
}
// They should also be identical to the typedef'd type
if ret := C.takes_typedef(longVar); ret != 9 {
t.Errorf("got %v but expected %v", ret, 9)
}
if ret := C.takes_typedef(typedefVar); ret != 16 {
t.Errorf("got %v but expected %v", ret, 16)
}
if ret := C.takes_typedef(typedefTypedefVar); ret != 25 {
t.Errorf("got %v but expected %v", ret, 25)
}
}

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

@ -14,4 +14,4 @@ func main() {
if a != 8 {
panic("FAIL")
}
}
}

View File

@ -574,7 +574,7 @@ Outer:
if !ok {
// First entry for this hash.
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
}
for _, n := range prev {

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 {
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 {
p.print(token.COLON)
// 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 y != nil {
p.expr0(y, depth+1)
if indexCount > 1 && hasBinaries {
needsBlanks = true
}
}
for i, x := range indices {
if i > 0 {
if indices[i-1] != nil && needsBlanks {
p.print(blank)
}
p.print(token.COLON)
if x != nil && needsBlanks {
p.print(blank)
}
}
if x != nil {
p.expr0(x, depth+1)
}
}
p.print(x.Rbrack, token.RBRACK)

View File

@ -122,18 +122,47 @@ func _() {
// slice expressions with cap
func _() {
_ = x[a:b:c]
_ = x[a:b : c+d]
_ = x[a : b : c+d]
_ = x[a : b+d : c]
_ = 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+d : c]
_ = x[a+d : b+d : c+d]
_ = x[:b:c]
_ = x[:b : c+d]
_ = x[:b+d : c]
_ = x[:b+d : c+d]
_ = x[: b : c+d]
_ = x[: b+d : c]
_ = 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 _() {

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

@ -122,18 +122,47 @@ func _() {
// slice expressions with cap
func _() {
_ = x[a:b:c]
_ = x[a:b : c+d]
_ = x[a : b : c+d]
_ = x[a : b+d : c]
_ = 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+d : c]
_ = x[a+d : b+d : c+d]
_ = x[:b:c]
_ = x[:b : c+d]
_ = x[:b+d : c]
_ = x[:b+d : c+d]
_ = x[: b : c+d]
_ = x[: b+d : c]
_ = 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 _() {