checker: check struct field name duplicate

pull/4644/head
yuyi 2020-04-29 15:11:36 +08:00 committed by GitHub
parent 0dc7a57e1f
commit a9e33e712a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 1 deletions

View File

@ -203,7 +203,12 @@ pub fn (mut c Checker) struct_decl(decl ast.StructDecl) {
}
c.error('struct name must begin with capital letter', pos)
}
for field in decl.fields {
for i, field in decl.fields {
for j in 0..i {
if field.name == decl.fields[j].name {
c.error('field name `$field.name` duplicate', field.pos)
}
}
sym := c.table.get_type_symbol(field.typ)
if sym.kind == .placeholder && !decl.is_c && !sym.name.starts_with('C.') {
c.error('unknown type `$sym.name`', field.pos)

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/struct_field_name_duplicate_err.v:3:4: error: field name `a` duplicate
1| struct A {
2| a int
3| a string
~~~~~~
4| }
5| fn main(){}

View File

@ -0,0 +1,5 @@
struct A {
a int
a string
}
fn main(){}

View File

@ -0,0 +1,5 @@
struct A {
a int
a string
}
fn main(){}