checker: check unknown generic type (#7954)

pull/7962/head
yuyi 2021-01-08 14:04:06 +08:00 committed by GitHub
parent 0998cbaaba
commit 828120a918
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 0 deletions

View File

@ -1658,6 +1658,12 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type {
// builtin C.m*, C.s* only - temp // builtin C.m*, C.s* only - temp
c.warn('function `$f.name` must be called from an `unsafe` block', call_expr.pos) c.warn('function `$f.name` must be called from an `unsafe` block', call_expr.pos)
} }
if f.is_generic {
sym := c.table.get_type_symbol(call_expr.generic_type)
if sym.kind == .placeholder {
c.error('unknown type `$sym.name`', call_expr.generic_list_pos)
}
}
if f.is_generic && f.return_type.has_flag(.generic) { if f.is_generic && f.return_type.has_flag(.generic) {
rts := c.table.get_type_symbol(f.return_type) rts := c.table.get_type_symbol(f.return_type)
if rts.kind == .struct_ { if rts.kind == .struct_ {

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/unknown_generic_type.vv:5:13: error: unknown type `Foo`
3 |
4 | fn main() {
5 | x := decode<Foo>('{"name": "test"}')?
| ~~~~~
6 | println(x)
7 | }

View File

@ -0,0 +1,7 @@
fn decode<T>(raw_data string) ?T {
}
fn main() {
x := decode<Foo>('{"name": "test"}')?
println(x)
}