checker: fix map of function as argument and direct call of function in map (#8494)
							parent
							
								
									969f19daf4
								
							
						
					
					
						commit
						a0a33f7ff1
					
				|  | @ -85,8 +85,8 @@ pub fn (mut c Checker) check_basic(got table.Type, expected table.Type) bool { | |||
| 		// fn == 0
 | ||||
| 		return true | ||||
| 	} | ||||
| 	// array fn
 | ||||
| 	if got_type_sym.kind == .array && exp_type_sym.kind == .array { | ||||
| 	// array/map fn
 | ||||
| 	if got_type_sym.kind in [.array, .map] && exp_type_sym.kind == got_type_sym.kind { | ||||
| 		if c.table.type_to_str(got) == c.table.type_to_str(expected).trim('&') { | ||||
| 			return true | ||||
| 		} | ||||
|  |  | |||
|  | @ -1707,6 +1707,12 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type { | |||
| 			if elem_typ.info is table.FnType { | ||||
| 				return elem_typ.info.func.return_type | ||||
| 			} | ||||
| 		} else if sym.kind == .map { | ||||
| 			info := sym.info as table.Map | ||||
| 			value_typ := c.table.get_type_symbol(info.value_type) | ||||
| 			if value_typ.info is table.FnType { | ||||
| 				return value_typ.info.func.return_type | ||||
| 			} | ||||
| 		} else if sym.kind == .array_fixed { | ||||
| 			info := sym.info as table.ArrayFixed | ||||
| 			elem_typ := c.table.get_type_symbol(info.elem_type) | ||||
|  |  | |||
|  | @ -72,8 +72,7 @@ fn test_map_and_array_with_fns_typeof_and_direct_call() { | |||
| 	assert a[0]('hello') == 15 | ||||
| 	b := {'one': foo3} | ||||
| 	assert typeof(b).name == 'map[string]fn (string) int' | ||||
| 	// TODO: enable this
 | ||||
| 	// assert b['one']('hi') == 12
 | ||||
| 	assert b['one']('hi') == 12 | ||||
| } | ||||
| 
 | ||||
| fn bar1(mut a []fn (string) int) int { | ||||
|  | @ -91,3 +90,19 @@ fn test_array_of_fns_as_argument() { | |||
| 	a2 := [foo3] | ||||
| 	assert bar2(a2) == 15 | ||||
| } | ||||
| 
 | ||||
| fn bar3(m map[string]fn (string) int) int { | ||||
| 	return m['fn']('hi') | ||||
| } | ||||
| 
 | ||||
| fn bar4(mut m map[string]fn (string) int) int { | ||||
| 	m['fn'] = foo4  | ||||
| 	return m['fn']('hi') | ||||
| } | ||||
| 
 | ||||
| fn test_map_of_fns_as_argument() { | ||||
| 	m1 := {'fn': foo3} | ||||
| 	assert bar3(m1) == 12 | ||||
| 	mut m2 := {'fn': foo3} | ||||
| 	assert bar4(mut m2) == 22 | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue