mirror of
https://github.com/golang/go.git
synced 2025-05-30 19:52:53 +00:00
go/printer: fix invalid output for empty decls
The current output for empty declarations such as var, const, import results in "var", "const", "import" respectively. These are not valid and the parser will promptly reject them as invalid syntax. This CL updates this behavior by adding "()" to the output of empty decls so the syntax becomes valid, e.g "var ()" instead of "var". Fixes #63566 Change-Id: I571b182d9ccf71b159360c8de003ad55d0ff3443 GitHub-Last-Rev: 2720419e364938e9962be71d0e6ed51375fec404 GitHub-Pull-Request: golang/go#63593 Reviewed-on: https://go-review.googlesource.com/c/go/+/535995 Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-by: Alan Donovan <adonovan@google.com> Auto-Submit: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Mauri de Souza Meneguzzo <mauri870@gmail.com>
This commit is contained in:
parent
860c2557ab
commit
ff7986d670
@ -1739,7 +1739,7 @@ func (p *printer) genDecl(d *ast.GenDecl) {
|
||||
p.setPos(d.Pos())
|
||||
p.print(d.Tok, blank)
|
||||
|
||||
if d.Lparen.IsValid() || len(d.Specs) > 1 {
|
||||
if d.Lparen.IsValid() || len(d.Specs) != 1 {
|
||||
// group of parenthesized declarations
|
||||
p.setPos(d.Lparen)
|
||||
p.print(token.LPAREN)
|
||||
|
@ -848,3 +848,18 @@ func TestSourcePosNewline(t *testing.T) {
|
||||
t.Errorf("unexpected Fprint output:\n%s", buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
// TestEmptyDecl tests that empty decls for const, var, import are printed with
|
||||
// valid syntax e.g "var ()" instead of just "var", which is invalid and cannot
|
||||
// be parsed.
|
||||
func TestEmptyDecl(t *testing.T) { // issue 63566
|
||||
for _, tok := range []token.Token{token.IMPORT, token.CONST, token.TYPE, token.VAR} {
|
||||
var buf bytes.Buffer
|
||||
Fprint(&buf, token.NewFileSet(), &ast.GenDecl{Tok: tok})
|
||||
got := buf.String()
|
||||
want := tok.String() + " ()"
|
||||
if got != want {
|
||||
t.Errorf("got %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user