Skip to content

Commit 87534e6

Browse files
committed
Improve type checker
1 parent f383e06 commit 87534e6

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

checker/checker.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,15 @@ func (v *visitor) BinaryNode(node *ast.BinaryNode) (reflect.Type, info) {
321321
return boolType, info{}
322322
}
323323
if isMap(r) {
324+
if l == nil {
325+
return v.error(node, "cannot use nil as map key")
326+
}
327+
if !isAny(l) && !l.AssignableTo(r.Key()) {
328+
return v.error(node, "cannot use %v (type %v) as type %v in map key", l, l, r.Key())
329+
}
324330
return boolType, info{}
325331
}
326-
if isArray(r) {
332+
if isInteger(l) && isArray(r) {
327333
return boolType, info{}
328334
}
329335
if isAny(l) && anyOf(r, isString, isArray, isMap) {

checker/checker_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,11 @@ FuncTyped(42)
489489
cannot use int as argument (type string) to call FuncTyped (1:11)
490490
| FuncTyped(42)
491491
| ..........^
492+
493+
.0 in MapOfFoo
494+
cannot use float64 (type float64) as type string in map key (1:4)
495+
| .0 in MapOfFoo
496+
| ...^
492497
`
493498

494499
func TestCheck_error(t *testing.T) {

0 commit comments

Comments
 (0)