Skip to content

Commit 6f26480

Browse files
committed
go/printer: remove exported StdFormat flag
The StdFormat flag was added as part of CL 231461, where the primary aim was to fix the bug #37476. It's expected that the existing printer modes only adjust spacing but do not change any of the code text itself. A new printing flag served as a way for cmd/gofmt and go/format to delegate a part of formatting work to the printer—where it's more more convenient and efficient to perform—while maintaining current low-level printing behavior of go/printer unmodified. We already have cmd/gofmt and the go/format API that implement standard formatting of Go source code, so there isn't a need to expose StdFormat flag to the world, as it can only cause confusion. Consider that to format source in canonical gofmt style completely it may require tasks A, B, C to be done. In one version of Go, the printer may do both A and B, while cmd/gofmt and go/format will do the remaining task C. In another version, the printer may take on doing just A, while cmd/gofmt and go/format will perform B and C. This makes it hard to add a gofmt-like mode to the printer without compromising on above fluidity. This change prefers to shift back some complexity to the implementation of the standard library, allowing us to avoid creating the new exported printing flag just for the internal needs of gofmt and go/format today. We may still want to re-think the API and consider if something better should be added, but unfortunately there isn't time for Go 1.15. We are not adding new APIs now, so we can defer this decision until Go 1.16 or later, when there is more time. For #37476. For #37453. For #39489. For #37419. Change-Id: I0bb07156dca852b043487099dcf05c5350b29e20 Reviewed-on: https://go-review.googlesource.com/c/go/+/240683 Reviewed-by: Robert Griesemer <gri@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
1 parent 4469f54 commit 6f26480

File tree

10 files changed

+58
-26
lines changed

10 files changed

+58
-26
lines changed

api/go1.15.txt

-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ pkg debug/pe, const IMAGE_SUBSYSTEM_WINDOWS_GUI = 2
112112
pkg debug/pe, const IMAGE_SUBSYSTEM_WINDOWS_GUI ideal-int
113113
pkg debug/pe, const IMAGE_SUBSYSTEM_XBOX = 14
114114
pkg debug/pe, const IMAGE_SUBSYSTEM_XBOX ideal-int
115-
pkg go/printer, const StdFormat = 16
116-
pkg go/printer, const StdFormat Mode
117115
pkg math/big, method (*Int) FillBytes([]uint8) []uint8
118116
pkg net, method (*Resolver) LookupIP(context.Context, string, string) ([]IP, error)
119117
pkg net/url, method (*URL) EscapedFragment() string

doc/go1.15.html

+10-7
Original file line numberDiff line numberDiff line change
@@ -658,15 +658,18 @@ <h3 id="minor_library_changes">Minor changes to the library</h3>
658658
</dd>
659659
</dl><!-- fmt -->
660660

661-
<dl id="go/printer"><dt><a href="/pkg/go/printer/">go/printer</a></dt>
661+
<dl id="go/format"><dt><a href="/pkg/go/format/">go/format</a></dt>
662662
<dd>
663-
<p><!-- CL 231461 -->
664-
The new <a href="/pkg/go/printer/#Mode"><code>Mode</code></a>
665-
value <a href="/pkg/go/printer/#StdFormat"><code>StdFormat</code></a>
666-
directs the printer to apply standard formatting changes while
667-
printing the output.
663+
<p><!-- golang.org/issue/37476, CL 231461, CL 240683 -->
664+
The <a href="/pkg/go/format/#Source"><code>Source</code></a> and
665+
<a href="/pkg/go/format/#Node"><code>Node</code></a> functions
666+
now canonicalize number literal prefixes and exponents as part
667+
of formatting Go source code. This matches the behavior of the
668+
<a href="/pkg/cmd/gofmt/"><code>gofmt</code></a> command as it
669+
was implemented <a href="/doc/go1.13#gofmt">since Go 1.13</a>.
670+
</p>
668671
</dd>
669-
</dl><!-- go/printer -->
672+
</dl><!-- go/format -->
670673

671674
<dl id="html/template"><dt><a href="/pkg/html/template/">html/template</a></dt>
672675
<dd>

