Skip to content
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: do not treat '\f' as a newline in (*printer).writeString #69859

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/go/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions src/go/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"io"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -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)
}
}
}