checker: fix panic in generic methods (#7944)

pull/7954/head
Daniel Däschle 2021-01-08 00:50:59 +01:00 committed by GitHub
parent 2820139216
commit 1ce93536d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 0 deletions

View File

@ -426,6 +426,9 @@ pub fn (mut c Checker) infer_fn_types(f table.Fn, mut call_expr ast.CallExpr) {
gt_name := 'T'
mut typ := table.void_type
for i, param in f.params {
if call_expr.args.len == 0 {
break
}
arg := if i != 0 && call_expr.is_method { call_expr.args[i - 1] } else { call_expr.args[i] }
if param.typ.has_flag(.generic) {
typ = arg.typ

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/method_generic_infer_err.vv:9:7: error: could not infer generic type `T` in call to `func`
7 | fn main() {
8 | data := Data{}
9 | data.func()
| ~~~~~~
10 | }

View File

@ -0,0 +1,10 @@
struct Data {}
fn (_ Data) func<T>() T {
return T{}
}
fn main() {
data := Data{}
data.func()
}