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

checker: check number of fields in short struct inits (#6280)

This commit is contained in:
spaceface777 2020-08-31 22:17:59 +02:00 committed by GitHub
parent 60a9d49382
commit bd304f1141
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 40 additions and 27 deletions

View File

@ -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 {

View File

@ -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 | }

View File

@ -1,8 +0,0 @@
struct Test {
foo bool
}
fn main() {
t := Test{true, false}
_ = t
}

View File

@ -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 | }

View File

@ -0,0 +1,9 @@
struct Test {
foo bool
bar bool
}
fn main() {
_ = Test{true}
_ = Test{true, false, true}
}

View File

@ -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')

View File

@ -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')

View File

@ -32,7 +32,7 @@ pub fn dial_udp(laddr, raddr string) ?UdpConn {
}
return UdpConn {
sock
sock: sock
}
}