1
0
mirror of https://github.com/golang/go synced 2024-11-21 16:14:42 -07:00

go/printer: do not treat '\f' as a newline in (*printer).writeString

Change-Id: I59a32c6bb16661a730930911dcdcfa1c20f16823
This commit is contained in:
Mateusz Poliwczak 2024-10-12 18:29:20 +02:00
parent 1041c2cf01
commit 320a44d20c
2 changed files with 27 additions and 1 deletions

View File

@ -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

View File

@ -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)
}
}
}