]> Cypherpunks repositories - gostls13.git/commitdiff
all: update vendored golang.org/x/tools
authorRobert Findley <rfindley@google.com>
Tue, 25 Jan 2022 22:31:52 +0000 (17:31 -0500)
committerRobert Findley <rfindley@google.com>
Tue, 25 Jan 2022 22:56:45 +0000 (22:56 +0000)
Update the vendored x/tools to pick up CL 380014, which updates the
ifaceassert vet analyzer to remove spurious errors for assertions
involving interfaces with type parameters.

This also picks up some superficial changes related to refactoring of
the x/tools/internal/typeparams API.

The following commands were used:

  go get -d golang.org/x/tools@master
  go mod tidy
  go mod vendor

Fixes #50658

Change-Id: I2f612fd186a1a260cab21860b192c9f6dc3f560f
Reviewed-on: https://go-review.googlesource.com/c/go/+/380777
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Tim King <taking@google.com>
13 files changed:
src/cmd/go.mod
src/cmd/go.sum
src/cmd/vendor/golang.org/x/tools/go/analysis/passes/ifaceassert/ifaceassert.go
src/cmd/vendor/golang.org/x/tools/go/analysis/passes/ifaceassert/parameterized.go [new file with mode: 0644]
src/cmd/vendor/golang.org/x/tools/go/analysis/passes/nilfunc/nilfunc.go
src/cmd/vendor/golang.org/x/tools/go/analysis/passes/testinggoroutine/testinggoroutine.go
src/cmd/vendor/golang.org/x/tools/go/analysis/passes/unusedresult/unusedresult.go
src/cmd/vendor/golang.org/x/tools/go/types/typeutil/callee.go
src/cmd/vendor/golang.org/x/tools/internal/lsp/fuzzy/symbol.go
src/cmd/vendor/golang.org/x/tools/internal/typeparams/common.go
src/cmd/vendor/golang.org/x/tools/internal/typeparams/typeparams_go117.go
src/cmd/vendor/golang.org/x/tools/internal/typeparams/typeparams_go118.go
src/cmd/vendor/modules.txt

