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

cgen: fix cast option ptr (#17884)

This commit is contained in:
Felipe Pena 2023-04-05 16:29:18 -03:00 committed by GitHub
parent 902d0dc93d
commit 7334f673a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 1 deletions

View File

@ -56,9 +56,10 @@ fn (mut g Gen) expr_opt_with_cast(expr ast.Expr, expr_typ ast.Type, ret_typ ast.
} else { } else {
stmt_str := g.go_before_stmt(0).trim_space() stmt_str := g.go_before_stmt(0).trim_space()
styp := g.base_type(ret_typ) styp := g.base_type(ret_typ)
decl_styp := g.typ(ret_typ).replace('*', '_ptr')
g.empty_line = true g.empty_line = true
tmp_var := g.new_tmp_var() tmp_var := g.new_tmp_var()
g.writeln('${g.typ(ret_typ)} ${tmp_var};') g.writeln('${decl_styp} ${tmp_var};')
g.write('_option_ok(&(${styp}[]) {') g.write('_option_ok(&(${styp}[]) {')
if expr is ast.CastExpr && expr_typ.has_flag(.option) { if expr is ast.CastExpr && expr_typ.has_flag(.option) {

View File

@ -0,0 +1,34 @@
struct ObjectDesc {
typ u32
ptr voidptr
}
struct ABCD {
name string
}
pub fn cast_object_desc[H](desc &ObjectDesc) ?H {
$if H is &ABCD {
/*
if desc.typ == 12 { // desc == ABCD
return &ABCD(desc.ptr)
}*/
if desc.typ == 12 { // desc == ABCD
return ?&ABCD(&ABCD(desc.ptr))
}
}
return none
}
fn test_option_ptr() {
obj := ABCD{
name: 'Foo'
}
desc := ObjectDesc{
typ: 12
ptr: voidptr(&obj)
}
obj2 := cast_object_desc[&ABCD](desc) or { panic('wwww') }
// obj2 := &ABCD(desc.ptr)
assert obj2.name == 'Foo'
}