1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: fix generic function call in for in expression

This commit is contained in:
Johan Hillerström 2019-12-16 23:08:30 +01:00 committed by Alexander Medvednikov
parent 3dbf7a4039
commit 6008fa44c6
3 changed files with 28 additions and 1 deletions

View File

@ -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')
}

View File

@ -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

View File

@ -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<T>(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'
}
}