1
0
mirror of https://github.com/golang/go synced 2024-11-19 21:14:43 -07:00

go/printer: don't reduce nesting depth twice if parentheses are present around an expr

No impact on formatting on our repos.

Fixes #8021.

LGTM=adonovan
R=adonovan, dvyukov
CC=golang-codereviews
https://golang.org/cl/142020043
This commit is contained in:
Robert Griesemer 2014-09-12 11:18:12 -07:00
parent fbd1b5f98f
commit a14c1c986e
5 changed files with 17 additions and 2 deletions

View File

@ -736,7 +736,7 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
if _, hasParens := x.X.(*ast.ParenExpr); hasParens {
// don't print parentheses around an already parenthesized expression
// TODO(gri) consider making this more general and incorporate precedence levels
p.expr0(x.X, reduceDepth(depth)) // parentheses undo one level of depth
p.expr0(x.X, depth)
} else {
p.print(token.LPAREN)
p.expr0(x.X, reduceDepth(depth)) // parentheses undo one level of depth

View File

@ -94,6 +94,11 @@ func _() {
_ = Open(dpath+"/file", O_WRONLY|O_CREAT, 0666)
_ = int(c0&_Mask4)<<18 | int(c1&_Maskx)<<12 | int(c2&_Maskx)<<6 | int(c3&_Maskx)
// test case for issue 8021
// want:
// ([]bool{})[([]int{})[((1)+(((1)+((((1)*(((1)+(1))+(1)))+(1))*(1)))+(1)))]]
_ = ([]bool{})[([]int{})[((1)+(((1)+((((1)*(((1)+(1))+(1)))+(1))*(1)))+(1)))]]
// the parser does not restrict expressions that may appear as statements
true
42

View File

@ -95,6 +95,11 @@ func _() {
_ = Open(dpath + "/file", O_WRONLY | O_CREAT, 0666)
_ = int(c0&_Mask4)<<18 | int(c1&_Maskx)<<12 | int(c2&_Maskx)<<6 | int(c3&_Maskx)
// test case for issue 8021
// want:
// ([]bool{})[([]int{})[((1)+(((1)+((((1)*(((1)+(1))+(1)))+(1))*(1)))+(1)))]]
_ = ([]bool{})[([]int{})[((1) + (((((1) + (((((((1) * (((((1) + (1))) + (1))))) + (1))) * (1))))) + (1))))]]
// the parser does not restrict expressions that may appear as statements
true
42

View File

@ -94,6 +94,11 @@ func _() {
_ = Open(dpath+"/file", O_WRONLY|O_CREAT, 0666)
_ = int(c0&_Mask4)<<18 | int(c1&_Maskx)<<12 | int(c2&_Maskx)<<6 | int(c3&_Maskx)
// test case for issue 8021
// want:
// ([]bool{})[([]int{})[((1)+(((1)+((((1)*(((1)+(1))+(1)))+(1))*(1)))+(1)))]]
_ = ([]bool{})[([]int{})[((1)+(((1)+((((1)*(((1)+(1))+(1)))+(1))*(1)))+(1)))]]
// the parser does not restrict expressions that may appear as statements
true
42

View File

@ -444,7 +444,7 @@ func _() {
}
if x == a+b*(T{42}[0]) {
}
if x == a+(b * (T{42}[0])) {
if x == a+(b*(T{42}[0])) {
}
if x == a+b*(T{42}[0]) {
}