1
0
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:
Felipe Pena 2023-04-18 12:51:43 -03:00 committed by GitHub
parent 377c2e25ff
commit adcd16b198
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -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('*')
}

View 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
}