Skip to content

Commit

Permalink
move check to checker
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Jul 16, 2024
1 parent 5e2bb55 commit 7007313
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
9 changes: 9 additions & 0 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,15 @@ fn (mut c Checker) alias_type_decl(node ast.AliasTypeDecl) {
node.type_pos)
}
}
if parent_typ_sym.info.is_anon {
// Disallow cases like `type Alias = struct { Int Int }` where Int is `type Int = int`
for field in parent_typ_sym.info.fields {
if field.name != '' && field.anon_struct_decl.fields.len == 0
&& field.anon_struct_decl.name == '' {
c.error('invalid anonymous struct', node.type_pos)
}
}
}
}
}
.array {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ vlib/v/checker/tests/type_alias_decl_anon_struct_invalid_embed.vv:2:16: error: c
| ~~~~~~~~
3 | Int
4 | }
vlib/v/checker/tests/type_alias_decl_anon_struct_invalid_embed.vv:2:16: error: invalid anonymous struct
1 | type Int = int
2 | type MyIndex = struct {
| ~~~~~~~~
3 | Int
4 | }
4 changes: 0 additions & 4 deletions vlib/v/parser/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,6 @@ 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')
return ast.StructDecl{}
}
typ = p.parse_type()
type_pos = start_type_pos.extend(p.prev_tok.pos())
}
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/parser/tests/struct_anon_invalid_embed_err.out
Original file line number Diff line number Diff line change
@@ -1,6 +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
vlib/v/parser/tests/struct_anon_invalid_embed_err.vv:2:16: error: invalid anonymous struct
1 | type Int = int
2 | type MyIndex = struct {
| ~~~~~~~~
3 | Int Int
| ~~~
4 | }

0 comments on commit 7007313

Please sign in to comment.