mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix generic return for option ptr (#17987)
This commit is contained in:
parent
377c2e25ff
commit
adcd16b198
@ -4755,7 +4755,7 @@ fn (mut g Gen) return_stmt(node ast.Return) {
|
||||
styp := g.base_type(fn_ret_type)
|
||||
g.writeln('${ret_typ} ${tmpvar};')
|
||||
g.write('_option_ok(&(${styp}[]) { ')
|
||||
if !fn_ret_type.is_ptr() && node.types[0].is_ptr() {
|
||||
if !g.unwrap_generic(fn_ret_type).is_ptr() && node.types[0].is_ptr() {
|
||||
if !(node.exprs[0] is ast.Ident && !g.is_amp) {
|
||||
g.write('*')
|
||||
}
|
||||
|
22
vlib/v/tests/option_generic_ptr_return_test.v
Normal file
22
vlib/v/tests/option_generic_ptr_return_test.v
Normal file
@ -0,0 +1,22 @@
|
||||
struct ObjectDesc {
|
||||
typ u32
|
||||
ptr voidptr
|
||||
}
|
||||
|
||||
struct ABCD {}
|
||||
|
||||
pub fn cast_object_desc[H](desc &ObjectDesc) ?H {
|
||||
$if H is &ABCD {
|
||||
if desc.typ == 12 { // desc == ABCD
|
||||
return &ABCD(desc.ptr)
|
||||
}
|
||||
}
|
||||
return none
|
||||
}
|
||||
|
||||
fn test_main() {
|
||||
obj := ABCD{}
|
||||
a := cast_object_desc[&ABCD](ObjectDesc{ typ: 12, ptr: voidptr(&obj) })
|
||||
dump(a)
|
||||
assert *a? == obj
|
||||
}
|
Loading…
Reference in New Issue
Block a user