]> Cypherpunks repositories - gostls13.git/commitdiff
go/printer: revert "do not treat comments inside a ast.Decl as godoc"
authorDmitri Shuralyov <dmitshur@golang.org>
Tue, 10 Sep 2024 17:51:53 +0000 (17:51 +0000)
committerChristian Höppner <hoeppi@google.com>
Wed, 11 Sep 2024 06:30:02 +0000 (06:30 +0000)
This reverts commit CL 609077.

Reason for revert: it turned out to also introduce a change to the
formatting as described in issue #69382, which wasn't intended.

For #69382.

Change-Id: Id8f36e5503e63e7586c8afe7c7d3dc25fd56ed94
Reviewed-on: https://go-review.googlesource.com/c/go/+/612137
Reviewed-by: Christian Höppner <hoeppi@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
src/go/printer/nodes.go
src/go/printer/printer.go
src/go/printer/printer_test.go

index 9e796132801db26bf3006f64feebb1efb1ccbfe5..a295a68d6f8417002e3e8504081e30144f0365dd 100644 (file)
@@ -1737,9 +1737,6 @@ func (p *printer) genDecl(d *ast.GenDecl) {
        p.setPos(d.Pos())
        p.print(d.Tok, blank)
 
-       defer func(d bool) { p.inDecl = d }(p.inDecl)
-       p.inDecl = true
-
        if d.Lparen.IsValid() || len(d.Specs) != 1 {
                // group of parenthesized declarations
                p.setPos(d.Lparen)
@@ -1924,10 +1921,6 @@ func (p *printer) funcDecl(d *ast.FuncDecl) {
        p.setComment(d.Doc)
        p.setPos(d.Pos())
        p.print(token.FUNC, blank)
-
-       defer func(d bool) { p.inDecl = d }(p.inDecl)
-       p.inDecl = true
-
        // We have to save startCol only after emitting FUNC; otherwise it can be on a
        // different line (all whitespace preceding the FUNC is emitted only when the
        // FUNC is emitted).
index 3aaf8947be72f8715be9da4d58ec78605ad2d7eb..5a6127c6b40dd7a018e3bfd8a575af22a8b83225 100644 (file)
@@ -63,7 +63,6 @@ type printer struct {
        mode         pmode        // current printer mode
        endAlignment bool         // if set, terminate alignment immediately
        impliedSemi  bool         // if set, a linebreak implies a semicolon
-       inDecl       bool         // if set, printer is inside declaration (after first token)
        lastTok      token.Token  // last token printed (token.ILLEGAL if it's whitespace)
        prevOpen     token.Token  // previous non-brace "open" token (, [, or token.ILLEGAL
        wsbuf        []whiteSpace // delayed white space
@@ -740,9 +739,8 @@ func (p *printer) intersperseComments(next token.Position, tok token.Token) (wro
        for p.commentBefore(next) {
                list := p.comment.List
                changed := false
-               if !p.inDecl &&
-                       p.lastTok != token.IMPORT && // do not rewrite cgo's import "C" comments
-                       p.posFor(p.comment.Pos()).Line != p.last.Line &&
+               if p.lastTok != token.IMPORT && // do not rewrite cgo's import "C" comments
+                       p.posFor(p.comment.Pos()).Column == 1 &&
                        p.posFor(p.comment.End()+1) == next {
                        // Unindented comment abutting next token position:
                        // a top-level doc comment.
index d541a544651d2a50b14deba8c51459750bf12118..2a9c8be30032493b685704a3692e40910d17c641 100644 (file)
@@ -16,7 +16,6 @@ import (
        "io"
        "os"
        "path/filepath"
-       "strings"
        "testing"
        "time"
 )
@@ -864,192 +863,3 @@ func TestEmptyDecl(t *testing.T) { // issue 63566
                }
        }
 }
-
-func TestDocFormat(t *testing.T) {
-       cases := []struct {
-               src  string
-               want string
-       }{
-               {
-                       src: `package main
-
-func main() {
-//
-//go:directive
-// test
-//
-}
-`,
-                       want: `package main
-
-func main() {
-       //
-       //go:directive
-       // test
-       //
-}
-`,
-               },
-               {
-                       src: `package main
-
-func main() {
-       //go:directive
-       // test
-       type a struct{}
-
-//go:directive
-// test
-test()
-}
-`,
-                       want: `package main
-
-func main() {
-       //go:directive
-       // test
-       type a struct{}
-
-       //go:directive
-       // test
-       test()
-}
-`,
-               },
-               {
-                       src: `package main
-
-func main() {
-//go:directive
-// test
-type a struct{}
-}
-`,
-                       want: `package main
-
-func main() {
-       //go:directive
-       // test
-       type a struct{}
-}
-`,
-               },
-               {
-                       src: `package main
-
-func a() {
-//line a:5:1
-       //
-}
-`,
-                       want: `package main
-
-func a() {
-//line a:5:1
-       //
-}
-`,
-               },
-
-               {
-                       src: `package main
-
-// test comment
-//go:directive2
-// test comment
-func main() {
-}
-`,
-                       want: `package main
-
-// test comment
-// test comment
-//
-//go:directive2
-func main() {
-}
-`,
-               },
-               {
-                       src: `package main
-
-       // test comment
-       //go:directive2
-       // test comment
-func main() {
-}
-`,
-                       want: `package main
-
-// test comment
-// test comment
-//
-//go:directive2
-func main() {
-}
-`,
-               },
-               {
-                       src: `package main
-
-/* test
- */ // test comment
-//go:directive2
-// test comment
-func main() {
-}
-`,
-                       want: `package main
-
-/* test
- */ // test comment
-//go:directive2
-// test comment
-func main() {
-}
-`,
-               },
-
-               {
-                       src: `package main  //comment
-var a int = 4 //comment
-func a() {
-}
-`,
-                       want: `package main  //comment
-var a int = 4 //comment
-func a() {
-}
-`,
-               },
-
-               // Edge case found by a fuzzer, not a real-world example.
-               {
-                       src:  "package A\n\nimport(\"\f\"\n//\n\"\")",
-                       want: "package A\n\nimport (\n\t\"\f\" //\n\t\"\"\n)\n",
-               },
-               {
-                       src:  "package A\n\nimport(`\f`\n//\n\"\")",
-                       want: "package A\n\nimport (\n\t`\f` //\n\t\"\"\n)\n",
-               },
-       }
-
-       for _, tt := range cases {
-               fset := token.NewFileSet()
-               f, err := parser.ParseFile(fset, "test.go", tt.src, parser.ParseComments|parser.SkipObjectResolution)
-               if err != nil {
-                       t.Fatal(err)
-               }
-
-               var buf strings.Builder
-               cfg := Config{Tabwidth: 8, Mode: UseSpaces | TabIndent}
-               if err := cfg.Fprint(&buf, fset, f); err != nil {
-                       t.Fatal(err)
-               }
-
-               got := buf.String()
-               if got != tt.want {
-                       t.Errorf("source\n%v\nformatted as:\n%v\nwant formatted as:\n%v", tt.src, got, tt.want)
-               }
-       }
-}