checker: improve error message for invalid property (#9263)

pull/9266/head^2
Nick Treleaven 2021-03-12 12:18:52 +00:00 committed by GitHub
parent 3be78d6777
commit e229d0c1c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 1 deletions

View File

@ -2335,7 +2335,7 @@ pub fn (mut c Checker) selector_expr(mut selector_expr ast.SelectorExpr) table.T
}
if sym.kind !in [.struct_, .aggregate, .interface_, .sum_type] {
if sym.kind != .placeholder {
c.error('`$sym.name` is not a struct', selector_expr.pos)
c.error('`$sym.name` has no property `$selector_expr.field_name`', selector_expr.pos)
}
} else {
if sym.info is table.Struct {

View File

@ -0,0 +1,18 @@
vlib/v/checker/tests/invalid_property.vv:2:7: error: `string` has no property `length`
1 | s :=''
2 | _ = s.length
| ~~~~~~
3 | _ = [1,2].foo
4 |
vlib/v/checker/tests/invalid_property.vv:3:11: error: `[]int` has no property `foo`
1 | s :=''
2 | _ = s.length
3 | _ = [1,2].foo
| ~~~
4 |
5 | mut fa := [3,4]!
vlib/v/checker/tests/invalid_property.vv:6:8: error: `[2]int` has no property `bar`
4 |
5 | mut fa := [3,4]!
6 | _ = fa.bar
| ~~~

View File

@ -0,0 +1,6 @@
s :=''
_ = s.length
_ = [1,2].foo
mut fa := [3,4]!
_ = fa.bar