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:
parent
902d0dc93d
commit
7334f673a0
@ -56,9 +56,10 @@ fn (mut g Gen) expr_opt_with_cast(expr ast.Expr, expr_typ ast.Type, ret_typ ast.
|
||||
} else {
|
||||
stmt_str := g.go_before_stmt(0).trim_space()
|
||||
styp := g.base_type(ret_typ)
|
||||
decl_styp := g.typ(ret_typ).replace('*', '_ptr')
|
||||
g.empty_line = true
|
||||
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}[]) {')
|
||||
|
||||
if expr is ast.CastExpr && expr_typ.has_flag(.option) {
|
||||
|
34
vlib/v/tests/option_ptr_cast_test.v
Normal file
34
vlib/v/tests/option_ptr_cast_test.v
Normal 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'
|
||||
}
|
Loading…
Reference in New Issue
Block a user