Skip to content

Commit

Permalink
go/printer: fix invalid output for empty decls
Browse files Browse the repository at this point in the history
The current output for empty declarations such as var, const, import
results in "var", "const", "import" respectively. These are not valid
syntax 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 golang#63566
  • Loading branch information
mauri870 committed Oct 17, 2023
1 parent 5873bd1 commit 8948fa0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/go/printer/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1739,6 +1739,15 @@ func (p *printer) genDecl(d *ast.GenDecl) {
p.setPos(d.Pos())
p.print(d.Tok, blank)

// Empty decls for "var ()", "const ()", "import ()"
if len(d.Specs) == 0 {
switch d.Tok {
case token.VAR, token.CONST, token.IMPORT:
p.print(token.LPAREN, token.RPAREN)
return
}
}

if d.Lparen.IsValid() || len(d.Specs) > 1 {
// group of parenthesized declarations
p.setPos(d.Lparen)
Expand Down
32 changes: 32 additions & 0 deletions src/go/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -848,3 +848,35 @@ 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
var tableTests = []struct {
tok token.Token
expected string
}{
{
tok: token.VAR,
expected: "var ()",
},
{
tok: token.CONST,
expected: "const ()",
},
{
tok: token.IMPORT,
expected: "import ()",
},
}

for _, tt := range tableTests {
var buf bytes.Buffer
Fprint(&buf, token.NewFileSet(), &ast.GenDecl{Tok: tt.tok})
got := buf.String()
if got != tt.expected {
t.Errorf("got %q, expected %q\n", got, tt.expected)
}
}
}

0 comments on commit 8948fa0

Please sign in to comment.