1
0
mirror of https://github.com/golang/go synced 2024-11-25 01:57:56 -07:00

check type of string/map/array index expressions

R=ken
OCL=34478
CL=34480
This commit is contained in:
Russ Cox 2009-09-09 01:31:10 -07:00
parent 83bdb805a2
commit f4ee9f133c
2 changed files with 24 additions and 0 deletions

View File

@ -500,18 +500,24 @@ reswitch:
case TARRAY:
defaultlit(&n->right, types[TUINT]);
if(n->right->type != T && !isint[n->right->type->etype])
yyerror("non-integer array index %#N", n->right);
n->type = t->type;
break;
case TMAP:
n->etype = 0;
defaultlit(&n->right, t->down);
if(n->right->type != T && !eqtype(n->right->type, t->down))
yyerror("invalid map index %#N - need type %T", n->right, t->down);
n->type = t->type;
n->op = OINDEXMAP;
break;
case TSTRING:
defaultlit(&n->right, types[TUINT]);
if(n->right->type != T && !isint[n->right->type->etype])
yyerror("non-integer string index %#N", n->right);
n->type = types[TUINT8];
n->op = OINDEXSTR;
break;

18
test/fixedbugs/bug205.go Normal file
View File

@ -0,0 +1,18 @@
// errchk $G $D/$F.go
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
var t []int
var s string;
var m map[string]int;
func main() {
println(t["hi"]); // ERROR "non-integer"
println(s["hi"]); // ERROR "non-integer"
println(m[0]); // ERROR "map index"
}