diff --git a/vlib/v/checker/struct.v b/vlib/v/checker/struct.v index d1cd0a213c..6b7447336e 100644 --- a/vlib/v/checker/struct.v +++ b/vlib/v/checker/struct.v @@ -3,6 +3,7 @@ module checker import v.ast +import v.util pub fn (mut c Checker) struct_decl(mut node ast.StructDecl) { mut struct_sym, struct_typ_idx := c.table.find_sym_and_type_idx(node.name) @@ -391,7 +392,8 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type { ast.StructField{} } if !exists { - c.error('unknown field `$field.name` in struct literal of type `$type_sym.name`', + existing_fields := c.table.struct_fields(type_sym).map(it.name) + c.error(util.new_suggestion(field.name, existing_fields).say('unknown field `$field.name` in struct literal of type `$type_sym.name`'), field.pos) continue } diff --git a/vlib/v/checker/tests/struct_unknown_field.out b/vlib/v/checker/tests/struct_unknown_field.out index 2c544b0c60..c710487b08 100644 --- a/vlib/v/checker/tests/struct_unknown_field.out +++ b/vlib/v/checker/tests/struct_unknown_field.out @@ -1,4 +1,5 @@ -vlib/v/checker/tests/struct_unknown_field.vv:8:3: error: unknown field `bar` in struct literal of type `Test` +vlib/v/checker/tests/struct_unknown_field.vv:8:3: error: unknown field `bar` in struct literal of type `Test`. +1 possibility: `foo`. 6 | t := Test{ 7 | foo: true 8 | bar: false diff --git a/vlib/v/checker/tests/unknown_field_in_struct_literal.out b/vlib/v/checker/tests/unknown_field_in_struct_literal.out new file mode 100644 index 0000000000..eab76783bd --- /dev/null +++ b/vlib/v/checker/tests/unknown_field_in_struct_literal.out @@ -0,0 +1,8 @@ +vlib/v/checker/tests/unknown_field_in_struct_literal.vv:10:2: error: unknown field `pos` in struct literal of type `TileLine`. +Did you mean `ypos`? + 8 | + 9 | hline := TileLine{ + 10 | pos: 123 + | ~~~~~~~~ + 11 | } + 12 | println(hline) diff --git a/vlib/v/checker/tests/unknown_field_in_struct_literal.vv b/vlib/v/checker/tests/unknown_field_in_struct_literal.vv new file mode 100644 index 0000000000..81890027c7 --- /dev/null +++ b/vlib/v/checker/tests/unknown_field_in_struct_literal.vv @@ -0,0 +1,12 @@ +struct TileLine { + ypos int +mut: + field [5]int + points int + shifts int +} + +hline := TileLine{ + pos: 123 +} +println(hline)