Skip to content

go/format: Inconsistent rendering of *ast.BasicLit #73172

@pjebs

Description

@pjebs

Go version

go version go1.24.0 darwin/amd64

Output of go env in your module/workspace:

Playground

What did you do?

See: https://go.dev/play/p/cF220dL8yUT

Sometimes I use *ast.BasicLit in my Code generators to write Go statements rather than properly breaking down the statement to the proper ast.* components. It's just easier this way and has never caused me any issues.

Until I noticed this very specific bug:

&ast.BasicLit{
	Kind:  token.INT,
	Value: "Error()",
}

gets rendered as:

error()

I understand this is not the proper way to parse and/or alter the syntax tree. This bug report is just regarding a weird inconsistency. Every other combination for Value: works properly.

What did you see happen?

Improper rendering.

What did you expect to see?

I expected to see: Error()

In the Go playground you can change the stmt to ANYTHING else and it will render properly:

https://go.dev/play/p/cF220dL8yUT
https://go.dev/play/p/FuxSXer6I4g

(WORK-AROUND: When I change it to token.STRING it works)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions