checker: remove redundant code of find field with embed (#12600)

pull/12611/head
yuyi 2021-11-29 03:31:19 +08:00 committed by GitHub
parent 87029e5707
commit 6d6a23a1c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 38 additions and 83 deletions

View File

@ -878,8 +878,6 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
mut inited_fields := []string{}
for i, mut field in node.fields {
mut field_info := ast.StructField{}
mut embed_type := ast.Type(0)
mut is_embed := false
mut field_name := ''
if node.is_short {
if i >= info.fields.len {
@ -893,27 +891,10 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
} else {
field_name = field.name
mut exists := true
field_info = info.find_field(field_name) or {
field_info = c.table.find_field_with_embeds(type_sym, field_name) or {
exists = false
ast.StructField{}
}
if !exists {
for embed in info.embeds {
embed_sym := c.table.get_type_symbol(embed)
if embed_sym.embed_name() == field_name {
exists = true
embed_type = embed
is_embed = true
break
}
embed_struct_info := embed_sym.info as ast.Struct
if embed_field_info := embed_struct_info.find_field(field_name) {
exists = true
field_info = embed_field_info
break
}
}
}
if !exists {
c.error('unknown field `$field.name` in struct literal of type `$type_sym.name`',
field.pos)
@ -927,20 +908,6 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
}
mut expr_type := ast.Type(0)
mut expected_type := ast.Type(0)
if is_embed {
expected_type = embed_type
c.expected_type = expected_type
expr_type = c.expr(field.expr)
expr_type_sym := c.table.get_type_symbol(expr_type)
if expr_type != ast.void_type && expr_type_sym.kind != .placeholder {
c.check_expected(expr_type, embed_type) or {
c.error('cannot assign to field `$field_info.name`: $err.msg',
field.pos)
}
}
node.fields[i].typ = expr_type
node.fields[i].expected_type = embed_type
} else {
inited_fields << field_name
field_type_sym := c.table.get_type_symbol(field_info.typ)
expected_type = field_info.typ
@ -977,7 +944,7 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
}
node.fields[i].typ = expr_type
node.fields[i].expected_type = field_info.typ
}
if field_info.typ.has_flag(.optional) {
c.error('field `$field_info.name` is optional, but initialization of optional fields currently unsupported',
field.pos)
@ -1661,23 +1628,11 @@ fn (mut c Checker) fail_if_immutable(expr ast.Expr) (string, token.Position) {
mut typ_sym := c.table.get_final_type_symbol(c.unwrap_generic(expr.expr_type))
match typ_sym.kind {
.struct_ {
struct_info := typ_sym.info as ast.Struct
mut has_field := true
mut field_info := struct_info.find_field(expr.field_name) or {
mut field_info := c.table.find_field_with_embeds(typ_sym, expr.field_name) or {
has_field = false
ast.StructField{}
}
if !has_field {
for embed in struct_info.embeds {
embed_sym := c.table.get_type_symbol(embed)
embed_struct_info := embed_sym.info as ast.Struct
if embed_field_info := embed_struct_info.find_field(expr.field_name) {
has_field = true
field_info = embed_field_info
break
}
}
}
if !has_field {
type_str := c.table.type_to_str(expr.expr_type)
c.error('unknown field `${type_str}.$expr.field_name`', expr.pos)