checker: skip checking `[required]` fields for struct update syntax (#10500)
							parent
							
								
									64f34f6d61
								
							
						
					
					
						commit
						751f2950ea
					
				|  | @ -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 { | ||||||
|  |  | ||||||
|  | @ -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 | ||||||
|  | } | ||||||
|  | @ -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 |  | ||||||
| } |  | ||||||
		Loading…
	
		Reference in New Issue