Skip to content

Commit b4d6293

Browse files
committed
Improve MemberNode checker
1 parent 81bb58d commit b4d6293

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

checker/checker.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,13 +387,15 @@ func (v *visitor) MemberNode(node *ast.MemberNode) (reflect.Type, info) {
387387
return anyType, info{}
388388

389389
case reflect.Map:
390-
// TODO: check key type == prop
390+
if !prop.AssignableTo(base.Key()) {
391+
return v.error(node.Property, "cannot use %v to get an element from %v", prop, base)
392+
}
391393
t, c := deref(base.Elem())
392394
node.Deref = c
393395
return t, info{}
394396

395397
case reflect.Array, reflect.Slice:
396-
if !isInteger(prop) {
398+
if !isInteger(prop) && !isAny(prop) {
397399
return v.error(node.Property, "array elements can only be selected using an integer (got %v)", prop)
398400
}
399401
t, c := deref(base.Elem())

checker/checker_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ var successTests = []string{
6767
"!ArrayOfAny[0].next.goes['any thing']",
6868
"ArrayOfFoo[0].Bar.Baz == ''",
6969
"ArrayOfFoo[0:10][0].Bar.Baz == ''",
70+
"!ArrayOfAny[Any]",
7071
"Bool && Any",
7172
"FuncParam(true, 1, 'str')",
7273
"FuncParamAny(nil)",
@@ -467,6 +468,11 @@ filter(42, {#})
467468
builtin filter takes only array (got int) (1:8)
468469
| filter(42, {#})
469470
| .......^
471+
472+
MapOfAny[0]
473+
cannot use int to get an element from map[string]interface {} (1:10)
474+
| MapOfAny[0]
475+
| .........^
470476
`
471477

472478
func TestCheck_error(t *testing.T) {

0 commit comments

Comments
 (0)