Skip to content

Commit

Permalink
parser, checker: improve checks for embed in anon struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Jul 15, 2024
1 parent cffdc14 commit 18aa285
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,13 @@ fn (mut c Checker) alias_type_decl(node ast.AliasTypeDecl) {
c.error('unknown type `${ct_sym.name}`', node.type_pos)
}
}
for embed_type in parent_typ_sym.info.embeds {
final_embed_sym := c.table.final_sym(embed_type)
if final_embed_sym.kind != .struct_ {
c.error('cannot embed non-struct `${c.table.sym(embed_type).name}`',
node.type_pos)
}
}
}
}
.array {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
vlib/v/checker/tests/type_alias_decl_anon_struct_invalid_embed.vv:2:16: error: cannot embed non-struct `Int`
1 | type Int = int
2 | type MyIndex = struct {
| ~~~~~~~~
3 | Int
4 | }
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Int = int
type MyIndex = struct {
Int
}
3 changes: 3 additions & 0 deletions vlib/v/parser/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
typ = p.table.find_type_idx(p.anon_struct_decl.name)
} else {
start_type_pos := p.tok.pos()
if field_name == p.tok.lit && p.tok.lit[0].is_capital() {
p.error('field-name `${p.tok.lit}` cannot be same as the type-name')
}
typ = p.parse_type()
type_pos = start_type_pos.extend(p.prev_tok.pos())
}
Expand Down
6 changes: 6 additions & 0 deletions vlib/v/parser/tests/struct_anon_invalid_embed_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
vlib/v/parser/tests/struct_anon_invalid_embed_err.vv:3:6: error: field-name `Int` cannot be same as the type-name
1 | type Int = int
2 | type MyIndex = struct {
3 | Int Int
| ~~~
4 | }
4 changes: 4 additions & 0 deletions vlib/v/parser/tests/struct_anon_invalid_embed_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Int = int
type MyIndex = struct {
Int Int
}

0 comments on commit 18aa285

Please sign in to comment.