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