-
Notifications
You must be signed in to change notification settings - Fork 17.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
go/printer: Fprint crashes with unexpected parenthesized expression panic #69206
Comments
I wonder whether this is the right fix: diff --git a/src/go/printer/nodes.go b/src/go/printer/nodes.go
index a4651e0608..495ec22031 100644
--- a/src/go/printer/nodes.go
+++ b/src/go/printer/nodes.go
@@ -411,9 +411,7 @@ func combinesWithName(x ast.Expr) bool {
case *ast.BinaryExpr:
return combinesWithName(x.X) && !isTypeElem(x.Y)
case *ast.ParenExpr:
- // name(x) combines but we are making sure at
- // the call site that x is never parenthesized.
- panic("unexpected parenthesized expression")
+ return combinesWithName(x.X)
}
return false
} |
But also looking at the parser, I wonder whether the parser is right here: Lines 2695 to 2702 in fc9f02c
We are loosing some bits of the AST, i believe it should be converted to diff --git a/src/go/parser/parser.go b/src/go/parser/parser.go
index 17808b366f..6c418fd796 100644
--- a/src/go/parser/parser.go
+++ b/src/go/parser/parser.go
@@ -2695,8 +2695,11 @@ func extractName(x ast.Expr, force bool) (*ast.Ident, ast.Expr) {
case *ast.CallExpr:
if name, _ := x.Fun.(*ast.Ident); name != nil {
if len(x.Args) == 1 && x.Ellipsis == token.NoPos && (force || isTypeElem(x.Args[0])) {
- // x = name "(" x.ArgList[0] ")"
- return name, x.Args[0]
+ return name, &ast.ParenExpr{
+ Lparen: x.Lparen,
+ X: x.Args[0],
+ Rparen: x.Rparen,
+ }
}
}
} |
Change https://go.dev/cl/610115 mentions this issue: |
CC @griesemer |
I think this looks right. Will comment on the CL. |
We are loosing a bit of the AST information, i believe we should convert *ast.CallExpr into *ast.ParenExpr. See #69206 (comment) Change-Id: I2d9ad8a3dead664a4fa9ac324e8d8a955a4d97c8 GitHub-Last-Rev: e5db56d GitHub-Pull-Request: #69209 Reviewed-on: https://go-review.googlesource.com/c/go/+/610078 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Griesemer <gri@google.com> Auto-Submit: Robert Griesemer <gri@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Change https://go.dev/cl/610758 mentions this issue: |
…nsistently Generally, the parser strips (i.e., does not record in the syntax tree) unnecessary parentheses. Specifically, given a type parameter list of the form [P (C),] it records it as [P C] and then no comma is required when printing. However it did only strip one level of parentheses, and [P ((C)),] made it through, causing a panic when printing. Somewhat related, the printer stripped parentheses around constraints as well. This CL implements a more consistent behavior: 1) The parser strips all parentheses around constraints. For testing purposes, a local flag (keep_parens) can be set to retain the parentheses. 2) The printer code now correctly intruces a comma if parentheses are present (e.g., when testing with keep_parens). This case does not occur in normal operation. 3) The printer does not strip parentheses around constraints since the parser does it already. For #69206. Change-Id: I974a800265625e8daf9477faa9ee4dd74dbd17ce Reviewed-on: https://go-review.googlesource.com/c/go/+/610758 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Findley <rfindley@google.com> Auto-Submit: Robert Griesemer <gri@google.com> Reviewed-by: Robert Griesemer <gri@google.com>
Change https://go.dev/cl/611355 mentions this issue: |
…en ParenExpr See discussion in CL 610115 and CL 610758. For #69206 Change-Id: I16f394cb3440106650fb64a466f2723a4dba3871 GitHub-Last-Rev: 37993b5 GitHub-Pull-Request: #69309 Reviewed-on: https://go-review.googlesource.com/c/go/+/611355 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Robert Griesemer <gri@google.com> Auto-Submit: Robert Griesemer <gri@google.com>
Reproducer:
The text was updated successfully, but these errors were encountered: