From db152868c01939e460b7fc073b2ba82a10c0caed Mon Sep 17 00:00:00 2001 From: Enzo Date: Sun, 14 Mar 2021 14:00:47 +0100 Subject: [PATCH] fix: fix infering int and float literal in generic fn calls (#9287) --- vlib/v/checker/check_types.v | 2 +- vlib/v/tests/generic_fn_infer_modifier_test.v | 2 +- vlib/v/tests/generic_fn_infer_test.v | 9 +++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index 49671c377e..1a9ac09391 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -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) diff --git a/vlib/v/tests/generic_fn_infer_modifier_test.v b/vlib/v/tests/generic_fn_infer_modifier_test.v index 7e813b5b0f..f0eb9422ae 100644 --- a/vlib/v/tests/generic_fn_infer_modifier_test.v +++ b/vlib/v/tests/generic_fn_infer_modifier_test.v @@ -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 } diff --git a/vlib/v/tests/generic_fn_infer_test.v b/vlib/v/tests/generic_fn_infer_test.v index f3da4affb3..6ae8ae5aea 100644 --- a/vlib/v/tests/generic_fn_infer_test.v +++ b/vlib/v/tests/generic_fn_infer_test.v @@ -19,6 +19,15 @@ fn test_explicit_calls_should_also_work() { assert true } +fn get_type_name(x T) string { + return T.name +} + +fn test_literal() { + assert get_type_name(1) == 'int' + assert get_type_name(1.0) == 'f64' +} + // fn choose4(a T, b T, c T, d T) T { // NB: a similar construct is used in prime31's via engine