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