Skip to content

Commit 43b469a

Browse files
committed
go/analysis/passes/printf: update logic now that type parameters are
interfaces Now that the underlying of type parameters is their constraint interface, a couple places in the printf analyzer need to be updated: - We need to explicitly exclude type parameters when looking at interfaces in isFormatter. - We need to consider at the element type, not its underlying, when looking at pointers to type parameters Change-Id: I8c6843e001a98d45ff0f30df305e3536335f567e Reviewed-on: https://go-review.googlesource.com/c/tools/+/364678 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 771dabf commit 43b469a

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-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

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)