checker: add a suggestion for misspelled struct field name errors

pull/6522/head
Delyan Angelov 2020-10-01 15:36:47 +03:00
parent f534edc371
commit 11b16a63cf
3 changed files with 27 additions and 0 deletions

View File

@ -1673,6 +1673,11 @@ pub fn (mut c Checker) selector_expr(mut selector_expr ast.SelectorExpr) table.T
c.error('`$sym.source_name` is not a struct', selector_expr.pos)
}
} else {
if sym.kind == .struct_ {
sss := sym.info as table.Struct
suggestion := util.new_suggestion(field_name, sss.fields.map(it.name))
c.error(suggestion.say(unknown_field_msg), selector_expr.pos)
}
c.error(unknown_field_msg, selector_expr.pos)
}
return table.void_type

View File

@ -0,0 +1,8 @@
vlib/v/checker/tests/unknown_struct_field_suggest_name.vv:11:16: error: type `Points` has no field or method `xxxa`.
Did you mean `xxxx`?
9 | p := Points{[1], [2], [3]}
10 | println('p: $p')
11 | for x in p.xxxa {
| ~~~~
12 | println('x: $x')
13 | }

View File

@ -0,0 +1,14 @@
struct Points {
xxxx []int
yyyy []int
zzzz []int
}
fn main() {
p := Points{[1], [2], [3]}
println('p: $p')
for x in p.xxxa {
println('x: $x')
}
}