From 6008fa44c6cc20693f705c33a7a787567330098d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Hillerstr=C3=B6m?= Date: Mon, 16 Dec 2019 23:08:30 +0100 Subject: [PATCH] parser: fix generic function call in `for in` expression --- vlib/compiler/fn.v | 7 ++++++- vlib/compiler/parser.v | 4 ++++ vlib/compiler/tests/generic_test.v | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/vlib/compiler/fn.v b/vlib/compiler/fn.v index 1ae6b719c1..7c2bae4dff 100644 --- a/vlib/compiler/fn.v +++ b/vlib/compiler/fn.v @@ -844,7 +844,12 @@ fn (p mut Parser) fn_call(f mut Fn, method_ph int, receiver_var, receiver_type s generic := f.is_generic p.fn_call_args(mut f) if generic { - p.cgen.resetln(p.cgen.cur_line.replace('$cgen_name (', '$f.name (')) + line := if p.cgen.is_tmp { + p.cgen.tmp_line + } else { + p.cgen.cur_line + } + p.cgen.resetln(line.replace('$cgen_name (', '$f.name (')) // println('calling inst $f.name: $p.cgen.cur_line') } diff --git a/vlib/compiler/parser.v b/vlib/compiler/parser.v index c25a4fc9ea..ec5932996f 100644 --- a/vlib/compiler/parser.v +++ b/vlib/compiler/parser.v @@ -99,6 +99,7 @@ struct ParserState { cgen_lines []string cgen_cur_line string cgen_tmp_line string + cgen_is_tmp bool tokens []Token token_idx int tok TokenKind @@ -293,6 +294,7 @@ pub fn (p &Parser) save_state() ParserState { cgen_lines : p.cgen.lines cgen_cur_line : p.cgen.cur_line cgen_tmp_line : p.cgen.tmp_line + cgen_is_tmp : p.cgen.is_tmp tokens : p.tokens token_idx : p.token_idx tok : p.tok @@ -314,6 +316,7 @@ pub fn (p mut Parser) restore_state(state ParserState, scanner bool, cgen bool) p.cgen.lines = state.cgen_lines p.cgen.cur_line = state.cgen_cur_line p.cgen.tmp_line = state.cgen_tmp_line + p.cgen.is_tmp = state.cgen_is_tmp } p.tokens = state.tokens p.token_idx = state.token_idx @@ -335,6 +338,7 @@ fn (p mut Parser) clear_state(scanner bool, cgen bool) { p.cgen.lines = [] p.cgen.cur_line = '' p.cgen.tmp_line = '' + p.cgen.is_tmp = false } p.tokens = [] p.token_idx = 0 diff --git a/vlib/compiler/tests/generic_test.v b/vlib/compiler/tests/generic_test.v index 2b077d79a1..4d642b8ab0 100644 --- a/vlib/compiler/tests/generic_test.v +++ b/vlib/compiler/tests/generic_test.v @@ -80,3 +80,21 @@ fn test_generic_method() { p.translate(2, 1.0) assert p.x == 2.0 && p.y == 1.0 } + +fn get_values(i T) []T { + return [i] +} + +fn test_generic_fn_in_for_in_expression() { + for value in get_values(1) { + assert value == 1 + } + + for i, val in get_values(0) { + assert i == val + } + + for value in get_values('a') { + assert value == 'a' + } +}