checker: fix error for array of sumtype appending literal value (#14200)
							parent
							
								
									bf75a873b5
								
							
						
					
					
						commit
						47c18c4570
					
				|  | @ -935,13 +935,13 @@ pub fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { | ||||||
| 					return ast.void_type | 					return ast.void_type | ||||||
| 				} else if left_value_sym.kind == .sum_type { | 				} else if left_value_sym.kind == .sum_type { | ||||||
| 					if right_final.kind != .array { | 					if right_final.kind != .array { | ||||||
| 						if !c.table.is_sumtype_or_in_variant(left_value_type, right_type) { | 						if !c.table.is_sumtype_or_in_variant(left_value_type, ast.mktyp(right_type)) { | ||||||
| 							c.error('cannot append `$right_sym.name` to `$left_sym.name`', | 							c.error('cannot append `$right_sym.name` to `$left_sym.name`', | ||||||
| 								right_pos) | 								right_pos) | ||||||
| 						} | 						} | ||||||
| 					} else { | 					} else { | ||||||
| 						right_value_type := c.table.value_type(right_type) | 						right_value_type := c.table.value_type(right_type) | ||||||
| 						if !c.table.is_sumtype_or_in_variant(left_value_type, right_value_type) { | 						if !c.table.is_sumtype_or_in_variant(left_value_type, ast.mktyp(right_value_type)) { | ||||||
| 							c.error('cannot append `$right_sym.name` to `$left_sym.name`', | 							c.error('cannot append `$right_sym.name` to `$left_sym.name`', | ||||||
| 								right_pos) | 								right_pos) | ||||||
| 						} | 						} | ||||||
|  |  | ||||||
|  | @ -0,0 +1,20 @@ | ||||||
|  | type Typ_var = f64 | int | ||||||
|  | 
 | ||||||
|  | fn test_array_of_sumtype_append_literal_type() { | ||||||
|  | 	mut arr := []Typ_var{} | ||||||
|  | 
 | ||||||
|  | 	// literal int/float type error
 | ||||||
|  | 	arr << 123 | ||||||
|  | 	arr << 1.23 | ||||||
|  | 
 | ||||||
|  | 	// cast/wrap in type
 | ||||||
|  | 	arr << int(123) | ||||||
|  | 	arr << f64(1.23) | ||||||
|  | 	arr << Typ_var(456) | ||||||
|  | 	arr << Typ_var(4.56) | ||||||
|  | 
 | ||||||
|  | 	println(arr) | ||||||
|  | 
 | ||||||
|  | 	assert arr[0] == Typ_var(123) | ||||||
|  | 	assert arr[1] == Typ_var(1.23) | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue