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: //-style comments must always be terminated with a newline #1503

Closed
griesemer opened this issue Feb 11, 2011 · 3 comments
Closed

Comments

@griesemer
Copy link
Contributor

Even in the presence of incorrect position information, the go/printer should always
format //-style comments with a newline at the end. Otherwise, illegal Go programs can
be the result. The following is a test case.

package main

import (
    "bytes"
    "fmt"
    "go/parser"
    "go/printer"
    "go/token"
)

const src =
`// comment 1
// comment 2
// comment 3
package main
`

func newlines(s string) (n int) {
    for _, b := range s {
        if b == '\n' {
            n++
        }
    }
    return
}


func main() {
    fset := token.NewFileSet()
    ast1, err1 := parser.ParseFile(fset, "", src, parser.ParseComments)
    if err1 != nil {
        panic(err1)
    }

    var buf bytes.Buffer
    // Comments are not properly terminated when using a new (empty)
    // file set. Without the assignment below, the code works.
    fset = token.NewFileSet()
    printer.Fprint(&buf, fset, ast1)

    nsrc := newlines(src)
    nbuf := newlines(buf.String())
    if nsrc != nbuf {
        fmt.Printf("got %d, expected %d\n", nbuf, nsrc)
    }
}
@rsc
Copy link
Contributor

rsc commented Feb 11, 2011

Comment 1:

fwiw the test case from earlier today had two problems.
in addition to not printing the final \n on the comment,
it was deciding to insert a comment after the fmt in fmt.Printf,
but that's not a valid place to insert a \n in go.
the closest valid place would have been after the dot.

@griesemer
Copy link
Contributor Author

Comment 2:

Added separate issue #1505: go/printer must always generate legal programs.

@griesemer
Copy link
Contributor Author

Comment 3:

This issue was closed by revision 6b526eb.

Status changed to Fixed.

@griesemer griesemer self-assigned this Feb 14, 2011
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants