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

checker: check generics fn that return generic struct (#9979)

This commit is contained in:
yuyi 2021-05-04 00:48:54 +08:00 committed by GitHub
parent 035fd052d1
commit ddc003380c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 0 deletions

View File

@ -6808,6 +6808,15 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
c.error('only functions that do NOT return values can have `[if $ct_name]` tags',
node.pos)
}
if node.generic_names.len > 0 {
gs := c.table.get_type_symbol(node.return_type)
if gs.info is ast.Struct {
if gs.info.is_generic && !node.return_type.has_flag(.generic) {
c.error('return generic struct in fn declaration must specify the generic type names, e.g. Foo<T>',
node.return_type_pos)
}
}
}
}
if node.is_method {
mut sym := c.table.get_type_symbol(node.receiver.typ)

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/generics_fn_return_generic_struct_err.vv:13:32: error: return generic struct in fn declaration must specify the generic type names, e.g. Foo<T>
11 | }
12 |
13 | pub fn new_channel_struct<T>() GenericChannelStruct {
| ~~~~~~~~~~~~~~~~~~~~
14 | d := GenericChannelStruct{
15 | ch: chan T{}

View File

@ -0,0 +1,18 @@
struct GenericChannelStruct<T> {
ch chan T
}
struct Simple {
msg string
}
fn main() {
new_channel_struct<Simple>()
}
pub fn new_channel_struct<T>() GenericChannelStruct {
d := GenericChannelStruct{
ch: chan T{}
}
return d
}