From 11b16a63cfed83281e102b494249755a970fde74 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 1 Oct 2020 15:36:47 +0300 Subject: [PATCH] checker: add a suggestion for misspelled struct field name errors --- vlib/v/checker/checker.v | 5 +++++ .../tests/unknown_struct_field_suggest_name.out | 8 ++++++++ .../tests/unknown_struct_field_suggest_name.vv | 14 ++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 vlib/v/checker/tests/unknown_struct_field_suggest_name.out create mode 100644 vlib/v/checker/tests/unknown_struct_field_suggest_name.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 78da238aee..83f6a1485c 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 diff --git a/vlib/v/checker/tests/unknown_struct_field_suggest_name.out b/vlib/v/checker/tests/unknown_struct_field_suggest_name.out new file mode 100644 index 0000000000..cc4d87077e --- /dev/null +++ b/vlib/v/checker/tests/unknown_struct_field_suggest_name.out @@ -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 | } diff --git a/vlib/v/checker/tests/unknown_struct_field_suggest_name.vv b/vlib/v/checker/tests/unknown_struct_field_suggest_name.vv new file mode 100644 index 0000000000..aebdcea0df --- /dev/null +++ b/vlib/v/checker/tests/unknown_struct_field_suggest_name.vv @@ -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') + } +}