]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gofmt: use SkipObjectResolution with -s as well
authorDaniel Martí <mvdan@mvdan.cc>
Fri, 22 Apr 2022 22:57:06 +0000 (23:57 +0100)
committerRobert Griesemer <gri@google.com>
Thu, 19 May 2022 20:06:14 +0000 (20:06 +0000)
The "simplify" feature used go/ast's object tracking in only one place -
to replace s[a:len(s)] with s[a:].
Using go/ast.Object did allow us to not simplify code like:

len := func(s []int) int { ... }
s = s[a:len(s)]

The existing code already noted the limitation with that approach,
such as "len" being redeclared in a different file in the same package.
Since go/ast's object tracking is file-based and very basic,
it wouldn't work with edge cases like those.

The reasoning is that redeclaring len and abusing it that way is
extremely unlikely, and hasn't been a problem in about a decade now.
I reason that the same applies to len being redeclared in the same file,
so we should be able to safely remove the use of go/ast.Object here.

Per https://go.dev/cl/401454, this makes "gofmt -s" about 5% faster.
If we ever wanted to truly get rid of false positive simplifications,
I imagine we'd want to reimplement the feature under go/analysis,
which is able to fully typecheck packages and suggest edits.
That seems unnecessary at this point, but we can always course correct
in the presumably unlikely scenario that users start reporting bugs.

See #46485.
For #52463.

Change-Id: I77fc97adceafde8f0fe6887ace83ae325bfa7416
Reviewed-on: https://go-review.googlesource.com/c/go/+/401875
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
src/cmd/gofmt/gofmt.go
src/cmd/gofmt/simplify.go

index 9b639bd996323c3818939622590a5376a11a474b..e464d64c98bac3724b07c6b5c7e57159592371d9 100644 (file)
@@ -76,9 +76,9 @@ func initParserMode() {
        if *allErrors {
                parserMode |= parser.AllErrors
        }
-       // Both -r and -s make use of go/ast's object resolution.
-       // If neither is being used, avoid that unnecessary work.
-       if *rewriteRule == "" && !*simplifyAST {
+       // It's only -r that makes use of go/ast's object resolution,
+       // so avoid the unnecessary work if the flag isn't used.
+       if *rewriteRule == "" {
                parserMode |= parser.SkipObjectResolution
        }
 }
index 1a0e8174afa384fad243c46fa56c8cd845d761d3..eb55daabc1db86740cdd63bb938c2f1e7d3d166f 100644 (file)
@@ -53,22 +53,26 @@ func (s simplifier) Visit(node ast.Node) ast.Visitor {
                // can be simplified to: s[a:]
                // if s is "simple enough" (for now we only accept identifiers)
                //
-               // Note: This may not be correct because len may have been redeclared in another
-               //       file belonging to the same package. However, this is extremely unlikely
-               //       and so far (April 2016, after years of supporting this rewrite feature)
+               // Note: This may not be correct because len may have been redeclared in
+               //       the same package. However, this is extremely unlikely and so far
+               //       (April 2022, after years of supporting this rewrite feature)
                //       has never come up, so let's keep it working as is (see also #15153).
+               //
+               // Also note that this code used to use go/ast's object tracking,
+               // which was removed in exchange for go/parser.Mode.SkipObjectResolution.
+               // False positives are extremely unlikely as described above,
+               // and go/ast's object tracking is incomplete in any case.
                if n.Max != nil {
                        // - 3-index slices always require the 2nd and 3rd index
                        break
                }
-               if s, _ := n.X.(*ast.Ident); s != nil && s.Obj != nil {
-                       // the array/slice object is a single, resolved identifier
+               if s, _ := n.X.(*ast.Ident); s != nil {
+                       // the array/slice object is a single identifier
                        if call, _ := n.High.(*ast.CallExpr); call != nil && len(call.Args) == 1 && !call.Ellipsis.IsValid() {
                                // the high expression is a function call with a single argument
-                               if fun, _ := call.Fun.(*ast.Ident); fun != nil && fun.Name == "len" && fun.Obj == nil {
-                                       // the function called is "len" and it is not locally defined; and
-                                       // because we don't have dot imports, it must be the predefined len()
-                                       if arg, _ := call.Args[0].(*ast.Ident); arg != nil && arg.Obj == s.Obj {
+                               if fun, _ := call.Fun.(*ast.Ident); fun != nil && fun.Name == "len" {
+                                       // the function called is "len"
+                                       if arg, _ := call.Args[0].(*ast.Ident); arg != nil && arg.Name == s.Name {
                                                // the len argument is the array/slice object
                                                n.High = nil
                                        }