assoc: verify the field exists and verify the type

pull/3067/head
Alexander Medvednikov 2019-12-12 20:32:55 +03:00
parent 6982f4a5a2
commit bd833deef3
1 changed files with 8 additions and 3 deletions

View File

@ -2262,13 +2262,19 @@ fn (p mut Parser) assoc() string {
} }
p.check(.pipe) p.check(.pipe)
p.gen('($var.typ){') p.gen('($var.typ){')
typ := p.table.find_type(var.typ)
mut fields := []string// track the fields user is setting, the rest will be copied from the old object mut fields := []string// track the fields user is setting, the rest will be copied from the old object
for p.tok != .rcbr { for p.tok != .rcbr {
field := p.check_name() field := p.check_name()
//if !typ.has_field(field) {
f := typ.find_field(field) or {
p.error('`$typ.name` has no field `$field`')
exit(1)
}
fields << field fields << field
p.gen('.$field = ') p.gen('.$field = ')
p.check(.colon) p.check(.colon)
p.bool_expression() p.check_types(p.bool_expression(), f.typ)
p.gen(',') p.gen(',')
if p.tok != .rcbr { if p.tok != .rcbr {
p.check(.comma) p.check(.comma)
@ -2276,8 +2282,7 @@ fn (p mut Parser) assoc() string {
p.fgen_nl() p.fgen_nl()
} }
// Copy the rest of the fields // Copy the rest of the fields
T := p.table.find_type(var.typ) for ffield in typ.fields {
for ffield in T.fields {
f := ffield.name f := ffield.name
if f in fields { if f in fields {
continue continue