checker/table: generic fixes

pull/4983/head
Alexander Medvednikov 2020-05-21 18:15:04 +02:00
parent 7e55261c17
commit 87d8e70d6d
3 changed files with 12 additions and 7 deletions

View File

@ -888,13 +888,6 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type {
c.error('unknown function: $fn_name', call_expr.pos)
return table.void_type
}
if call_expr.generic_type != table.void_type && f.return_type != 0 { // table.t_type {
// Handle `foo<T>() T` => `foo<int>() int` => return int
sym := c.table.get_type_symbol(f.return_type)
if sym.name == 'T' {
return call_expr.generic_type
}
}
if !found_in_args && call_expr.mod in ['builtin', 'main'] {
scope := c.file.scope.innermost(call_expr.pos.pos)
if _ := scope.find_var(fn_name) {
@ -988,6 +981,13 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type {
call_expr.pos)
}
}
if call_expr.generic_type != table.void_type && f.return_type != 0 { // table.t_type {
// Handle `foo<T>() T` => `foo<int>() int` => return int
sym := c.table.get_type_symbol(f.return_type)
if sym.name == 'T' {
return call_expr.generic_type
}
}
return f.return_type
}

View File

@ -572,6 +572,9 @@ pub fn (table &Table) qualify_module(mod, file_path string) string {
pub fn (table &Table) register_fn_gen_type(fn_name string, typ Type) {
mut a := table.fn_gen_types[fn_name]
if typ in a {
return
}
a << typ
table.fn_gen_types[fn_name] = a
}

View File

@ -7,7 +7,9 @@ fn simple<T>(p T) T {
fn test_generic_fn() {
assert simple<int>(1) == 1
assert simple<int>(1 + 0) == 1
assert simple<string>('g') == 'g'
assert simple<string>('g') + 'h' == 'gh'
}
/*