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