-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcheckcompositelit.go
329 lines (306 loc) · 9.62 KB
/
checkcompositelit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package eval
import (
"reflect"
"go/ast"
)
func checkCompositeLit(lit *ast.CompositeLit, env Env) (*CompositeLit, []error) {
return checkCompositeLitR(lit, nil, env)
}
// Recursively check composite literals, where a child composite lit's type depends the
// parent's type For example, the expression [][]int{{1,2},{3,4}} contains two
// slice lits, {1,2} and {3,4}, but their types are inferenced from the parent [][]int{}.
func checkCompositeLitR(lit *ast.CompositeLit, t reflect.Type, env Env) (*CompositeLit, []error) {
alit := &CompositeLit{CompositeLit: lit}
// We won't generate any errors here if the given type does not match lit.Type.
// The caller will need to detect the type incompatibility.
if lit.Type != nil {
var errs []error
lit.Type, t, _, errs = checkType(lit.Type, env)
if errs != nil {
return alit, errs
}
} else if t == nil {
return alit, []error{ErrMissingCompositeLitType{alit}}
}
alit.knownType = knownType{t}
if alit.CompositeLit.Elts != nil {
alit.Elts = make([]Expr, len(alit.CompositeLit.Elts))
}
switch t.Kind() {
case reflect.Map:
return checkCompositeLitMap(alit, t, env)
case reflect.Array, reflect.Slice:
return checkCompositeLitArrayOrSlice(alit, t, env)
case reflect.Struct:
return checkCompositeLitStruct(alit, t, env)
default:
panic("eval: unimplemented composite lit " + t.Kind().String())
}
}
func checkCompositeLitMap(lit *CompositeLit, t reflect.Type, env Env) (*CompositeLit, []error) {
var errs []error
kT := t.Key()
// Don't check for duplicate interface{} keys. This is a gc bug
// http://code.google.com/p/go/issues/detail?id=7214
var seen map[interface{}] bool
if kT.Kind() != reflect.Interface {
seen = make(map[interface{}] bool, len(lit.Elts))
}
eltT := t.Elem()
for i := range lit.Elts {
if kv, ok := lit.CompositeLit.Elts[i].(*ast.KeyValueExpr); !ok {
elt, moreErrs := CheckExpr(lit.CompositeLit.Elts[i], env)
lit.Elts[i] = elt
if moreErrs != nil {
errs = append(errs, moreErrs...)
}
errs = append(errs, ErrMissingMapKey{elt})
} else {
k, ok, moreErrs := checkExprAssignableTo(kv.Key, kT, env)
if !ok {
if len(k.KnownType()) != 0 {
kF := fakeCheckExpr(kv.Key, env)
kF.setKnownType(knownType(k.KnownType()))
errs = append(errs, ErrBadMapKey{kF, kT})
}
} else {
errs = append(errs, moreErrs...)
}
if seen != nil && k.IsConst() {
var constKey interface{}
if k.KnownType()[0] == ConstNil {
constKey = nil
} else if cT, ok := k.KnownType()[0].(ConstType); ok {
c, _ := promoteConstToTyped(cT, constValue(k.Const()),
cT.DefaultPromotion(), k)
constKey = reflect.Value(c).Interface()
} else {
constKey = k.Const().Interface()
}
if seen[constKey] {
errs = append(errs, ErrDuplicateMapKey{k})
}
seen[constKey] = true
}
v, moreErrs := checkMapValue(kv.Value, eltT, env)
if moreErrs != nil {
errs = append(errs, moreErrs...)
}
lit.Elts[i] = &KeyValueExpr{KeyValueExpr: kv, Key: k, Value: v}
}
}
return lit, errs
}
func checkCompositeLitArrayOrSlice(lit *CompositeLit, t reflect.Type, env Env) (*CompositeLit, []error) {
var errs, moreErrs []error
eltT := t.Elem()
maxIndex, curIndex := -1, 0
outOfBounds := false
length := -1
if t.Kind() == reflect.Array {
length = t.Len()
}
used := make(map[int] bool, len(lit.Elts))
// Check all keys are valid and calculate array or slice length.
// Elements with key are placed at the keyed position.
// Elements without are placed in the next position.
// For example, []int{1, 2:1, 1} -> [1, 0, 1, 1]
for i := range lit.Elts {
var value ast.Expr
var key Expr
var avalue *Expr
skipIndexChecks := false
kv, ok := lit.CompositeLit.Elts[i].(*ast.KeyValueExpr)
if !ok {
avalue, value = &lit.Elts[i], lit.CompositeLit.Elts[i]
} else {
akv := &KeyValueExpr{KeyValueExpr: kv}
lit.Elts[i] = akv
avalue, value = &akv.Value, kv.Value
// Check the array key
var index int
key, index, ok, moreErrs = checkArrayIndex(kv.Key, env);
if !ok || moreErrs != nil {
// NOTE[crc] Haven't checked the gc implementation, but
// from experimentation it seems that only undefined
// idents are reported. This filter should perhaps be part
// of checkArrayIndex
for _, err := range moreErrs {
if _, ok := err.(ErrUndefined); ok {
errs = append(errs, err)
}
}
errs = append(errs, ErrBadArrayKey{key})
// Don't include this element in index calculations
curIndex -= 1
skipIndexChecks = true
} else {
lit.indices = append(lit.indices, struct{pos, index int}{i, index})
curIndex = index
}
}
// finally check the value
v, moreErrs := checkArrayValue(value, eltT, env)
*avalue = v
// These errors slide in before the check errors, but we need v
if !skipIndexChecks {
if maxIndex < curIndex {
maxIndex = curIndex
}
if !outOfBounds && length != -1 && curIndex >= length {
outOfBounds = true
errs = append(errs, ErrArrayKeyOutOfBounds{v, t, curIndex})
}
// has this index been used already
if used[curIndex] {
errs = append(errs, ErrDuplicateArrayKey{key, curIndex})
}
used[curIndex] = true
}
// Add the check errors
if moreErrs != nil {
errs = append(errs, moreErrs...)
}
curIndex += 1
}
lit.indices = append(lit.indices, struct{pos, index int}{-1, -1})
if length == -1 {
lit.length = maxIndex + 1
} else {
lit.length = length
}
return lit, errs
}
func checkCompositeLitStruct(lit *CompositeLit, t reflect.Type, env Env) (*CompositeLit, []error) {
var errs, moreErrs []error
// X{} is treated as if it has zero KeyValue'd elements, i.e. unspecified
// elements are set to zero. This is always valid
if len(lit.Elts) == 0 {
return lit, nil
}
// gc first checks if there are ANY keys present, and then decides how
// to process the initialisers.
keysPresent := false
for _, elt := range lit.CompositeLit.Elts {
_, ok := elt.(*ast.KeyValueExpr)
keysPresent = keysPresent || ok
}
if keysPresent {
seen := make(map[string] bool, len(lit.Elts))
mixed := false
for i := 0; i < len(lit.Elts); i += 1 {
kv, ok := lit.CompositeLit.Elts[i].(*ast.KeyValueExpr)
if !ok {
elt := fakeCheckExpr(lit.CompositeLit.Elts[i], env)
lit.Elts[i] = elt
if !mixed {
// This error only gets reported once
mixed = true
errs = append(errs, ErrMixedStructValues{elt})
}
continue
}
key := fakeCheckExpr(kv.Key, env)
var value Expr
// Check the key is a struct member
if ident, ok := key.(*Ident); !ok {
value = fakeCheckExpr(kv.Value, env)
errs = append(errs, ErrInvalidStructField{key})
} else {
name := ident.Name
if field, ok := t.FieldByName(name); !ok {
value = fakeCheckExpr(kv.Value, env)
errs = append(errs, ErrUnknownStructField{key, t, name})
} else {
if seen[name] {
errs = append(errs, ErrDuplicateStructField{ident, name})
}
seen[name] = true
lit.fields = append(lit.fields, field.Index[0])
value, moreErrs = checkStructField(kv.Value, field, env)
if moreErrs != nil {
errs = append(errs, moreErrs...)
}
}
}
lit.Elts[i] = &KeyValueExpr{KeyValueExpr: kv, Key: key, Value: value}
}
} else {
numFields := t.NumField()
var i int
for i = 0; i < numFields && i < len(lit.Elts); i += 1 {
field := t.Field(i)
lit.Elts[i], moreErrs = checkStructField(lit.CompositeLit.Elts[i], field, env)
if moreErrs != nil {
errs = append(errs, moreErrs...)
}
lit.fields = append(lit.fields, i)
}
if numFields != len(lit.Elts) {
errs = append(errs, ErrWrongNumberOfStructValues{lit})
}
// Remaining fields are type checked reguardless of use
for ; i < len(lit.Elts); i += 1 {
lit.Elts[i], moreErrs = CheckExpr(lit.CompositeLit.Elts[i], env)
if moreErrs != nil {
errs = append(errs, moreErrs...)
}
}
}
return lit, errs
}
func checkMapValue(expr ast.Expr, eltT reflect.Type, env Env) (Expr, []error) {
switch eltT.Kind() {
case reflect.Array, reflect.Slice, reflect.Map, reflect.Struct:
if lit, ok := expr.(*ast.CompositeLit); ok {
return checkCompositeLitR(lit, eltT, env)
}
}
aexpr, ok, errs := checkExprAssignableTo(expr, eltT, env)
if !ok {
// NOTE[crc] this hack removes conversion errors from consts other
// than strings and nil to match the output of gc.
if ccerr, ok := errs[0].(ErrBadConstConversion); ok {
if ccerr.from == ConstNil {
// No ErrBadMapValue for nil
return aexpr, errs
} else if ccerr.from != ConstString {
// gc implementation only displays string conversion errors
errs = nil
}
}
errs = append(errs, ErrBadMapValue{aexpr, eltT})
}
return aexpr, errs
}
func checkArrayValue(expr ast.Expr, eltT reflect.Type, env Env) (Expr, []error) {
switch eltT.Kind() {
case reflect.Array, reflect.Slice, reflect.Map, reflect.Struct:
if lit, ok := expr.(*ast.CompositeLit); ok {
return checkCompositeLitR(lit, eltT, env)
}
}
aexpr, ok, errs := checkExprAssignableTo(expr, eltT, env)
if !ok {
// NOTE[crc] this hack removes conversion errors from consts other
// than strings and nil to match the output of gc.
if ccerr, ok := errs[0].(ErrBadConstConversion); ok {
if ccerr.from == ConstNil {
// No ErrBadArrayValue for nil
return aexpr, errs
} else if ccerr.from != ConstString {
// gc implementation only displays string conversion errors
errs = nil
}
}
errs = append(errs, ErrBadArrayValue{aexpr, eltT})
}
return aexpr, errs
}
func checkStructField(expr ast.Expr, field reflect.StructField, env Env) (Expr, []error) {
aexpr, ok, errs := checkExprAssignableTo(expr, field.Type, env)
if !ok {
errs = append([]error{}, ErrBadStructValue{aexpr, field.Type})
}
return aexpr, errs
}