checker: improve error message when using fields as methods (#10367)
parent
a71e382b95
commit
3b6045865b
|
@ -1730,7 +1730,11 @@ pub fn (mut c Checker) method_call(mut call_expr ast.CallExpr) ast.Type {
|
||||||
call_expr.receiver_type = left_type
|
call_expr.receiver_type = left_type
|
||||||
left_type_sym := c.table.get_type_symbol(c.unwrap_generic(left_type))
|
left_type_sym := c.table.get_type_symbol(c.unwrap_generic(left_type))
|
||||||
method_name := call_expr.name
|
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) {
|
if left_type.has_flag(.optional) {
|
||||||
c.error('optional type cannot be called directly', call_expr.left.position())
|
c.error('optional type cannot be called directly', call_expr.left.position())
|
||||||
return ast.void_type
|
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
|
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 has_field := false
|
||||||
mut field := ast.StructField{}
|
mut field := ast.StructField{}
|
||||||
if field_name.len > 0 && field_name[0].is_capital() && sym.info is ast.Struct
|
if field_name.len > 0 && field_name[0].is_capital() && sym.info is ast.Struct
|
||||||
|
|
|
@ -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() {
|
1 | fn main() {
|
||||||
2 | _ := 'test'.filter(it == `t`)
|
2 | _ := 'test'.filter(it == `t`)
|
||||||
| ~~~~~~~~~~~~~~~~~
|
| ~~~~~~~~~~~~~~~~~
|
||||||
|
|
|
@ -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')
|
16 | a := new_animal('persian')
|
||||||
17 | if a is Cat {
|
17 | if a is Cat {
|
||||||
18 | a.foo()
|
18 | a.foo()
|
||||||
| ~~~~~
|
| ~~~~~
|
||||||
19 | }
|
19 | }
|
||||||
20 | }
|
20 | }
|
||||||
|
|
|
@ -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() {
|
5 | fn main() {
|
||||||
6 | t := Test{}
|
6 | t := Test{}
|
||||||
7 | println(t.sdd)
|
7 | println(t.sdd)
|
||||||
|
|
|
@ -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() {
|
5 | fn main() {
|
||||||
6 | t := Test{}
|
6 | t := Test{}
|
||||||
7 | println(t.sdd())
|
7 | println(t.sdd())
|
||||||
|
|
|
@ -6,7 +6,7 @@ Did you mean `crc32.Crc32`?
|
||||||
| ~~~~~
|
| ~~~~~
|
||||||
14 | }
|
14 | }
|
||||||
15 |
|
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`?
|
Did you mean `translate`?
|
||||||
25 | p := Point{1, 2, 3}
|
25 | p := Point{1, 2, 3}
|
||||||
26 | v := Vector{x: 5, y: 5, z: 10}
|
26 | v := Vector{x: 5, y: 5, z: 10}
|
||||||
|
|
|
@ -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`?
|
Did you mean `xxxx`?
|
||||||
9 | p := Points{[1], [2], [3]}
|
9 | p := Points{[1], [2], [3]}
|
||||||
10 | println('p: $p')
|
10 | println('p: $p')
|
||||||
|
|
Loading…
Reference in New Issue