]> Cypherpunks repositories - gostls13.git/commitdiff
go/printer: print parenthesized declarations if len(d.Specs) > 1
authorDavid Tolpin <david.tolpin@gmail.com>
Tue, 27 Nov 2018 00:17:32 +0000 (00:17 +0000)
committerRobert Griesemer <gri@golang.org>
Tue, 27 Nov 2018 18:35:40 +0000 (18:35 +0000)
Parenthesized declaration must be printed if len(d.Specs) > 1 even if d.Lparen==token.NoPos. This happens if the node tree is created programmatically. Otherwise, all but the first specifications just silently disappear from the output.

Change-Id: I17ab24bb1cd56fe1e611199698535ca60a97f5ea
GitHub-Last-Rev: 2f168dc7ad4a29149685efc70f180987523271e4
GitHub-Pull-Request: golang/go#28533
Reviewed-on: https://go-review.googlesource.com/c/146657
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/go/printer/nodes.go
src/go/printer/printer_test.go

index a307d8395e9d94400a367519049b1843a1cc9e7e..d22f865652246eb7559eb7b99c36a131db4cd527 100644 (file)
@@ -1537,7 +1537,7 @@ func (p *printer) genDecl(d *ast.GenDecl) {
        p.setComment(d.Doc)
        p.print(d.Pos(), d.Tok, blank)
 
-       if d.Lparen.IsValid() {
+       if d.Lparen.IsValid() || len(d.Specs) > 1 {
                // group of parenthesized declarations
                p.print(d.Lparen, token.LPAREN)
                if n := len(d.Specs); n > 0 {
index 27d46df6b186bcc6e313ea980573b20fc35d5f5a..91eca585c09563cfc4657c81a4081d7ae1e9798a 100644 (file)
@@ -736,3 +736,35 @@ func TestIssue11151(t *testing.T) {
                t.Errorf("%v\norig: %q\ngot : %q", err, src, got)
        }
 }
+
+// If a declaration has multiple specifications, a parenthesized
+// declaration must be printed even if Lparen is token.NoPos.
+func TestParenthesizedDecl(t *testing.T) {
+       // a package with multiple specs in a single declaration
+       const src = "package p; var ( a float64; b int )"
+       fset := token.NewFileSet()
+       f, err := parser.ParseFile(fset, "", src, 0)
+
+       // print the original package
+       var buf bytes.Buffer
+       err = Fprint(&buf, fset, f)
+       if err != nil {
+               t.Fatal(err)
+       }
+       original := buf.String()
+
+       // now remove parentheses from the declaration
+       for i := 0; i != len(f.Decls); i++ {
+               f.Decls[i].(*ast.GenDecl).Lparen = token.NoPos
+       }
+       buf.Reset()
+       err = Fprint(&buf, fset, f)
+       if err != nil {
+               t.Fatal(err)
+       }
+       noparen := buf.String()
+
+       if noparen != original {
+               t.Errorf("got %q, want %q", noparen, original)
+       }
+}