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