checker: check generic method receivers with no type parameter (#10374)

pull/10378/head weekly.2021.23
yuyi 2021-06-07 17:11:29 +08:00 committed by GitHub
parent 86778d06b1
commit 0615f2e236
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 0 deletions

View File

@ -7252,6 +7252,15 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
c.error('missing return at end of function `$node.name`', node.pos)
}
}
if node.is_method {
sym := c.table.get_type_symbol(node.receiver.typ)
if sym.kind == .struct_ {
info := sym.info as ast.Struct
if info.is_generic && c.table.cur_fn.generic_names.len == 0 {
c.error('receiver must specify the generic type names, e.g. Foo<T>', node.method_type_pos)
}
}
}
c.returns = false
node.source_file = c.file
}

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/generics_method_receiver_type_err.vv:6:11: error: receiver must specify the generic type names, e.g. Foo<T>
4 | }
5 |
6 | pub fn (x Node) str() string {
| ~~~~
7 | return 'Value is : ${u16(x.val)}\nName is : $x.name'
8 | }

View File

@ -0,0 +1,16 @@
struct Node<T> {
val T
name string
}
pub fn (x Node) str() string {
return 'Value is : ${u16(x.val)}\nName is : $x.name'
}
fn main() {
xx := Node<u16>{
val: u16(11)
name: 'man'
}
println(xx.str())
}