aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/gofmt
diff options
context:
space:
mode:
authorRob Findley <rfindley@google.com>2021-03-05 10:57:48 -0500
committerRobert Findley <rfindley@google.com>2021-04-13 22:24:31 +0000
commitefaf75a2166d397b2050cc0bb1b60b0c80698e2d (patch)
treec1ec333957953f7ed90c0883b85fe786fb3f7596 /src/cmd/gofmt
parent693859542e71fdd9186fff759bf121e9df197fed (diff)
downloadgo-efaf75a2166d397b2050cc0bb1b60b0c80698e2d.tar.gz
go-efaf75a2166d397b2050cc0bb1b60b0c80698e2d.zip
go/*,cmd/gofmt: guard AST changes with the typeparams build tag
This CL changes our approach to guarding type parameter functionality and API. Previously, we guarded type parameter functionality with the parser.parseTypeParams parser mode, and were in the process of hiding the type parameter API behind the go1.18 build constraint. These mechanisms had several limitations: + Requiring the parser.parseTypeParams mode to be set meant that existing tooling would have to opt-in to type parameters in all places where it parses Go files. + The parseTypeParams mode value had to be copied in several places. + go1.18 is not specific to typeparams, making it difficult to set up the builders to run typeparams tests. This CL addresses the above limitations, and completes the task of hiding the AST API, by switching to a new 'typeparams' build constraint and adding a new go/internal/typeparams helper package. The typeparams build constraint is used to conditionally compile the new AST changes. The typeparams package provides utilities for accessing and writing the new AST data, so that we don't have to fragment our parser or type checker logic across build constraints. The typeparams.Enabled const is used to guard tests that require type parameter support. The parseTypeParams parser mode is gone, replaced by a new typeparams.DisableParsing mode with the opposite sense. Now, type parameters are only parsed if go/parser is compiled with the typeparams build constraint set AND typeparams.DisableParsing not set. This new parser mode allows opting out of type parameter parsing for tests. How exactly to run tests on builders is left to a follow-up CL. Updates #44933 Change-Id: I3091e42a2e5e2f23e8b2ae584f415a784b9fbd65 Reviewed-on: https://go-review.googlesource.com/c/go/+/300649 Trust: Robert Findley <rfindley@google.com> 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.go11
-rw-r--r--src/cmd/gofmt/gofmt_test.go9
-rw-r--r--src/cmd/gofmt/gofmt_typeparams_test.go (renamed from src/cmd/gofmt/gofmt_go1.18.go)6
3 files changed, 9 insertions, 17 deletions
diff --git a/src/cmd/gofmt/gofmt.go b/src/cmd/gofmt/gofmt.go
index cd867bba15..2793c2c2a4 100644
--- a/src/cmd/gofmt/gofmt.go
+++ b/src/cmd/gofmt/gofmt.go
@@ -33,10 +33,6 @@ var (
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")
)
@@ -53,10 +49,6 @@ const (
printerNormalizeNumbers = 1 << 30
)
-// parseTypeParams tells go/parser to parse type parameters. Must be kept in
-// sync with go/parser/interface.go.
-const parseTypeParams parser.Mode = 1 << 30
-
var (
fileSet = token.NewFileSet() // per process FileSet
exitCode = 0
@@ -79,9 +71,6 @@ func initParserMode() {
if *allErrors {
parserMode |= parser.AllErrors
}
- if allowTypeParams {
- parserMode |= parseTypeParams
- }
}
func isGoFile(f fs.DirEntry) bool {
diff --git a/src/cmd/gofmt/gofmt_test.go b/src/cmd/gofmt/gofmt_test.go
index 9e2239a692..f0d3f8780f 100644
--- a/src/cmd/gofmt/gofmt_test.go
+++ b/src/cmd/gofmt/gofmt_test.go
@@ -49,12 +49,13 @@ func gofmtFlags(filename string, maxLines int) string {
case scanner.EOF:
return ""
}
-
}
return ""
}
+var typeParamsEnabled = false
+
func runTest(t *testing.T, in, out string) {
// process flags
*simplifyAST = false
@@ -78,8 +79,10 @@ func runTest(t *testing.T, in, out string) {
// fake flag - pretend input is from stdin
stdin = true
case "-G":
- // fake flag - allow parsing type parameters
- allowTypeParams = true
+ // fake flag - test is for generic code
+ if !typeParamsEnabled {
+ return
+ }
default:
t.Errorf("unrecognized flag name: %s", name)
}
diff --git a/src/cmd/gofmt/gofmt_go1.18.go b/src/cmd/gofmt/gofmt_typeparams_test.go
index be7b46b5ed..10641a77cb 100644
--- a/src/cmd/gofmt/gofmt_go1.18.go
+++ b/src/cmd/gofmt/gofmt_typeparams_test.go
@@ -2,11 +2,11 @@
// 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
+//go:build typeparams
+// +build typeparams
package main
func init() {
- allowTypeParams = true
+ typeParamsEnabled = true
}