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.invalidate()
65 return
66 }
67 }
68 x.setConst(e.Kind, e.Value)
69 if !x.isValid() {
70
71
72
73
74 check.errorf(e, InvalidConstVal, "malformed constant: %s", e.Value)
75 x.invalidate()
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.invalidate()
108 }
109 }
110
111 func (check *Checker) compositeLit(U Type, 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
132
133 case hint != nil:
134
135 typ = hint
136 base = typ
137
138 u, _ := commonUnder(base, nil)
139 if b, ok := deref(u); ok {
140 base = b
141 }
142 isElem = true
143
144 default:
145
146 if U != nil {
147
148 check.verifyVersionf(e, go1_28, "missing type in composite literal")
149
150 typ = U
151 base = typ
152
153 u, _ := commonUnder(base, nil)
154 if b, ok := deref(u); ok {
155 base = b
156 }
157 } else {
158
159 check.error(e, UntypedLit, "missing type in composite literal")
160
161 typ = Typ[Invalid]
162 base = typ
163 }
164 }
165
166
167 if !check.isComplete(base) {
168 x.invalidate()
169 return
170 }
171
172 switch u, _ := commonUnder(base, nil); utyp := u.(type) {
173 case *Struct:
174 if len(e.Elts) == 0 {
175 break
176 }
177
178
179
180 fields := utyp.fields
181 if _, ok := e.Elts[0].(*ast.KeyValueExpr); ok {
182
183 visited := make(trie[*Var])
184 for _, e := range e.Elts {
185 kv, _ := e.(*ast.KeyValueExpr)
186 if kv == nil {
187 check.error(e, MixedStructLit, "mixture of field:value and value elements in struct literal")
188 continue
189 }
190 key, _ := kv.Key.(*ast.Ident)
191
192
193 check.genericExpr(nil, x, kv.Value, nil)
194 if key == nil {
195 check.errorf(kv, InvalidLitField, "invalid field name %s in struct literal", kv.Key)
196 continue
197 }
198 obj, index, indirect := lookupFieldOrMethod(utyp, false, check.pkg, key.Name, false)
199 if obj == nil {
200 alt, _, _ := lookupFieldOrMethod(utyp, false, check.pkg, key.Name, true)
201 msg := check.lookupError(base, key.Name, alt, true)
202 check.error(kv.Key, MissingLitField, msg)
203 continue
204 }
205 fld, _ := obj.(*Var)
206 if fld == nil {
207 check.errorf(kv.Key, MissingLitField, "%s is not a field", kv.Key)
208 continue
209 }
210 if len(index) > 1 && !check.verifyVersionf(kv.Key, go1_27, "use of promoted field %s in struct literal of type %s", fieldPath(utyp, index), base) {
211 continue
212 }
213 if indirect {
214 check.errorf(kv.Key, InvalidLitField, "invalid implicit pointer indirection to reach %s", kv.Key)
215 continue
216 }
217 check.recordUse(key, fld)
218 etyp := fld.typ
219 check.assignment(x, etyp, "struct literal")
220 if alt, n := visited.insert(index, fld); n != 0 {
221 if fld == alt {
222 check.errorf(kv, DuplicateLitField, "duplicate field name %s in struct literal", fld.name)
223 } else if n < len(index) {
224 check.errorf(kv, DuplicateLitField, "cannot specify promoted field %s and enclosing embedded field %s", fld.name, alt.name)
225 } else {
226 check.errorf(kv, DuplicateLitField, "cannot specify embedded field %s and enclosed promoted field %s", fld.name, alt.name)
227 }
228 }
229 }
230 } else {
231
232 for i, e := range e.Elts {
233 if kv, _ := e.(*ast.KeyValueExpr); kv != nil {
234 check.error(kv, MixedStructLit, "mixture of field:value and value elements in struct literal")
235 continue
236 }
237 check.genericExpr(nil, x, e, nil)
238 if i >= len(fields) {
239 check.errorf(x, InvalidStructLit, "too many values in struct literal of type %s", base)
240 break
241 }
242
243 fld := fields[i]
244 if !fld.Exported() && fld.pkg != check.pkg {
245 check.errorf(x, UnexportedLitField, "implicit assignment to unexported field %s in struct literal of type %s", fld.name, base)
246 continue
247 }
248 etyp := fld.typ
249 check.assignment(x, etyp, "struct literal")
250 }
251 if len(e.Elts) < len(fields) {
252 var hint string
253 for _, fld := range fields {
254 if !fld.Exported() && fld.pkg != check.pkg {
255 hint = " (type has unexported fields - use key:value pairs)"
256 break
257 }
258 }
259 check.errorf(inNode(e, e.Rbrace), InvalidStructLit, "too few values in struct literal of type %s%s", base, hint)
260
261 }
262 }
263
264 case *Array:
265 n := check.indexedElts(e.Elts, utyp.elem, utyp.len)
266
267
268
269
270
271
272
273
274 if utyp.len < 0 {
275 utyp.len = n
276
277
278
279
280 if e.Type != nil {
281 check.recordTypeAndValue(e.Type, typexpr, utyp, nil)
282 }
283 }
284
285 case *Slice:
286 check.indexedElts(e.Elts, utyp.elem, -1)
287
288 case *Map:
289
290
291
292 keyIsInterface := isNonTypeParamInterface(utyp.key)
293 visited := make(map[any][]Type, len(e.Elts))
294 for _, e := range e.Elts {
295 kv, _ := e.(*ast.KeyValueExpr)
296 if kv == nil {
297 check.error(e, MissingLitKey, "missing key in map literal")
298 continue
299 }
300 check.genericExpr(utyp.key, x, kv.Key, utyp.key)
301 check.assignment(x, utyp.key, "map literal")
302 if !x.isValid() {
303 continue
304 }
305 if x.mode() == constant_ {
306 duplicate := false
307 xkey := keyVal(x.val)
308 if keyIsInterface {
309 for _, vtyp := range visited[xkey] {
310 if Identical(vtyp, x.typ()) {
311 duplicate = true
312 break
313 }
314 }
315 visited[xkey] = append(visited[xkey], x.typ())
316 } else {
317 _, duplicate = visited[xkey]
318 visited[xkey] = nil
319 }
320 if duplicate {
321 check.errorf(x, DuplicateLitKey, "duplicate key %s in map literal", x.val)
322 continue
323 }
324 }
325 check.genericExpr(utyp.elem, x, kv.Value, utyp.elem)
326 check.assignment(x, utyp.elem, "map literal")
327 }
328
329 default:
330
331
332 for _, e := range e.Elts {
333 if kv, _ := e.(*ast.KeyValueExpr); kv != nil {
334
335
336
337 e = kv.Value
338 }
339 check.use(e)
340 }
341
342 if isValid(utyp) {
343 var qualifier string
344 if isElem {
345 qualifier = " element"
346 }
347 var cause string
348 if utyp == nil {
349 cause = " (no common underlying type)"
350 }
351 check.errorf(e, InvalidLit, "invalid composite literal%s type %s%s", qualifier, typ, cause)
352 x.invalidate()
353 return
354 }
355 }
356
357 x.mode_ = value
358 x.typ_ = typ
359 }
360
361
362
363
364
365 func (check *Checker) indexedElts(elts []ast.Expr, typ Type, length int64) int64 {
366 visited := make(map[int64]bool, len(elts))
367 var index, max int64
368 for _, e := range elts {
369
370 validIndex := false
371 eval := e
372 if kv, _ := e.(*ast.KeyValueExpr); kv != nil {
373 if typ, i := check.index(kv.Key, length); isValid(typ) {
374 if i >= 0 {
375 index = i
376 validIndex = true
377 } else {
378 check.errorf(e, InvalidLitIndex, "index %s must be integer constant", kv.Key)
379 }
380 }
381 eval = kv.Value
382 } else if length >= 0 && index >= length {
383 check.errorf(e, OversizeArrayLit, "index %d is out of bounds (>= %d)", index, length)
384 } else {
385 validIndex = true
386 }
387
388
389 if validIndex {
390 if visited[index] {
391 check.errorf(e, DuplicateLitKey, "duplicate index %d in array or slice literal", index)
392 }
393 visited[index] = true
394 }
395 index++
396 if index > max {
397 max = index
398 }
399
400
401 var x operand
402 check.genericExpr(typ, &x, eval, typ)
403 check.assignment(&x, typ, "array or slice literal")
404 }
405 return max
406 }
407
View as plain text