1
2
3
4
5
6
7 package types2
8
9 import (
10 "cmd/compile/internal/syntax"
11 . "internal/types/errors"
12 "strings"
13 )
14
15
16
17 func (check *Checker) langCompat(lit *syntax.BasicLit) {
18 s := lit.Value
19 if len(s) <= 2 || check.allowVersion(go1_13) {
20 return
21 }
22
23 if strings.Contains(s, "_") {
24 check.versionErrorf(lit, go1_13, "underscore in numeric literal")
25 return
26 }
27 if s[0] != '0' {
28 return
29 }
30 radix := s[1]
31 if radix == 'b' || radix == 'B' {
32 check.versionErrorf(lit, go1_13, "binary literal")
33 return
34 }
35 if radix == 'o' || radix == 'O' {
36 check.versionErrorf(lit, go1_13, "0o/0O-style octal literal")
37 return
38 }
39 if lit.Kind != syntax.IntLit && (radix == 'x' || radix == 'X') {
40 check.versionErrorf(lit, go1_13, "hexadecimal floating-point literal")
41 }
42 }
43
44 func (check *Checker) basicLit(x *operand, e *syntax.BasicLit) {
45 switch e.Kind {
46 case syntax.IntLit, syntax.FloatLit, syntax.ImagLit:
47 check.langCompat(e)
48
49
50
51
52
53
54
55
56
57 const limit = 10000
58 if len(e.Value) > limit {
59 check.errorf(e, InvalidConstVal, "excessively long constant: %s... (%d chars)", e.Value[:10], len(e.Value))
60 x.mode = invalid
61 return
62 }
63 }
64 x.setConst(e.Kind, e.Value)
65 if x.mode == invalid {
66
67
68
69
70 check.errorf(e, InvalidConstVal, "malformed constant: %s", e.Value)
71 x.mode = invalid
72 return
73 }
74
75 x.expr = e
76 check.overflow(x, opPos(x.expr))
77 }
78
79 func (check *Checker) funcLit(x *operand, e *syntax.FuncLit) {
80 if sig, ok := check.typ(e.Type).(*Signature); ok {
81
82
83 sig.scope.pos = e.Pos()
84 sig.scope.end = endPos(e)
85 if !check.conf.IgnoreFuncBodies && e.Body != nil {
86
87
88
89 decl := check.decl
90 iota := check.iota
91
92
93
94
95 check.later(func() {
96 check.funcBody(decl, "<function literal>", sig, e.Body, iota)
97 }).describef(e, "func literal")
98 }
99 x.mode = value
100 x.typ = sig
101 } else {
102 check.errorf(e, InvalidSyntaxTree, "invalid function literal %v", e)
103 x.mode = invalid
104 }
105 }
106
107 func (check *Checker) compositeLit(x *operand, e *syntax.CompositeLit, hint Type) {
108 var typ, base Type
109 var isElem bool
110
111 switch {
112 case e.Type != nil:
113
114
115
116 if atyp, _ := e.Type.(*syntax.ArrayType); atyp != nil && isdddArray(atyp) {
117
118
119
120 typ = &Array{len: -1, elem: check.varType(atyp.Elem)}
121 base = typ
122 break
123 }
124 typ = check.typ(e.Type)
125 base = typ
126
127 case hint != nil:
128
129 typ = hint
130 base = typ
131
132 if b, ok := deref(coreType(base)); ok {
133 base = b
134 }
135 isElem = true
136
137 default:
138
139 check.error(e, UntypedLit, "missing type in composite literal")
140
141 typ = Typ[Invalid]
142 base = typ
143 }
144
145 switch utyp := coreType(base).(type) {
146 case *Struct:
147
148
149 if utyp.fields == nil {
150 check.error(e, InvalidTypeCycle, "invalid recursive type")
151 x.mode = invalid
152 return
153 }
154 if len(e.ElemList) == 0 {
155 break
156 }
157
158
159
160 fields := utyp.fields
161 if _, ok := e.ElemList[0].(*syntax.KeyValueExpr); ok {
162
163 visited := make([]bool, len(fields))
164 for _, e := range e.ElemList {
165 kv, _ := e.(*syntax.KeyValueExpr)
166 if kv == nil {
167 check.error(e, MixedStructLit, "mixture of field:value and value elements in struct literal")
168 continue
169 }
170 key, _ := kv.Key.(*syntax.Name)
171
172
173 check.expr(nil, x, kv.Value)
174 if key == nil {
175 check.errorf(kv, InvalidLitField, "invalid field name %s in struct literal", kv.Key)
176 continue
177 }
178 i := fieldIndex(fields, check.pkg, key.Value, false)
179 if i < 0 {
180 var alt Object
181 if j := fieldIndex(fields, check.pkg, key.Value, true); j >= 0 {
182 alt = fields[j]
183 }
184 msg := check.lookupError(base, key.Value, alt, true)
185 check.error(kv.Key, MissingLitField, msg)
186 continue
187 }
188 fld := fields[i]
189 check.recordUse(key, fld)
190 etyp := fld.typ
191 check.assignment(x, etyp, "struct literal")
192
193 if visited[i] {
194 check.errorf(kv, DuplicateLitField, "duplicate field name %s in struct literal", key.Value)
195 continue
196 }
197 visited[i] = true
198 }
199 } else {
200
201 for i, e := range e.ElemList {
202 if kv, _ := e.(*syntax.KeyValueExpr); kv != nil {
203 check.error(kv, MixedStructLit, "mixture of field:value and value elements in struct literal")
204 continue
205 }
206 check.expr(nil, x, e)
207 if i >= len(fields) {
208 check.errorf(x, InvalidStructLit, "too many values in struct literal of type %s", base)
209 break
210 }
211
212 fld := fields[i]
213 if !fld.Exported() && fld.pkg != check.pkg {
214 check.errorf(x, UnexportedLitField, "implicit assignment to unexported field %s in struct literal of type %s", fld.name, base)
215 continue
216 }
217 etyp := fld.typ
218 check.assignment(x, etyp, "struct literal")
219 }
220 if len(e.ElemList) < len(fields) {
221 check.errorf(inNode(e, e.Rbrace), InvalidStructLit, "too few values in struct literal of type %s", base)
222
223 }
224 }
225
226 case *Array:
227
228
229
230 if utyp.elem == nil {
231 check.error(e, InvalidTypeCycle, "invalid recursive type")
232 x.mode = invalid
233 return
234 }
235 n := check.indexedElts(e.ElemList, utyp.elem, utyp.len)
236
237
238
239
240
241
242
243
244 if utyp.len < 0 {
245 utyp.len = n
246
247
248
249
250 if e.Type != nil {
251 check.recordTypeAndValue(e.Type, typexpr, utyp, nil)
252 }
253 }
254
255 case *Slice:
256
257
258 if utyp.elem == nil {
259 check.error(e, InvalidTypeCycle, "invalid recursive type")
260 x.mode = invalid
261 return
262 }
263 check.indexedElts(e.ElemList, utyp.elem, -1)
264
265 case *Map:
266
267
268 if utyp.key == nil || utyp.elem == nil {
269 check.error(e, InvalidTypeCycle, "invalid recursive type")
270 x.mode = invalid
271 return
272 }
273
274
275
276 keyIsInterface := isNonTypeParamInterface(utyp.key)
277 visited := make(map[any][]Type, len(e.ElemList))
278 for _, e := range e.ElemList {
279 kv, _ := e.(*syntax.KeyValueExpr)
280 if kv == nil {
281 check.error(e, MissingLitKey, "missing key in map literal")
282 continue
283 }
284 check.exprWithHint(x, kv.Key, utyp.key)
285 check.assignment(x, utyp.key, "map literal")
286 if x.mode == invalid {
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.exprWithHint(x, kv.Value, utyp.elem)
310 check.assignment(x, utyp.elem, "map literal")
311 }
312
313 default:
314
315
316 for _, e := range e.ElemList {
317 if kv, _ := e.(*syntax.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 core type)"
334 }
335 check.errorf(e, InvalidLit, "invalid composite literal%s type %s%s", qualifier, typ, cause)
336 x.mode = invalid
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 []syntax.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.(*syntax.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.exprWithHint(&x, eval, typ)
387 check.assignment(&x, typ, "array or slice literal")
388 }
389 return max
390 }
391
View as plain text