checker: check anon struct field valid case (partial fix of #24284) (#24286)

This commit is contained in:
Swastik Baranwal 2025-04-21 21:29:35 +05:30 committed by GitHub
parent 5ac1e14e7f
commit 157d8e84e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 0 deletions

View File

@ -602,13 +602,18 @@ fn (mut c Checker) alias_type_decl(mut node ast.AliasTypeDecl) {
}
if parent_typ_sym.info.is_anon {
for field in parent_typ_sym.info.fields {
mut is_embed := false
field_sym := c.table.sym(field.typ)
if field_sym.info is ast.Alias {
if c.table.sym(field_sym.info.parent_type).kind != .struct {
c.error('cannot embed non-struct `${field_sym.name}`',
field.type_pos)
is_embed = true
}
}
if !is_embed {
c.check_valid_snake_case(field.name, 'field name', field.pos)
}
}
}
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/type_alias_struct_invalid_field_naming.vv:2:2: error: field name `field_One` cannot contain uppercase letters, use snake_case instead
1 | type Struct = struct {
2 | field_One int
| ~~~~~~~~~~~~~
3 | field_two int
4 | }

View File

@ -0,0 +1,4 @@
type Struct = struct {
field_One int
field_two int
}