From 320a44d20c5e48ee94b28ff648b13c6960a9b4a3 Mon Sep 17 00:00:00 2001 From: Mateusz Poliwczak Date: Sat, 12 Oct 2024 18:29:20 +0200 Subject: [PATCH] go/printer: do not treat '\f' as a newline in (*printer).writeString Change-Id: I59a32c6bb16661a730930911dcdcfa1c20f16823 --- src/go/printer/printer.go | 2 +- src/go/printer/printer_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/go/printer/printer.go b/src/go/printer/printer.go index 5a6127c6b40..e84fb372b00 100644 --- a/src/go/printer/printer.go +++ b/src/go/printer/printer.go @@ -311,7 +311,7 @@ func (p *printer) writeString(pos token.Position, s string, isLit bool) { var li int // index of last newline; valid if nlines > 0 for i := 0; i < len(s); i++ { // Raw string literals may contain any character except back quote (`). - if ch := s[i]; ch == '\n' || ch == '\f' { + if ch := s[i]; ch == '\n' { // account for line break nlines++ li = i diff --git a/src/go/printer/printer_test.go b/src/go/printer/printer_test.go index 2a9c8be3003..19fdd44f37b 100644 --- a/src/go/printer/printer_test.go +++ b/src/go/printer/printer_test.go @@ -16,6 +16,7 @@ import ( "io" "os" "path/filepath" + "strings" "testing" "time" ) @@ -863,3 +864,28 @@ func TestEmptyDecl(t *testing.T) { // issue 63566 } } } + +func TestIssue69858(t *testing.T) { + cases := []string{ + "package A\nimport(\"a\"/*\f*/\n\"bb\")", + "package A\nfunc test() {\"a\"/*\f*/\n\"bb\"}", + } + for _, src := range cases { + fset := token.NewFileSet() + f, err := parser.ParseFile(fset, "test.go", src, parser.ParseComments|parser.SkipObjectResolution) + if err != nil { + t.Fatal(err) + } + + var out strings.Builder + if err := Fprint(&out, fset, f); err != nil { + t.Fatal(err) + } + + _, err = parser.ParseFile(fset, "test.go", out.String(), parser.ParseComments|parser.SkipObjectResolution) + if err != nil { + t.Logf("source:\n%s\nformatted as:\n%s", src, out.String()) + t.Error(err) + } + } +}