From e229d0c1c317f4bcdb74871a39731734d629b649 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Fri, 12 Mar 2021 12:18:52 +0000 Subject: [PATCH] checker: improve error message for invalid property (#9263) --- vlib/v/checker/checker.v | 2 +- vlib/v/checker/tests/invalid_property.out | 18 ++++++++++++++++++ vlib/v/checker/tests/invalid_property.vv | 6 ++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 vlib/v/checker/tests/invalid_property.out create mode 100644 vlib/v/checker/tests/invalid_property.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 7bf98ce7c9..4c98854b0f 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 { diff --git a/vlib/v/checker/tests/invalid_property.out b/vlib/v/checker/tests/invalid_property.out new file mode 100644 index 0000000000..e7839a42b9 --- /dev/null +++ b/vlib/v/checker/tests/invalid_property.out @@ -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 + | ~~~ diff --git a/vlib/v/checker/tests/invalid_property.vv b/vlib/v/checker/tests/invalid_property.vv new file mode 100644 index 0000000000..427c2e7fb7 --- /dev/null +++ b/vlib/v/checker/tests/invalid_property.vv @@ -0,0 +1,6 @@ +s :='' +_ = s.length +_ = [1,2].foo + +mut fa := [3,4]! +_ = fa.bar