src/cmd/gofmt/gofmt.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ var (
4040
// Keep these in sync with go/format/format.go.
4141
const (
4242
tabWidth = 8
43-
printerMode = printer.UseSpaces | printer.TabIndent | printer.StdFormat
43+
printerMode = printer.UseSpaces | printer.TabIndent | printerNormalizeNumbers
44+
45+
// printerNormalizeNumbers means to canonicalize number literal prefixes
46+
// and exponents while printing. See https://golang.org/doc/go1.13#gofmt.
47+
//
48+
// This value is defined in go/printer specifically for go/format and cmd/gofmt.
49+
printerNormalizeNumbers = 1 << 30
4450
)
4551

4652
var (

src/go/format/format.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ import (
2727
// Keep these in sync with cmd/gofmt/gofmt.go.
2828
const (
2929
tabWidth = 8
30-
printerMode = printer.UseSpaces | printer.TabIndent | printer.StdFormat
30+
printerMode = printer.UseSpaces | printer.TabIndent | printerNormalizeNumbers
31+
32+
// printerNormalizeNumbers means to canonicalize number literal prefixes
33+
// and exponents while printing. See https://golang.org/doc/go1.13#gofmt.
34+
//
35+
// This value is defined in go/printer specifically for go/format and cmd/gofmt.
36+
printerNormalizeNumbers = 1 << 30
3137
)
3238

3339
var config = printer.Config{Mode: printerMode, Tabwidth: tabWidth}

src/go/format/format_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ func TestNode(t *testing.T) {
5858
diff(t, buf.Bytes(), src)
5959
}
6060

61-
// Node is documented to not modify the AST. Test that it is so, even when
62-
// formatting changes are applied due to printer.StdFormat mode being used.
61+
// Node is documented to not modify the AST.
62+
// Test that it is so even when numbers are normalized.
6363
func TestNodeNoModify(t *testing.T) {
6464
const (
6565
src = "package p\n\nconst _ = 0000000123i\n"

src/go/printer/nodes.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -791,8 +791,8 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
791791
}
792792

793793
case *ast.BasicLit:
794-
if p.Config.Mode&StdFormat != 0 {
795-
x = normalizeNumbers(x)
794+
if p.Config.Mode&normalizeNumbers != 0 {
795+
x = normalizedNumber(x)
796796
}
797797
p.print(x)
798798

@@ -974,11 +974,15 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
974974
}
975975
}
976976

977-
// normalizeNumbers rewrites base prefixes and exponents to
978-
// use lower-case letters, and removes leading 0's from
979-
// integer imaginary literals. It leaves hexadecimal digits
980-
// alone.
981-
func normalizeNumbers(lit *ast.BasicLit) *ast.BasicLit {
977+
// normalizedNumber rewrites base prefixes and exponents
978+
// of numbers to use lower-case letters (0X123 to 0x123 and 1.2E3 to 1.2e3),
979+
// and removes leading 0's from integer imaginary literals (0765i to 765i).
980+
// It leaves hexadecimal digits alone.
981+
//
982+
// normalizedNumber doesn't modify the ast.BasicLit value lit points to.
983+
// If lit is not a number or a number in canonical format already,
984+
// lit is returned as is. Otherwise a new ast.BasicLit is created.
985+
func normalizedNumber(lit *ast.BasicLit) *ast.BasicLit {
982986
if lit.Kind != token.INT && lit.Kind != token.FLOAT && lit.Kind != token.IMAG {
983987
return lit // not a number - nothing to do
984988
}

src/go/printer/performance_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
var testfile *ast.File
2121

2222
func testprint(out io.Writer, file *ast.File) {
23-
if err := (&Config{TabIndent | UseSpaces | StdFormat, 8, 0}).Fprint(out, fset, file); err != nil {
23+
if err := (&Config{TabIndent | UseSpaces | normalizeNumbers, 8, 0}).Fprint(out, fset, file); err != nil {
2424
log.Fatalf("print error: %s", err)
2525
}
2626
}

src/go/printer/printer.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,22 @@ const (
12761276
TabIndent // use tabs for indentation independent of UseSpaces
12771277
UseSpaces // use spaces instead of tabs for alignment
12781278
SourcePos // emit //line directives to preserve original source positions
1279-
StdFormat // apply standard formatting changes (exact byte output may change between versions of Go)
1279+
)
1280+
1281+
// The mode below is not included in printer's public API because
1282+
// editing code text is deemed out of scope. Because this mode is
1283+
// unexported, it's also possible to modify or remove it based on
1284+
// the evolving needs of go/format and cmd/gofmt without breaking
1285+
// users. See discussion in CL 240683.
1286+
const (
1287+
// normalizeNumbers means to canonicalize number
1288+
// literal prefixes and exponents while printing.
1289+
//
1290+
// This value is known in and used by go/format and cmd/gofmt.
1291+
// It is currently more convenient and performant for those
1292+
// packages to apply number normalization during printing,
1293+
// rather than by modifying the AST in advance.
1294+
normalizeNumbers Mode = 1 << 30
12801295
)
12811296

12821297
// A Config node controls the output of Fprint.

src/go/printer/printer_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type checkMode uint
3333
const (
3434
export checkMode = 1 << iota
3535
rawFormat
36-
stdFormat
36+
normNumber
3737
idempotent
3838
)
3939

@@ -58,8 +58,8 @@ func format(src []byte, mode checkMode) ([]byte, error) {
5858
if mode&rawFormat != 0 {
5959
cfg.Mode |= RawFormat
6060
}
61-
if mode&stdFormat != 0 {
62-
cfg.Mode |= StdFormat
61+
if mode&normNumber != 0 {
62+
cfg.Mode |= normalizeNumbers
6363
}
6464

6565
// print AST
@@ -205,7 +205,7 @@ var data = []entry{
205205
{"slow.input", "slow.golden", idempotent},
206206
{"complit.input", "complit.x", export},
207207
{"go2numbers.input", "go2numbers.golden", idempotent},
208-
{"go2numbers.input", "go2numbers.stdfmt", stdFormat | idempotent},
208+
{"go2numbers.input", "go2numbers.norm", normNumber | idempotent},
209209
}
210210

211211
func TestFiles(t *testing.T) {

0 commit comments

Comments
 (0)