aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/gofmt
diff options
context:
space:
mode:
authorRob Findley <rfindley@google.com>2021-02-24 10:31:11 -0500
committerRobert Findley <rfindley@google.com>2021-03-02 01:50:12 +0000
commita6eeb4add46eddb19ceba36bdd448738808e5ce2 (patch)
tree06b2d25e5aef46c7ce3da6fae61e199025efdd88 /src/cmd/gofmt
parentff5cf4ced3f1681ec972cd954d4b476f87616fe3 (diff)
downloadgo-a6eeb4add46eddb19ceba36bdd448738808e5ce2.tar.gz
go-a6eeb4add46eddb19ceba36bdd448738808e5ce2.zip
go/parser,go/types: hide API changes related to type parameters
While the dev.typeparams branch was merged, the type parameter API is slated for go1.18. Hide these changes to the go/parser and go/types API. This was done as follows: + For APIs that will probably not be needed for go1.18, simply unexport them. + For APIs that we expect to export in go1.18, prefix symbols with '_', so that the planned (upper-cased) symbol name is apparent. + For APIs that must be exported for testing, move both API and tests to files guarded by the go1.18 build constraint. + parser.ParseTypeParams is unexported and copied wherever it is needed. + The -G flag is removed from gofmt, replaced by enabling type parameters if built with the go1.18 build constraint. Notably, changes related to type parameters in go/ast are currently left exported. We're looking at the AST API separately. The new API diff from 1.16 is: +pkg go/ast, method (*FuncDecl) IsMethod() bool +pkg go/ast, method (*ListExpr) End() token.Pos +pkg go/ast, method (*ListExpr) Pos() token.Pos +pkg go/ast, type FuncType struct, TParams *FieldList +pkg go/ast, type ListExpr struct +pkg go/ast, type ListExpr struct, ElemList []Expr +pkg go/ast, type TypeSpec struct, TParams *FieldList +pkg go/types, type Config struct, GoVersion string Change-Id: I1baf67e26279b49092e774309a836c460979774a Reviewed-on: https://go-review.googlesource.com/c/go/+/295929 Trust: Robert Findley <rfindley@google.com> Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/cmd/gofmt')
-rw-r--r--src/cmd/gofmt/gofmt.go25
-rw-r--r--src/cmd/gofmt/gofmt_go1.18.go12
-rw-r--r--src/cmd/gofmt/gofmt_test.go3
3 files changed, 30 insertions, 10 deletions
diff --git a/src/cmd/gofmt/gofmt.go b/src/cmd/gofmt/gofmt.go
index b82aa7e7a9..95f537d91e 100644
--- a/src/cmd/gofmt/gofmt.go
+++ b/src/cmd/gofmt/gofmt.go
@@ -26,13 +26,16 @@ import (
var (
// main operation modes
- list = flag.Bool("l", false, "list files whose formatting differs from gofmt's")
- write = flag.Bool("w", false, "write result to (source) file instead of stdout")
- rewriteRule = flag.String("r", "", "rewrite rule (e.g., 'a[b:len(a)] -> a[b:]')")
- simplifyAST = flag.Bool("s", false, "simplify code")
- doDiff = flag.Bool("d", false, "display diffs instead of rewriting files")
- allErrors = flag.Bool("e", false, "report all errors (not just the first 10 on different lines)")
- allowTypeParams = flag.Bool("G", false, "allow generic code")
+ list = flag.Bool("l", false, "list files whose formatting differs from gofmt's")
+ write = flag.Bool("w", false, "write result to (source) file instead of stdout")
+ rewriteRule = flag.String("r", "", "rewrite rule (e.g., 'a[b:len(a)] -> a[b:]')")
+ simplifyAST = flag.Bool("s", false, "simplify code")
+ doDiff = flag.Bool("d", false, "display diffs instead of rewriting files")
+ allErrors = flag.Bool("e", false, "report all errors (not just the first 10 on different lines)")
+
+ // allowTypeParams controls whether type parameters are allowed in the code
+ // being formatted. It is enabled for go1.18 in gofmt_go1.18.go.
+ allowTypeParams = false
// debugging
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to this file")
@@ -48,6 +51,10 @@ const (
//
// This value is defined in go/printer specifically for go/format and cmd/gofmt.
printerNormalizeNumbers = 1 << 30
+
+ // parseTypeParams tells go/parser to parse type parameters. Must be kept in
+ // sync with go/parser/interface.go.
+ parseTypeParams parser.Mode = 1 << 30
)
var (
@@ -72,8 +79,8 @@ func initParserMode() {
if *allErrors {
parserMode |= parser.AllErrors
}
- if *allowTypeParams {
- parserMode |= parser.ParseTypeParams
+ if allowTypeParams {
+ parserMode |= parseTypeParams
}
}
diff --git a/src/cmd/gofmt/gofmt_go1.18.go b/src/cmd/gofmt/gofmt_go1.18.go
new file mode 100644
index 0000000000..be7b46b5ed
--- /dev/null
+++ b/src/cmd/gofmt/gofmt_go1.18.go
@@ -0,0 +1,12 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build go1.18
+// +build go1.18
+
+package main
+
+func init() {
+ allowTypeParams = true
+}
diff --git a/src/cmd/gofmt/gofmt_test.go b/src/cmd/gofmt/gofmt_test.go
index 60e4f2e03d..9e2239a692 100644
--- a/src/cmd/gofmt/gofmt_test.go
+++ b/src/cmd/gofmt/gofmt_test.go
@@ -78,7 +78,8 @@ func runTest(t *testing.T, in, out string) {
// fake flag - pretend input is from stdin
stdin = true
case "-G":
- *allowTypeParams = true
+ // fake flag - allow parsing type parameters
+ allowTypeParams = true
default:
t.Errorf("unrecognized flag name: %s", name)
}