1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: add a suggestion for misspelled field names in struct literals

This commit is contained in:
Delyan Angelov 2022-08-10 10:37:03 +03:00
parent d3aa7700c7
commit c752e5eb3e
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
4 changed files with 25 additions and 2 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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)

View File

@ -0,0 +1,12 @@
struct TileLine {
ypos int
mut:
field [5]int
points int
shifts int
}
hline := TileLine{
pos: 123
}
println(hline)