Source file
src/go/types/literals.go
1
2
3
4
5
6
7
8
9
10 package types
11
12 import (
13 "go/ast"
14 "go/token"
15 . "internal/types/errors"
16 "strings"
17 )
18
19
20
21 func (check *Checker) langCompat(lit *ast.BasicLit) {
22 s := lit.Value
23 if len(s) <= 2 || check.allowVersion(go1_13) {
24 return
25 }
26
27 if strings.Contains(s, "_") {
28 check.versionErrorf(lit, go1_13, "underscore in numeric literal")
29 return
30 }
31 if s[0] != '0' {
32 return
33 }
34 radix := s[1]
35 if radix == 'b' || radix == 'B' {
36 check.versionErrorf(lit, go1_13, "binary literal")
37 return
38 }
39 if radix == 'o' || radix == 'O' {
40 check.versionErrorf(lit, go1_13, "0o/0O-style octal literal")
41 return
42 }
43 if lit.Kind != token.INT && (radix == 'x' || radix == 'X') {
44 check.versionErrorf(lit, go1_13, "hexadecimal floating-point literal")
45 }
46 }
47
48 func (check *Checker) basicLit(x *operand, e *ast.BasicLit) {
49 switch e.Kind {
50 case token.INT, token.FLOAT, token.IMAG:
51 check.langCompat(e)
52
53
54
55
56
57
58
59
60
61 const limit = 10000
62 if len(e.Value) > limit {
63 check.errorf(e, InvalidConstVal, "excessively long constant: %s... (%d chars)", e.Value[:10], len(e.Value))
64 x.mode = invalid
65 return
66 }
67 }
68 x.setConst(e.Kind, e.Value)
69 if x.mode == invalid {
70
71
72
73
74 check.errorf(e, InvalidConstVal, "malformed constant: %s", e.Value)
75 x.mode = invalid
76 return
77 }
78
79 x.expr = e
80 check.overflow(x, opPos(x.expr))
81 }
82
83 func (check *Checker) funcLit(x *operand, e *ast.FuncLit) {
84 if sig, ok := check.typ(e.Type).(*Signature); ok {
85
86
87 sig.scope.pos = e.Pos()
88 sig.scope.end = endPos(e)
89 if !check.conf.IgnoreFuncBodies && e.Body != nil {
90
91
92
93 decl := check.decl
94 iota := check.iota
95
96
97
98
99 check.later(func() {
100 check.funcBody(decl, "<function literal>", sig, e.Body, iota)
101 }).describef(e, "func literal")
102 }
103 x.mode = value
104 x.typ = sig
105 } else {
106 check.errorf(e, InvalidSyntaxTree, "invalid function literal %v", e)
107 x.mode = invalid
108 }
109 }
110
111 func (check *Checker) compositeLit(x *operand, e *ast.CompositeLit, hint Type) {
112 var typ, base Type
113 var isElem bool
114
115 switch {
116 case e.Type != nil:
117
118
119
120 if atyp, _ := e.Type.(*ast.ArrayType); atyp != nil && isdddArray(atyp) {
121
122
123
124 typ = &Array{len: -1, elem: check.varType(atyp.Elt)}
125 base = typ
126 break
127 }
128 typ = check.typ(e.Type)
129 base = typ
130
131 case hint != nil:
132
133 typ = hint
134 base = typ
135
136 u, _ := commonUnder(base, nil)
137 if b, ok := deref(u); ok {
138 base = b
139 }
140 isElem = true
141
142 default:
143
144 check.error(e, UntypedLit, "missing type in composite literal")
145
146 typ = Typ[Invalid]
147 base = typ
148 }
149
150 switch u, _ := commonUnder(base, nil); utyp := u.(type) {
151 case *Struct:
152
153
154 if utyp.fields == nil {
155 check.error(e, InvalidTypeCycle, "invalid recursive type")
156 x.mode = invalid
157 return
158 }
159 if len(e.Elts) == 0 {
160 break
161 }
162
163
164
165 fields := utyp.fields
166 if _, ok := e.Elts[0].(*ast.KeyValueExpr); ok {
167
168 visited := make([]bool, len(fields))
169 for _, e := range e.Elts {
170 kv, _ := e.(*ast.KeyValueExpr)
171 if kv == nil {
172 check.error(e, MixedStructLit, "mixture of field:value and value elements in struct literal")
173 continue
174 }
175 key, _ := kv.Key.(*ast.Ident)
176
177
178 check.expr(nil, x, kv.Value)
179 if key == nil {
180 check.errorf(kv, InvalidLitField, "invalid field name %s in struct literal", kv.Key)
181 continue
182 }
183 i := fieldIndex(fields, check.pkg, key.Name, false)
184 if i < 0 {
185 var alt Object
186 if j := fieldIndex(fields, check.pkg, key.Name, true); j >= 0 {
187 alt = fields[j]
188 }
189 msg := check.lookupError(base, key.Name, alt, true)
190 check.error(kv.Key, MissingLitField, msg)
191 continue
192 }
193 fld := fields[i]
194 check.recordUse(key, fld)
195 etyp := fld.typ
196 check.assignment(x, etyp, "struct literal")
197
198 if visited[i] {
199 check.errorf(kv, DuplicateLitField, "duplicate field name %s in struct literal", key.Name)
200 continue
201 }
202 visited[i] = true
203 }
204 } else {
205
206 for i, e := range e.Elts {
207 if kv, _ := e.(*ast.KeyValueExpr); kv != nil {
208 check.error(kv, MixedStructLit, "mixture of field:value and value elements in struct literal")
209 continue
210 }
211 check.expr(nil, x, e)
212 if i >= len(fields) {
213 check.errorf(x, InvalidStructLit, "too many values in struct literal of type %s", base)
214 break
215 }
216
217 fld := fields[i]
218 if !fld.Exported() && fld.pkg != check.pkg {
219 check.errorf(x, UnexportedLitField, "implicit assignment to unexported field %s in struct literal of type %s", fld.name, base)
220 continue
221 }
222 etyp := fld.typ
223 check.assignment(x, etyp, "struct literal")
224 }
225 if len(e.Elts) < len(fields) {
226 check.errorf(inNode(e, e.Rbrace), InvalidStructLit, "too few values in struct literal of type %s", base)
227
228 }
229 }
230
231 case *Array:
232
233
234
235 if utyp.elem == nil {
236 check.error(e, InvalidTypeCycle, "invalid recursive type")
237 x.mode = invalid
238 return
239 }
240 n := check.indexedElts(e.Elts, utyp.elem, utyp.len)
241
242
243
244
245
246
247
248
249 if utyp.len < 0 {
250 utyp.len = n
251
252
253
254
255 if e.Type != nil {
256 check.recordTypeAndValue(e.Type, typexpr, utyp, nil)
257 }
258 }
259
260 case *Slice:
261
262
263 if utyp.elem == nil {
264 check.error(e, InvalidTypeCycle, "invalid recursive type")
265 x.mode = invalid
266 return
267 }
268 check.indexedElts(e.Elts, utyp.elem, -1)
269
270 case *Map:
271
272
273 if utyp.key == nil || utyp.elem == nil {
274 check.error(e, InvalidTypeCycle, "invalid recursive type")
275 x.mode = invalid
276 return
277 }
278
279
280
281 keyIsInterface := isNonTypeParamInterface(utyp.key)
282 visited := make(map[any][]Type, len(e.Elts))
283 for _, e := range e.Elts {
284 kv, _ := e.(*ast.KeyValueExpr)
285 if kv == nil {
286 check.error(e, MissingLitKey, "missing key in map literal")
287 continue
288 }
289 check.exprWithHint(x, kv.Key, utyp.key)
290 check.assignment(x, utyp.key, "map literal")
291 if x.mode == invalid {
292 continue
293 }
294 if x.mode == constant_ {
295 duplicate := false
296 xkey := keyVal(x.val)
297 if keyIsInterface {
298 for _, vtyp := range visited[xkey] {
299 if Identical(vtyp, x.typ) {
300 duplicate = true
301 break
302 }
303 }
304 visited[xkey] = append(visited[xkey], x.typ)
305 } else {
306 _, duplicate = visited[xkey]
307 visited[xkey] = nil
308 }
309 if duplicate {
310 check.errorf(x, DuplicateLitKey, "duplicate key %s in map literal", x.val)
311 continue
312 }
313 }
314 check.exprWithHint(x, kv.Value, utyp.elem)
315 check.assignment(x, utyp.elem, "map literal")
316 }
317
318 default:
319
320
321 for _, e := range e.Elts {
322 if kv, _ := e.(*ast.KeyValueExpr); kv != nil {
323
324
325
326 e = kv.Value
327 }
328 check.use(e)
329 }
330
331 if isValid(utyp) {
332 var qualifier string
333 if isElem {
334 qualifier = " element"
335 }
336 var cause string
337 if utyp == nil {
338 cause = " (no common underlying type)"
339 }
340 check.errorf(e, InvalidLit, "invalid composite literal%s type %s%s", qualifier, typ, cause)
341 x.mode = invalid
342 return
343 }
344 }
345
346 x.mode = value
347 x.typ = typ
348 }
349
350
351
352
353
354 func (check *Checker) indexedElts(elts []ast.Expr, typ Type, length int64) int64 {
355 visited := make(map[int64]bool, len(elts))
356 var index, max int64
357 for _, e := range elts {
358
359 validIndex := false
360 eval := e
361 if kv, _ := e.(*ast.KeyValueExpr); kv != nil {
362 if typ, i := check.index(kv.Key, length); isValid(typ) {
363 if i >= 0 {
364 index = i
365 validIndex = true
366 } else {
367 check.errorf(e, InvalidLitIndex, "index %s must be integer constant", kv.Key)
368 }
369 }
370 eval = kv.Value
371 } else if length >= 0 && index >= length {
372 check.errorf(e, OversizeArrayLit, "index %d is out of bounds (>= %d)", index, length)
373 } else {
374 validIndex = true
375 }
376
377
378 if validIndex {
379 if visited[index] {
380 check.errorf(e, DuplicateLitKey, "duplicate index %d in array or slice literal", index)
381 }
382 visited[index] = true
383 }
384 index++
385 if index > max {
386 max = index
387 }
388
389
390 var x operand
391 check.exprWithHint(&x, eval, typ)
392 check.assignment(&x, typ, "array or slice literal")
393 }
394 return max
395 }
396
View as plain text