diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index e279e280ed..7126c38bce 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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) diff --git a/vlib/v/checker/tests/struct_field_name_duplicate_err.out b/vlib/v/checker/tests/struct_field_name_duplicate_err.out new file mode 100644 index 0000000000..7827126183 --- /dev/null +++ b/vlib/v/checker/tests/struct_field_name_duplicate_err.out @@ -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(){} diff --git a/vlib/v/checker/tests/struct_field_name_duplicate_err.v b/vlib/v/checker/tests/struct_field_name_duplicate_err.v new file mode 100644 index 0000000000..e59e586300 --- /dev/null +++ b/vlib/v/checker/tests/struct_field_name_duplicate_err.v @@ -0,0 +1,5 @@ +struct A { + a int + a string +} +fn main(){} diff --git a/vlib/v/checker/tests/struct_field_name_duplicate_err.vv b/vlib/v/checker/tests/struct_field_name_duplicate_err.vv new file mode 100644 index 0000000000..e59e586300 --- /dev/null +++ b/vlib/v/checker/tests/struct_field_name_duplicate_err.vv @@ -0,0 +1,5 @@ +struct A { + a int + a string +} +fn main(){}