Skip to content

Commit f7f495a

Browse files
committed
all: merge master (43b469a) into gopls-release-branch.0.7
Merge List: + 2021-11-17 43b469a go/analysis/passes/printf: update logic now that type parameters are interfaces + 2021-11-17 771dabf gopls/internal/regtest/misc: skip TestInconsistentVendoring on Windows + 2021-11-17 7d6c71f internal/lsp/source/completion: avoid invalid AST in enclosingSignature Change-Id: I7926670ad4f2897ac4951f08ad37df9b39222847
2 parents e1ca49e + 43b469a commit f7f495a

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

go/analysis/passes/printf/printf.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
2626
"golang.org/x/tools/go/ast/inspector"
2727
"golang.org/x/tools/go/types/typeutil"
28+
"golang.org/x/tools/internal/typeparams"
2829
)
2930

3031
func init() {
@@ -520,7 +521,12 @@ func printfNameAndKind(pass *analysis.Pass, call *ast.CallExpr) (fn *types.Func,
520521
func isFormatter(typ types.Type) bool {
521522
// If the type is an interface, the value it holds might satisfy fmt.Formatter.
522523
if _, ok := typ.Underlying().(*types.Interface); ok {
523-
return true
524+
// Don't assume type parameters could be formatters. With the greater
525+
// expressiveness of constraint interface syntax we expect more type safety
526+
// when using type parameters.
527+
if !typeparams.IsTypeParam(typ) {
528+
return true
529+
}
524530
}
525531
obj, _, _ := types.LookupFieldOrMethod(typ, false, nil, "Format")
526532
fn, ok := obj.(*types.Func)

go/analysis/passes/printf/types.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,12 @@ func (m *argMatcher) match(typ types.Type, topLevel bool) bool {
178178
return true
179179
}
180180

181+
if typeparams.IsTypeParam(typ.Elem()) {
182+
return true // We don't know whether the logic below applies. Give up.
183+
}
184+
181185
under := typ.Elem().Underlying()
182186
switch under.(type) {
183-
case *typeparams.TypeParam:
184-
return true // We don't know whether the logic below applies. Give up.
185187
case *types.Struct: // see below
186188
case *types.Array: // see below
187189
case *types.Slice: // see below

gopls/internal/regtest/misc/vendor_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package misc
66

77
import (
8+
"runtime"
89
"testing"
910

1011
. "golang.org/x/tools/internal/lsp/regtest"
@@ -26,6 +27,9 @@ var Goodbye error
2627

2728
func TestInconsistentVendoring(t *testing.T) {
2829
testenv.NeedsGo1Point(t, 14)
30+
if runtime.GOOS == "windows" {
31+
t.Skipf("skipping test due to flakiness on Windows: https://golang.org/issue/49646")
32+
}
2933

3034
const pkgThatUsesVendoring = `
3135
-- go.mod --

internal/lsp/source/completion/completion.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,12 @@ func enclosingFunction(path []ast.Node, info *types.Info) *funcInfo {
16941694
}
16951695
case *ast.FuncLit:
16961696
if typ, ok := info.Types[t]; ok {
1697+
if sig, _ := typ.Type.(*types.Signature); sig == nil {
1698+
// golang/go#49397: it should not be possible, but we somehow arrived
1699+
// here with a non-signature type, most likely due to AST mangling
1700+
// such that node.Type is not a FuncType.
1701+
return nil
1702+
}
16971703
return &funcInfo{
16981704
sig: typ.Type.(*types.Signature),
16991705
body: t.Body,

internal/typeparams/common.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ package typeparams
1313
import (
1414
"go/ast"
1515
"go/token"
16+
"go/types"
1617
)
1718

1819
// A IndexExprData holds data from both ast.IndexExpr and the new
@@ -23,3 +24,9 @@ type IndexExprData struct {
2324
Indices []ast.Expr // index expressions
2425
Rbrack token.Pos // position of "]"
2526
}
27+
28+
// IsTypeParam reports whether t is a type parameter.
29+
func IsTypeParam(t types.Type) bool {
30+
_, ok := t.(*TypeParam)
31+
return ok
32+
}

0 commit comments

Comments
 (0)