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