mirror of
https://github.com/golang/go
synced 2024-11-19 18:54:41 -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:
parent
fbd1b5f98f
commit
a14c1c986e
@ -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
|
||||
|
5
src/go/printer/testdata/expressions.golden
vendored
5
src/go/printer/testdata/expressions.golden
vendored
@ -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
|
||||
|
5
src/go/printer/testdata/expressions.input
vendored
5
src/go/printer/testdata/expressions.input
vendored
@ -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
|
||||
|
5
src/go/printer/testdata/expressions.raw
vendored
5
src/go/printer/testdata/expressions.raw
vendored
@ -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
|
||||
|
2
src/go/printer/testdata/statements.golden
vendored
2
src/go/printer/testdata/statements.golden
vendored
@ -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]) {
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user