index 6684fbf95d9b9118ac3e6d6dffb6b69483f8b0e2..48fc888f9492d07e43f774817c955d8608d240db 100644 (file)
@@ -8,7 +8,7 @@ require (
        golang.org/x/mod v0.6.0-dev.0.20211102181907-3a5865c02020
        golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
        golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
-       golang.org/x/tools v0.1.9-0.20211207220608-fd2bfb79a16a
+       golang.org/x/tools v0.1.9-0.20220124164225-97de9ec46646
 )
 
 require (
index 9e202354976530eed517969f61845c578287cfe1..4a5479f881b7f68c3a3ba164797b12341842db93 100644 (file)
@@ -18,7 +18,7 @@ golang.org/x/sys v0.0.0-20211205182925-97ca703d548d h1:FjkYO/PPp4Wi0EAUOVLxePm7q
 golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
 golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
-golang.org/x/tools v0.1.9-0.20211207220608-fd2bfb79a16a h1:G+TZ7v63o8mn+LBWOdnHaiypIhcgFZ6BDDnyX+RXDYg=
-golang.org/x/tools v0.1.9-0.20211207220608-fd2bfb79a16a/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
+golang.org/x/tools v0.1.9-0.20220124164225-97de9ec46646 h1:f8aekWvlQQ8ZhD8SL7lOu18dtWslZYl029PN2F0VnS4=
+golang.org/x/tools v0.1.9-0.20220124164225-97de9ec46646/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
 golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
 golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
index fd2285332ccec2dc6cf503b1e7847ca873af74c6..30130f63ea6bf28c9956a2f2ed63779d39abd54b 100644 (file)
@@ -51,6 +51,12 @@ func assertableTo(v, t types.Type) *types.Func {
        if V == nil || T == nil {
                return nil
        }
+
+       // Mitigations for interface comparisons and generics.
+       // TODO(https://github.com/golang/go/issues/50658): Support more precise conclusion.
+       if isParameterized(V) || isParameterized(T) {
+               return nil
+       }
        if f, wrongType := types.MissingMethod(V, T, false); wrongType {
                return f
        }
diff --git a/src/cmd/vendor/golang.org/x/tools/go/analysis/passes/ifaceassert/parameterized.go b/src/cmd/vendor/golang.org/x/tools/go/analysis/passes/ifaceassert/parameterized.go
new file mode 100644 (file)
index 0000000..1285ecf
--- /dev/null
@@ -0,0 +1,112 @@
+// Copyright 2022 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.
+package ifaceassert
+
+import (
+       "go/types"
+
+       "golang.org/x/tools/internal/typeparams"
+)
+
+// isParameterized reports whether typ contains any of the type parameters of tparams.
+//
+// NOTE: Adapted from go/types/infer.go. If that is exported in a future release remove this copy.
+func isParameterized(typ types.Type) bool {
+       w := tpWalker{
+               seen: make(map[types.Type]bool),
+       }
+       return w.isParameterized(typ)
+}
+
+type tpWalker struct {
+       seen map[types.Type]bool
+}
+
+func (w *tpWalker) isParameterized(typ types.Type) (res bool) {
+       // detect cycles
+       if x, ok := w.seen[typ]; ok {
+               return x
+       }
+       w.seen[typ] = false
+       defer func() {
+               w.seen[typ] = res
+       }()
+
+       switch t := typ.(type) {
+       case nil, *types.Basic: // TODO(gri) should nil be handled here?
+               break
+
+       case *types.Array:
+               return w.isParameterized(t.Elem())
+
+       case *types.Slice:
+               return w.isParameterized(t.Elem())
+
+       case *types.Struct:
+               for i, n := 0, t.NumFields(); i < n; i++ {
+                       if w.isParameterized(t.Field(i).Type()) {
+                               return true
+                       }
+               }
+
+       case *types.Pointer:
+               return w.isParameterized(t.Elem())
+
+       case *types.Tuple:
+               n := t.Len()
+               for i := 0; i < n; i++ {
+                       if w.isParameterized(t.At(i).Type()) {
+                               return true
+                       }
+               }
+
+       case *types.Signature:
+               // t.tparams may not be nil if we are looking at a signature
+               // of a generic function type (or an interface method) that is
+               // part of the type we're testing. We don't care about these type
+               // parameters.
+               // Similarly, the receiver of a method may declare (rather then
+               // use) type parameters, we don't care about those either.
+               // Thus, we only need to look at the input and result parameters.
+               return w.isParameterized(t.Params()) || w.isParameterized(t.Results())
+
+       case *types.Interface:
+               for i, n := 0, t.NumMethods(); i < n; i++ {
+                       if w.isParameterized(t.Method(i).Type()) {
+                               return true
+                       }
+               }
+               terms, err := typeparams.InterfaceTermSet(t)
+               if err != nil {
+                       panic(err)
+               }
+               for _, term := range terms {
+                       if w.isParameterized(term.Type()) {
+                               return true
+                       }
+               }
+
+       case *types.Map:
+               return w.isParameterized(t.Key()) || w.isParameterized(t.Elem())
+
+       case *types.Chan:
+               return w.isParameterized(t.Elem())
+
+       case *types.Named:
+               list := typeparams.NamedTypeArgs(t)
+               for i, n := 0, list.Len(); i < n; i++ {
+                       if w.isParameterized(list.At(i)) {
+                               return true
+                       }
+               }
+
+       case *typeparams.TypeParam:
+               return true
+
+       default:
+               panic(t) // unreachable
+       }
+
+       return false
+}
index 850f6f8faed38f56dc453dff8a20ffaf91749204..e4c66df6d6dca306a97b6b0cef07c7a5e625c6a9 100644 (file)
@@ -62,7 +62,8 @@ func run(pass *analysis.Pass) (interface{}, error) {
                        obj = pass.TypesInfo.Uses[v.Sel]
                case *ast.IndexExpr, *typeparams.IndexListExpr:
                        // Check generic functions such as "f[T1,T2]".
-                       if id, ok := typeparams.GetIndexExprData(v).X.(*ast.Ident); ok {
+                       x, _, _, _ := typeparams.UnpackIndexExpr(v)
+                       if id, ok := x.(*ast.Ident); ok {
                                obj = pass.TypesInfo.Uses[id]
                        }
                default:
index 3d4bd49085281daf0a2d727612b241b9c8a465d0..7ea8f77e335a0c6675592bc84f3f700a33160034 100644 (file)
@@ -127,11 +127,8 @@ func typeIsTestingDotTOrB(expr ast.Expr) (string, bool) {
 func goStmtFun(goStmt *ast.GoStmt) ast.Node {
        switch fun := goStmt.Call.Fun.(type) {
        case *ast.IndexExpr, *typeparams.IndexListExpr:
-               ix := typeparams.GetIndexExprData(fun)
-               if ix == nil {
-                       break
-               }
-               id, _ := ix.X.(*ast.Ident)
+               x, _, _, _ := typeparams.UnpackIndexExpr(fun)
+               id, _ := x.(*ast.Ident)
                if id == nil {
                        break
                }
index fd94508f883a13ef683ad453e32f08eb76b0a134..06747ba72b9df34aeac317de187181de88fbd9da 100644 (file)
@@ -71,9 +71,9 @@ func run(pass *analysis.Pass) (interface{}, error) {
                        return // a conversion, not a call
                }
 
-               index := typeparams.GetIndexExprData(fun)
-               if index != nil {
-                       fun = index.X // If this is generic function or method call, skip the instantiation arguments
+               x, _, _, _ := typeparams.UnpackIndexExpr(fun)
+               if x != nil {
+                       fun = x // If this is generic function or method call, skip the instantiation arguments
                }
 
                selector, ok := fun.(*ast.SelectorExpr)
index 2b8960332d1fd01189faf107b54b5bf309abf474..90b3ab0e21cb72d26547964fe173665a10b61ec0 100644 (file)
@@ -27,8 +27,7 @@ func Callee(info *types.Info, call *ast.CallExpr) types.Object {
                // it is a *types.Func and not a *types.Var.
                // Example: Don't match a slice m within the expression `m[0]()`.
                isInstance = true
-               ix := typeparams.GetIndexExprData(fun)
-               fun = ix.X
+               fun, _, _, _ = typeparams.UnpackIndexExpr(fun)
        }
 
        var obj types.Object
index 062f491fb5ce6e79b47ad6aaf6afbcf20ba11c58..df9fbd51410b5993b9e1ee264279eede88e85202 100644 (file)
@@ -49,11 +49,6 @@ const (
 //
 // Currently this matcher only accepts case-insensitive fuzzy patterns.
 //
-// TODO(rfindley):
-//  - implement smart-casing
-//  - implement space-separated groups
-//  - implement ', ^, and $ modifiers
-//
 // An empty pattern matches no input.
 func NewSymbolMatcher(pattern string) *SymbolMatcher {
        m := &SymbolMatcher{}
@@ -176,7 +171,12 @@ input:
        //   1. 1.0 if the character starts a segment, .8 if the character start a
        //      mid-segment word, otherwise 0.6. This carries over to immediately
        //      following characters.
-       //   2. 1.0 if the character is part of the last segment, otherwise
+       //   2. For the final character match, the multiplier from (1) is reduced to
+       //     .8 if the next character in the input is a mid-segment word, or 0.6 if
+       //      the next character in the input is not a word or segment start. This
+       //      ensures that we favor whole-word or whole-segment matches over prefix
+       //      matches.
+       //   3. 1.0 if the character is part of the last segment, otherwise
        //      1.0-.2*<segments from the right>, with a max segment count of 3.
        //
        // This is a very naive algorithm, but it is fast. There's lots of prior art
@@ -211,8 +211,20 @@ input:
                        case m.roles[ii]&wordStart != 0 && wordStreak > streakBonus:
                                streakBonus = wordStreak
                        }
+                       finalChar := pi >= m.patternLen
+                       // finalCost := 1.0
+                       if finalChar && streakBonus > noStreak {
+                               switch {
+                               case ii == inputLen-1 || m.roles[ii+1]&segmentStart != 0:
+                                       // Full segment: no reduction
+                               case m.roles[ii+1]&wordStart != 0:
+                                       streakBonus = wordStreak
+                               default:
+                                       streakBonus = noStreak
+                               }
+                       }
                        totScore += streakBonus * (1.0 - float64(m.segments[ii])*perSegment)
-                       if pi >= m.patternLen {
+                       if finalChar {
                                break
                        }
                } else {
index 961d036fdb018e7adf5f3cd96b6447a7324b7435..1222764b6a312062b8e89ac690d39d83fbb87fbc 100644 (file)
@@ -2,12 +2,25 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// Package typeparams provides functions to work indirectly with type parameter
-// data stored in go/ast and go/types objects, while these API are guarded by a
-// build constraint.
+// Package typeparams contains common utilities for writing tools that interact
+// with generic Go code, as introduced with Go 1.18.
 //
-// This package exists to make it easier for tools to work with generic code,
-// while also compiling against older Go versions.
+// Many of the types and functions in this package are proxies for the new APIs
+// introduced in the standard library with Go 1.18. For example, the
+// typeparams.Union type is an alias for go/types.Union, and the ForTypeSpec
+// function returns the value of the go/ast.TypeSpec.TypeParams field. At Go
+// versions older than 1.18 these helpers are implemented as stubs, allowing
+// users of this package to write code that handles generic constructs inline,
+// even if the Go version being used to compile does not support generics.
+//
+// Additionally, this package contains common utilities for working with the
+// new generic constructs, to supplement the standard library APIs. Notably,
+// the StructuralTerms API computes a minimal representation of the structural
+// restrictions on a type parameter. In the future, this API may be available
+// from go/types.
+//
+// See the example/README.md for a more detailed guide on how to update tools
+// to support generics.
 package typeparams
 
 import (
@@ -16,13 +29,47 @@ import (
        "go/types"
 )
 
-// A IndexExprData holds data from both ast.IndexExpr and the new
-// ast.MultiIndexExpr, which was introduced in Go 1.18.
-type IndexExprData struct {
-       X       ast.Expr   // expression
-       Lbrack  token.Pos  // position of "["
-       Indices []ast.Expr // index expressions
-       Rbrack  token.Pos  // position of "]"
+// UnpackIndexExpr extracts data from AST nodes that represent index
+// expressions.
+//
+// For an ast.IndexExpr, the resulting indices slice will contain exactly one
+// index expression. For an ast.IndexListExpr (go1.18+), it may have a variable
+// number of index expressions.
+//
+// For nodes that don't represent index expressions, the first return value of
+// UnpackIndexExpr will be nil.
+func UnpackIndexExpr(n ast.Node) (x ast.Expr, lbrack token.Pos, indices []ast.Expr, rbrack token.Pos) {
+       switch e := n.(type) {
+       case *ast.IndexExpr:
+               return e.X, e.Lbrack, []ast.Expr{e.Index}, e.Rbrack
+       case *IndexListExpr:
+               return e.X, e.Lbrack, e.Indices, e.Rbrack
+       }
+       return nil, token.NoPos, nil, token.NoPos
+}
+
+// PackIndexExpr returns an *ast.IndexExpr or *ast.IndexListExpr, depending on
+// the cardinality of indices. Calling PackIndexExpr with len(indices) == 0
+// will panic.
+func PackIndexExpr(x ast.Expr, lbrack token.Pos, indices []ast.Expr, rbrack token.Pos) ast.Expr {
+       switch len(indices) {
+       case 0:
+               panic("empty indices")
+       case 1:
+               return &ast.IndexExpr{
+                       X:      x,
+                       Lbrack: lbrack,
+                       Index:  indices[0],
+                       Rbrack: rbrack,
+               }
+       default:
+               return &IndexListExpr{
+                       X:       x,
+                       Lbrack:  lbrack,
+                       Indices: indices,
+                       Rbrack:  rbrack,
+               }
+       }
 }
 
 // IsTypeParam reports whether t is a type parameter.
index e509daf7be7f35f56df91c80c8dc8ebc41a153bf..5fd3fc351561ee628c92b94cbd33744f567d9306 100644 (file)
@@ -17,38 +17,6 @@ func unsupported() {
        panic("type parameters are unsupported at this go version")
 }
 
-// GetIndexExprData extracts data from *ast.IndexExpr nodes.
-// For other nodes, GetIndexExprData returns nil.
-func GetIndexExprData(n ast.Node) *IndexExprData {
-       if e, _ := n.(*ast.IndexExpr); e != nil {
-               return &IndexExprData{
-                       X:       e.X,
-                       Lbrack:  e.Lbrack,
-                       Indices: []ast.Expr{e.Index},
-                       Rbrack:  e.Rbrack,
-               }
-       }
-       return nil
-}
-
-// PackIndexExpr returns an *ast.IndexExpr with the given index.
-// Calling PackIndexExpr with len(indices) != 1 will panic.
-func PackIndexExpr(x ast.Expr, lbrack token.Pos, indices []ast.Expr, rbrack token.Pos) ast.Expr {
-       switch len(indices) {
-       case 0:
-               panic("empty indices")
-       case 1:
-               return &ast.IndexExpr{
-                       X:      x,
-                       Lbrack: lbrack,
-                       Index:  indices[0],
-                       Rbrack: rbrack,
-               }
-       default:
-               panic("cannot pack multiple indices at this go version")
-       }
-}
-
 // IndexListExpr is a placeholder type, as type parameters are not supported at
 // this Go version. Its methods panic on use.
 type IndexListExpr struct {
index e45896fb02249f5652f495fe9329d860a70046cb..7470aed8c998278a9b2dde6cd144ab9e345ce02f 100644 (file)
@@ -9,59 +9,9 @@ package typeparams
 
 import (
        "go/ast"
-       "go/token"
        "go/types"
 )
 
-// GetIndexExprData extracts data from AST nodes that represent index
-// expressions.
-//
-// For an ast.IndexExpr, the resulting IndexExprData will have exactly one
-// index expression. For an ast.IndexListExpr (go1.18+), it may have a
-// variable number of index expressions.
-//
-// For nodes that don't represent index expressions, GetIndexExprData returns
-// nil.
-// TODO(rfindley): remove this function in favor of using the alias below.
-func GetIndexExprData(n ast.Node) *IndexExprData {
-       switch e := n.(type) {
-       case *ast.IndexExpr:
-               return &IndexExprData{
-                       X:       e.X,
-                       Lbrack:  e.Lbrack,
-                       Indices: []ast.Expr{e.Index},
-                       Rbrack:  e.Rbrack,
-               }
-       case *ast.IndexListExpr:
-               return (*IndexExprData)(e)
-       }
-       return nil
-}
-
-// PackIndexExpr returns an *ast.IndexExpr or *ast.IndexListExpr, depending on
-// the cardinality of indices. Calling PackIndexExpr with len(indices) == 0
-// will panic.
-func PackIndexExpr(x ast.Expr, lbrack token.Pos, indices []ast.Expr, rbrack token.Pos) ast.Expr {
-       switch len(indices) {
-       case 0:
-               panic("empty indices")
-       case 1:
-               return &ast.IndexExpr{
-                       X:      x,
-                       Lbrack: lbrack,
-                       Index:  indices[0],
-                       Rbrack: rbrack,
-               }
-       default:
-               return &ast.IndexListExpr{
-                       X:       x,
-                       Lbrack:  lbrack,
-                       Indices: indices,
-                       Rbrack:  rbrack,
-               }
-       }
-}
-
 // IndexListExpr is an alias for ast.IndexListExpr.
 type IndexListExpr = ast.IndexListExpr
 
index 0762dee8f2a731be6486a8ebba42fa6a0edcd79a..f2cd884b820ce95fa4584709068e23161e33d94b 100644 (file)
@@ -51,7 +51,7 @@ golang.org/x/sys/windows
 # golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
 ## explicit; go 1.17
 golang.org/x/term
-# golang.org/x/tools v0.1.9-0.20211207220608-fd2bfb79a16a
+# golang.org/x/tools v0.1.9-0.20220124164225-97de9ec46646
 ## explicit; go 1.17
 golang.org/x/tools/cover
 golang.org/x/tools/go/analysis