parser: fix generic anon fn decl with type only argument (#14124)

yuyi 2022-04-21 17:53:13 +08:00 committed by Jef Roosens
parent 0a12fd7212
commit 052c8e0282
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 15 additions and 1 deletions

View File

@ -783,9 +783,10 @@ fn (mut p Parser) fn_args() ([]ast.Param, bool, bool) {
} else { } else {
p.tok.lit p.tok.lit
} }
is_generic_type := p.tok.kind == .name && p.tok.lit.len == 1 && p.tok.lit[0].is_capital()
types_only := p.tok.kind in [.amp, .ellipsis, .key_fn, .lsbr] types_only := p.tok.kind in [.amp, .ellipsis, .key_fn, .lsbr]
|| (p.peek_tok.kind == .comma && p.table.known_type(argname)) || (p.peek_tok.kind == .comma && (p.table.known_type(argname) || is_generic_type))
|| p.peek_tok.kind == .dot || p.peek_tok.kind == .rpar || p.peek_tok.kind == .dot || p.peek_tok.kind == .rpar
|| (p.tok.kind == .key_mut && (p.peek_token(2).kind == .comma || (p.tok.kind == .key_mut && (p.peek_token(2).kind == .comma
|| p.peek_token(2).kind == .rpar || (p.peek_tok.kind == .name || p.peek_token(2).kind == .rpar || (p.peek_tok.kind == .name

View File

@ -0,0 +1,13 @@
fn test_generics_anon_fn_decl_with_type_only_arg() {
ret := func_b<int>(11, 22, add)
println(ret)
assert ret == 33
}
fn add(a int, b int) int {
return a + b
}
fn func_b<T>(x T, y T, f fn (T, T) T) T {
return f(x, y)
}