checker: skip checking `[required]` fields for struct update syntax (#10500)

pull/10503/head
zakuro 2021-06-18 08:51:33 +09:00 committed by GitHub
parent 64f34f6d61
commit 751f2950ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 18 deletions

View File

@ -929,7 +929,7 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
} }
*/ */
// Check for `[required]` struct attr // Check for `[required]` struct attr
if field.attrs.contains('required') && !node.is_short { if field.attrs.contains('required') && !node.is_short && !node.has_update_expr {
mut found := false mut found := false
for init_field in node.fields { for init_field in node.fields {
if field.name == init_field.name { if field.name == init_field.name {

View File

@ -0,0 +1,32 @@
struct Fns {
f1 fn () [required]
f2 fn () [attr1; required]
}
fn func() {
}
fn test_struct_fields_storing_required_functions() {
s := Fns{
f1: func
f2: func
}
assert s.f1 == func
assert s.f2 == func
}
struct Data {
v1 int [required]
v2 int [required]
}
fn test_required_fields() {
data := Data{1, 2}
assert data.v1 == 1
data2 := Data{
...data
v1: 10
}
assert data.v2 == data2.v2
}

View File

@ -1,17 +0,0 @@
struct Struct {
f1 fn () [required]
f2 fn () [attr1; required]
}
fn func() {
}
fn test_struct_fields_storing_required_functions() {
s := Struct{
f1: func
f2: func
}
assert s.f1 == func
assert s.f2 == func
}