checker: check generics fn that return generic struct (#9979)
parent
035fd052d1
commit
ddc003380c
|
@ -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',
|
c.error('only functions that do NOT return values can have `[if $ct_name]` tags',
|
||||||
node.pos)
|
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 {
|
if node.is_method {
|
||||||
mut sym := c.table.get_type_symbol(node.receiver.typ)
|
mut sym := c.table.get_type_symbol(node.receiver.typ)
|
||||||
|
|
|
@ -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{}
|
|
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue