Debian Codegraph @

go/analysis/passes/printf/types.go

raw symbols SCIP
1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package printf
6
7 import (
8 "fmt"
9 "go/ast"
10 "go/types"
11
12 "golang.org/x/tools/go/analysis"
13 "golang.org/x/tools/internal/typeparams"
14 )
15
16 var errorType = types.Universe.Lookup("error").Type().Underlying().(*types.Interface)
17
18 // matchArgType reports an error if printf verb t is not appropriate for
19 // operand arg.
20 //
21 // If arg is a type parameter, the verb t must be appropriate for every type in
22 // the type parameter type set.
23 func matchArgType(pass *analysis.Pass, t printfArgType, arg ast.Expr) (reason string, ok bool) {
24 // %v, %T accept any argument type.
25 if t == anyType {
26 return "", true
27 }
28
29 typ := pass.TypesInfo.Types[arg].Type
30 if typ == nil {
31 return "", true // probably a type check problem
32 }
33
34 m := &argMatcher{t: t, seen: make(map[types.Type]bool)}
35 ok = m.match(typ, true)
36 return m.reason, ok
37 }
38
39 // argMatcher recursively matches types against the printfArgType t.
40 //
41 // To short-circuit recursion, it keeps track of types that have already been
42 // matched (or are in the process of being matched) via the seen map. Recursion
43 // arises from the compound types {map,chan,slice} which may be printed with %d
44 // etc. if that is appropriate for their element types, as well as from type
45 // parameters, which are expanded to the constituents of their type set.
46 //
47 // The reason field may be set to report the cause of the mismatch.
48 type argMatcher struct {
49 t printfArgType
50 seen map[types.Type]bool
51 reason string
52 }
53
54 // match checks if typ matches m's printf arg type. If topLevel is true, typ is
55 // the actual type of the printf arg, for which special rules apply. As a
56 // special case, top level type parameters pass topLevel=true when checking for
57 // matches among the constituents of their type set, as type arguments will
58 // replace the type parameter at compile time.
59 func (m *argMatcher) match(typ types.Type, topLevel bool) bool {
60 // %w accepts only errors.
61 if m.t == argError {
62 return types.ConvertibleTo(typ, errorType)
63 }
64
65 // If the type implements fmt.Formatter, we have nothing to check.
66 if isFormatter(typ) {
67 return true
68 }
69
70 // If we can use a string, might arg (dynamically) implement the Stringer or Error interface?
71 if m.t&argString != 0 && isConvertibleToString(typ) {
72 return true
73 }
74
75 if typ, _ := types.Unalias(typ).(*types.TypeParam); typ != nil {
76 // Avoid infinite recursion through type parameters.
77 if m.seen[typ] {
78 return true
79 }
80 m.seen[typ] = true
81 terms, err := typeparams.StructuralTerms(typ)
82 if err != nil {
83 return true // invalid type (possibly an empty type set)
84 }
85
86 if len(terms) == 0 {
87 // No restrictions on the underlying of typ. Type parameters implementing
88 // error, fmt.Formatter, or fmt.Stringer were handled above, and %v and
89 // %T was handled in matchType. We're about to check restrictions the
90 // underlying; if the underlying type is unrestricted there must be an
91 // element of the type set that violates one of the arg type checks
92 // below, so we can safely return false here.
93
94 if m.t == anyType { // anyType must have already been handled.
95 panic("unexpected printfArgType")
96 }
97 return false
98 }
99
100 // Only report a reason if typ is the argument type, otherwise it won't
101 // make sense. Note that it is not sufficient to check if topLevel == here,
102 // as type parameters can have a type set consisting of other type
103 // parameters.
104 reportReason := len(m.seen) == 1
105
106 for _, term := range terms {
107 if !m.match(term.Type(), topLevel) {
108 if reportReason {
109 if term.Tilde() {
110 m.reason = fmt.Sprintf("contains ~%s", term.Type())
111 } else {
112 m.reason = fmt.Sprintf("contains %s", term.Type())
113 }
114 }
115 return false
116 }
117 }
118 return true
119 }
120
121 typ = typ.Underlying()
122 if m.seen[typ] {
123 // We've already considered typ, or are in the process of considering it.
124 // In case we've already considered typ, it must have been valid (else we
125 // would have stopped matching). In case we're in the process of
126 // considering it, we must avoid infinite recursion.
127 //
128 // There are some pathological cases where returning true here is
129 // incorrect, for example `type R struct { F []R }`, but these are
130 // acceptable false negatives.
131 return true
132 }
133 m.seen[typ] = true
134
135 switch typ := typ.(type) {
136 case *types.Signature:
137 return m.t == argPointer
138
139 case *types.Map:
140 if m.t == argPointer {
141 return true
142 }
143 // Recur: map[int]int matches %d.
144 return m.match(typ.Key(), false) && m.match(typ.Elem(), false)
145
146 case *types.Chan:
147 return m.t&argPointer != 0
148
149 case *types.Array:
150 // Same as slice.
151 if types.Identical(typ.Elem().Underlying(), types.Typ[types.Byte]) && m.t&argString != 0 {
152 return true // %s matches []byte
153 }
154 // Recur: []int matches %d.
155 return m.match(typ.Elem(), false)
156
157 case *types.Slice:
158 // Same as array.
159 if types.Identical(typ.Elem().Underlying(), types.Typ[types.Byte]) && m.t&argString != 0 {
160 return true // %s matches []byte
161 }
162 if m.t == argPointer {
163 return true // %p prints a slice's 0th element
164 }
165 // Recur: []int matches %d. But watch out for
166 // type T []T
167 // If the element is a pointer type (type T[]*T), it's handled fine by the Pointer case below.
168 return m.match(typ.Elem(), false)
169
170 case *types.Pointer:
171 // Ugly, but dealing with an edge case: a known pointer to an invalid type,
172 // probably something from a failed import.
173 if typ.Elem() == types.Typ[types.Invalid] {
174 return true // special case
175 }
176 // If it's actually a pointer with %p, it prints as one.
177 if m.t == argPointer {
178 return true
179 }
180
181 if typeparams.IsTypeParam(typ.Elem()) {
182 return true // We don't know whether the logic below applies. Give up.
183 }
184
185 under := typ.Elem().Underlying()
186 switch under.(type) {
187 case *types.Struct: // see below
188 case *types.Array: // see below
189 case *types.Slice: // see below
190 case *types.Map: // see below
191 default:
192 // Check whether the rest can print pointers.
193 return m.t&argPointer != 0
194 }
195 // If it's a top-level pointer to a struct, array, slice, type param, or
196 // map, that's equivalent in our analysis to whether we can
197 // print the type being pointed to. Pointers in nested levels
198 // are not supported to minimize fmt running into loops.
199 if !topLevel {
200 return false
201 }
202 return m.match(under, false)
203
204 case *types.Struct:
205 // report whether all the elements of the struct match the expected type. For
206 // instance, with "%d" all the elements must be printable with the "%d" format.
207 for typf := range typ.Fields() {
208 if !m.match(typf.Type(), false) {
209 return false
210 }
211 if m.t&argString != 0 && !typf.Exported() && isConvertibleToString(typf.Type()) {
212 // Issue #17798: unexported Stringer or error cannot be properly formatted.
213 return false
214 }
215 }
216 return true
217
218 case *types.Interface:
219 // There's little we can do.
220 // Whether any particular verb is valid depends on the argument.
221 // The user may have reasonable prior knowledge of the contents of the interface.
222 return true
223
224 case *types.Basic:
225 switch typ.Kind() {
226 case types.UntypedBool,
227 types.Bool:
228 return m.t&argBool != 0
229
230 case types.Byte:
231 return m.t&(argInt|argByte) != 0
232
233 case types.Rune, types.UntypedRune:
234 return m.t&(argInt|argRune) != 0
235
236 case types.UntypedInt,
237 types.Int,
238 types.Int8,
239 types.Int16,
240 // see case Rune for int32
241 types.Int64,
242 types.Uint,
243 // see case Byte for uint8
244 types.Uint16,
245 types.Uint32,
246 types.Uint64,
247 types.Uintptr:
248 return m.t&argInt != 0
249
250 case types.UntypedFloat,
251 types.Float32,
252 types.Float64:
253 return m.t&argFloat != 0
254
255 case types.UntypedComplex,
256 types.Complex64,
257 types.Complex128:
258 return m.t&argComplex != 0
259
260 case types.UntypedString,
261 types.String:
262 return m.t&argString != 0
263
264 case types.UnsafePointer:
265 return m.t&(argPointer|argInt) != 0
266
267 case types.UntypedNil:
268 return false
269
270 case types.Invalid:
271 return true // Probably a type check problem.
272 }
273 panic("unreachable")
274 }
275
276 return false
277 }
278
279 func isConvertibleToString(typ types.Type) bool {
280 if bt, ok := types.Unalias(typ).(*types.Basic); ok && bt.Kind() == types.UntypedNil {
281 // We explicitly don't want untyped nil, which is
282 // convertible to both of the interfaces below, as it
283 // would just panic anyway.
284 return false
285 }
286 if types.ConvertibleTo(typ, errorType) {
287 return true // via .Error()
288 }
289
290 // Does it implement fmt.Stringer?
291 if obj, _, _ := types.LookupFieldOrMethod(typ, false, nil, "String"); obj != nil {
292 if fn, ok := obj.(*types.Func); ok {
293 sig := fn.Type().(*types.Signature)
294 if sig.Params().Len() == 0 &&
295 sig.Results().Len() == 1 &&
296 sig.Results().At(0).Type() == types.Typ[types.String] {
297 return true
298 }
299 }
300 }
301
302 return false
303 }