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

reject invalid map key types at compile time

R=ken
OCL=25720
CL=25720
This commit is contained in:
Russ Cox 2009-03-04 17:38:37 -08:00
parent 0793c88371
commit 98b34e5bbd
3 changed files with 24 additions and 12 deletions

View File

@ -657,6 +657,7 @@ int isslice(Type*);
int isinter(Type*);
int isnilinter(Type*);
int isddd(Type*);
Type* maptype(Type*, Type*);
Type* dclmethod(Type*);
Type* methtype(Type*);
int methconv(Type*);

View File

@ -1019,9 +1019,7 @@ convtype:
| LMAP '[' type ']' type
{
// map literal
$$ = typ(TMAP);
$$->down = $3;
$$->type = $5;
$$ = maptype($3, $5);
}
| structtype
| '(' type ')'
@ -1126,9 +1124,7 @@ Aothertype:
}
| LMAP '[' type ']' Atype
{
$$ = typ(TMAP);
$$->down = $3;
$$->type = $5;
$$ = maptype($3, $5);
}
| '*' Atype
{
@ -1160,9 +1156,7 @@ Bothertype:
}
| LMAP '[' type ']' Btype
{
$$ = typ(TMAP);
$$->down = $3;
$$->type = $5;
$$ = maptype($3, $5);
}
| '*' Btype
{
@ -1806,9 +1800,7 @@ hidden_type1:
}
| LMAP '[' hidden_type ']' hidden_type
{
$$ = typ(TMAP);
$$->down = $3;
$$->type = $5;
$$ = maptype($3, $5);
}
| LSTRUCT '{' ohidden_structdcl_list '}'
{

View File

@ -305,6 +305,25 @@ algtype(Type *t)
return a;
}
Type*
maptype(Type *key, Type *val)
{
Type *t;
if(key != nil && key->etype != TANY && algtype(key) == ANOEQ)
yyerror("invalid map key type %T", key);
t = typ(TMAP);
t->down = key;
t->type = val;
return t;
}
int
iskeytype(Type *t)
{
return algtype(t) != ANOEQ;
}
Node*
list(Node *a, Node *b)
{