checker: implement `[required]` struct field attribute (#6276)

pull/6286/head
spaceface777 2020-08-31 23:15:40 +02:00 committed by GitHub
parent bd304f1141
commit 35cbca96e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 2 deletions

View File

@ -180,7 +180,7 @@ fn (t TileLine) to_left() TileLine {
fn (b Board) to_left() Board {
mut res := b
for y := 0; y < 4; y++ {
mut hline := TileLine{y}
mut hline := TileLine{ypos: y}
for x := 0; x < 4; x++ {
hline.field[x] = b.field[y][x]
}

View File

@ -36,7 +36,7 @@ mut:
fn (mut a App) init() {
a.frame = 0
a.last = time.ticks()
a.ps = particle.System{a.width, a.height}
a.ps = particle.System{width: a.width, height: a.height}
a.ps.init(particle.SystemConfig{
pool: 20000
})

View File

@ -480,6 +480,20 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type {
c.warn('reference field `${type_sym.source_name}.$field.name` must be initialized',
struct_init.pos)
}
// Check for `[required]` struct attr
if field.attrs.contains('required') && !struct_init.is_short {
mut found := false
for init_field in struct_init.fields {
if field.name == init_field.name {
found = true
break
}
}
if !found {
c.error('field `${type_sym.source_name}.$field.name` is required',
struct_init.pos)
}
}
}
}
else {}

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/struct_required_field.vv:12:6: error: field `Abc.f3` is required
10 | f3: 789
11 | }
12 | _ = Abc{
| ~~~~
13 | f1: 123
14 | f2: 789

View File

@ -0,0 +1,16 @@
struct Abc {
f1 int [required]
f2 int
f3 int [required]
}
fn main() {
_ = Abc{
f1: 123
f3: 789
}
_ = Abc{
f1: 123
f2: 789
}
}