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

checker: improve error message when using fields as methods (#10367)

This commit is contained in:
pancake 2021-06-12 14:15:25 +02:00 committed by GitHub
parent a71e382b95
commit 3b6045865b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 13 additions and 9 deletions

View File

@ -1730,7 +1730,11 @@ pub fn (mut c Checker) method_call(mut call_expr ast.CallExpr) ast.Type {
call_expr.receiver_type = left_type
left_type_sym := c.table.get_type_symbol(c.unwrap_generic(left_type))
method_name := call_expr.name
mut unknown_method_msg := 'unknown method: `${left_type_sym.name}.$method_name`'
mut unknown_method_msg := if field := c.table.find_field(left_type_sym, method_name) {
'unknown method `$field.name` did you mean to access the field with the same name instead?'
} else {
'unknown method or field: `${left_type_sym.name}.$method_name`'
}
if left_type.has_flag(.optional) {
c.error('optional type cannot be called directly', call_expr.left.position())
return ast.void_type
@ -2896,7 +2900,7 @@ pub fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type {
return ast.u32_type
}
}
mut unknown_field_msg := 'type `$sym.name` has no field or method `$field_name`'
mut unknown_field_msg := 'type `$sym.name` has no field named `$field_name`'
mut has_field := false
mut field := ast.StructField{}
if field_name.len > 0 && field_name[0].is_capital() && sym.info is ast.Struct

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/filter_on_non_arr_err.vv:2:14: error: unknown method: `string.filter`
vlib/v/checker/tests/filter_on_non_arr_err.vv:2:14: error: unknown method or field: `string.filter`
1 | fn main() {
2 | _ := 'test'.filter(it == `t`)
| ~~~~~~~~~~~~~~~~~

View File

@ -1,7 +1,7 @@
vlib/v/checker/tests/no_method_on_interface_propagation.vv:18:5: error: unknown method: `Cat.foo`
vlib/v/checker/tests/no_method_on_interface_propagation.vv:18:5: error: unknown method or field: `Cat.foo`
16 | a := new_animal('persian')
17 | if a is Cat {
18 | a.foo()
| ~~~~~
19 | }
20 | }
20 | }

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/unknown_field.vv:7:12: error: type `Test` has no field or method `sdd`
vlib/v/checker/tests/unknown_field.vv:7:12: error: type `Test` has no field named `sdd`
5 | fn main() {
6 | t := Test{}
7 | println(t.sdd)

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/unknown_method.vv:7:12: error: unknown method: `Test.sdd`
vlib/v/checker/tests/unknown_method.vv:7:12: error: unknown method or field: `Test.sdd`
5 | fn main() {
6 | t := Test{}
7 | println(t.sdd())

View File

@ -6,7 +6,7 @@ Did you mean `crc32.Crc32`?
| ~~~~~
14 | }
15 |
vlib/v/checker/tests/unknown_method_suggest_name.vv:27:9: error: unknown method: `Point.tranzlate`.
vlib/v/checker/tests/unknown_method_suggest_name.vv:27:9: error: unknown method or field: `Point.tranzlate`.
Did you mean `translate`?
25 | p := Point{1, 2, 3}
26 | v := Vector{x: 5, y: 5, z: 10}

View File

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