fix: fix infering int and float literal in generic fn calls (#9287)

pull/9296/head
Enzo 2021-03-14 14:00:47 +01:00 committed by GitHub
parent c0779e8455
commit db152868c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 2 deletions

View File

@ -443,7 +443,7 @@ pub fn (mut c Checker) infer_fn_types(f table.Fn, mut call_expr ast.CallExpr) {
arg := call_expr.args[arg_i]
param_type_sym := c.table.get_type_symbol(param.typ)
if param.typ.has_flag(.generic) && param_type_sym.name == gt_name {
typ = arg.typ
typ = c.table.mktyp(arg.typ)
break
}
arg_sym := c.table.get_type_symbol(arg.typ)

View File

@ -10,7 +10,7 @@ fn test_array() {
mut a := [7, 8]
r := f_array(a)
assert r == 7
g_array(mut a)
assert a[0] == 8
}

View File

@ -19,6 +19,15 @@ fn test_explicit_calls_should_also_work() {
assert true
}
fn get_type_name<T>(x T) string {
return T.name
}
fn test_literal() {
assert get_type_name(1) == 'int'
assert get_type_name(1.0) == 'f64'
}
//
fn choose4<T>(a T, b T, c T, d T) T {
// NB: a similar construct is used in prime31's via engine