From bd304f114174862145751b207a57d95b3326bcb1 Mon Sep 17 00:00:00 2001 From: spaceface777 Date: Mon, 31 Aug 2020 22:17:59 +0200 Subject: [PATCH] checker: check number of fields in short struct inits (#6280) --- vlib/v/checker/checker.v | 10 ++++++++-- vlib/v/checker/tests/short_struct_too_many.out | 7 ------- vlib/v/checker/tests/short_struct_too_many.vv | 8 -------- vlib/v/checker/tests/short_struct_wrong_number.out | 13 +++++++++++++ vlib/v/checker/tests/short_struct_wrong_number.vv | 9 +++++++++ .../checker/tests/unknown_method_suggest_name.out | 14 +++++++------- .../v/checker/tests/unknown_method_suggest_name.vv | 4 ++-- vlib/x/net/udp.v | 2 +- 8 files changed, 40 insertions(+), 27 deletions(-) delete mode 100644 vlib/v/checker/tests/short_struct_too_many.out delete mode 100644 vlib/v/checker/tests/short_struct_too_many.vv create mode 100644 vlib/v/checker/tests/short_struct_wrong_number.out create mode 100644 vlib/v/checker/tests/short_struct_wrong_number.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 53b9c96111..281dee302e 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -411,8 +411,14 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type { } else { info = type_sym.info as table.Struct } - if struct_init.is_short && struct_init.fields.len > info.fields.len { - c.error('too many fields', struct_init.pos) + if struct_init.is_short { + exp_len := info.fields.len + got_len := struct_init.fields.len + if exp_len != got_len { + amount := if exp_len < got_len { 'many' } else { 'few' } + c.error('too $amount fields in `$type_sym.source_name` literal (expecting $exp_len, got $got_len)', + struct_init.pos) + } } mut inited_fields := []string{} for i, field in struct_init.fields { diff --git a/vlib/v/checker/tests/short_struct_too_many.out b/vlib/v/checker/tests/short_struct_too_many.out deleted file mode 100644 index 275e67a1c8..0000000000 --- a/vlib/v/checker/tests/short_struct_too_many.out +++ /dev/null @@ -1,7 +0,0 @@ -vlib/v/checker/tests/short_struct_too_many.vv:6:7: error: too many fields - 4 | - 5 | fn main() { - 6 | t := Test{true, false} - | ~~~~~~~~~~~~~~~~~ - 7 | _ = t - 8 | } diff --git a/vlib/v/checker/tests/short_struct_too_many.vv b/vlib/v/checker/tests/short_struct_too_many.vv deleted file mode 100644 index d33acc614d..0000000000 --- a/vlib/v/checker/tests/short_struct_too_many.vv +++ /dev/null @@ -1,8 +0,0 @@ -struct Test { - foo bool -} - -fn main() { - t := Test{true, false} - _ = t -} diff --git a/vlib/v/checker/tests/short_struct_wrong_number.out b/vlib/v/checker/tests/short_struct_wrong_number.out new file mode 100644 index 0000000000..fa6044b6a0 --- /dev/null +++ b/vlib/v/checker/tests/short_struct_wrong_number.out @@ -0,0 +1,13 @@ +vlib/v/checker/tests/short_struct_wrong_number.vv:7:6: error: too few fields in `Test` literal (expecting 2, got 1) + 5 | + 6 | fn main() { + 7 | _ = Test{true} + | ~~~~~~~~~~ + 8 | _ = Test{true, false, true} + 9 | } +vlib/v/checker/tests/short_struct_wrong_number.vv:8:6: error: too many fields in `Test` literal (expecting 2, got 3) + 6 | fn main() { + 7 | _ = Test{true} + 8 | _ = Test{true, false, true} + | ~~~~~~~~~~~~~~~~~~~~~~~ + 9 | } diff --git a/vlib/v/checker/tests/short_struct_wrong_number.vv b/vlib/v/checker/tests/short_struct_wrong_number.vv new file mode 100644 index 0000000000..393ca5e00f --- /dev/null +++ b/vlib/v/checker/tests/short_struct_wrong_number.vv @@ -0,0 +1,9 @@ +struct Test { + foo bool + bar bool +} + +fn main() { + _ = Test{true} + _ = Test{true, false, true} +} diff --git a/vlib/v/checker/tests/unknown_method_suggest_name.out b/vlib/v/checker/tests/unknown_method_suggest_name.out index 39d47fe12d..d5791c484c 100644 --- a/vlib/v/checker/tests/unknown_method_suggest_name.out +++ b/vlib/v/checker/tests/unknown_method_suggest_name.out @@ -1,15 +1,15 @@ -vlib/v/checker/tests/unknown_method_suggest_name.vv:7:2: error: unknown type `hash.crc32.Crc33`. +vlib/v/checker/tests/unknown_method_suggest_name.vv:13:2: error: unknown type `hash.crc32.Crc33`. Did you mean `crc32.Crc32`? - 5 | y int - 6 | z int - 7 | ccc crc32.Crc33 + 11 | y int + 12 | z int + 13 | ccc crc32.Crc33 | ~~~~~~~~~~~~~~~ - 8 | } - 9 | + 14 | } + 15 | vlib/v/checker/tests/unknown_method_suggest_name.vv:27:9: error: unknown method: `Point.tranzlate`. Did you mean `translate`? 25 | p := Point{1, 2, 3} - 26 | v := Vector{5, 5, 10} + 26 | v := Vector{x: 5, y: 5, z: 10} 27 | z := p.tranzlate(v) | ~~~~~~~~~~~~ 28 | println('p: $p') diff --git a/vlib/v/checker/tests/unknown_method_suggest_name.vv b/vlib/v/checker/tests/unknown_method_suggest_name.vv index 44d1f849d2..52af44c774 100644 --- a/vlib/v/checker/tests/unknown_method_suggest_name.vv +++ b/vlib/v/checker/tests/unknown_method_suggest_name.vv @@ -4,13 +4,13 @@ struct Point { x int y int z int - ccc crc32.Crc33 } struct Vector { x int y int z int + ccc crc32.Crc33 } fn (p Point) translate(v Vector) Point { @@ -23,7 +23,7 @@ fn (p Point) identity() Point { fn main() { p := Point{1, 2, 3} - v := Vector{5, 5, 10} + v := Vector{x: 5, y: 5, z: 10} z := p.tranzlate(v) println('p: $p') println('v: $v') diff --git a/vlib/x/net/udp.v b/vlib/x/net/udp.v index 7eff651d59..c36e16613f 100644 --- a/vlib/x/net/udp.v +++ b/vlib/x/net/udp.v @@ -32,7 +32,7 @@ pub fn dial_udp(laddr, raddr string) ?UdpConn { } return UdpConn { - sock + sock: sock } }