mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix option ptr printing (#17651)
This commit is contained in:
parent
7e8723d603
commit
5eb331ed89
@ -56,7 +56,11 @@ fn (mut g Gen) get_str_fn(typ ast.Type) string {
|
|||||||
$if trace_autostr ? {
|
$if trace_autostr ? {
|
||||||
eprintln('> get_str_fn: ${typ.debug()}')
|
eprintln('> get_str_fn: ${typ.debug()}')
|
||||||
}
|
}
|
||||||
mut unwrapped := g.unwrap_generic(typ).set_nr_muls(0).clear_flag(.variadic)
|
mut unwrapped := if typ.has_flag(.option) {
|
||||||
|
g.unwrap_generic(typ).clear_flag(.variadic)
|
||||||
|
} else {
|
||||||
|
g.unwrap_generic(typ).set_nr_muls(0).clear_flag(.variadic)
|
||||||
|
}
|
||||||
if g.pref.nofloat {
|
if g.pref.nofloat {
|
||||||
if typ == ast.f32_type {
|
if typ == ast.f32_type {
|
||||||
unwrapped = ast.u32_type
|
unwrapped = ast.u32_type
|
||||||
@ -515,6 +519,9 @@ fn styp_to_str_fn_name(styp string) string {
|
|||||||
|
|
||||||
// deref_kind returns deref, deref_label
|
// deref_kind returns deref, deref_label
|
||||||
fn deref_kind(str_method_expects_ptr bool, is_elem_ptr bool, typ ast.Type) (string, string) {
|
fn deref_kind(str_method_expects_ptr bool, is_elem_ptr bool, typ ast.Type) (string, string) {
|
||||||
|
if typ.has_flag(.option) {
|
||||||
|
return '', ''
|
||||||
|
}
|
||||||
if str_method_expects_ptr != is_elem_ptr {
|
if str_method_expects_ptr != is_elem_ptr {
|
||||||
if is_elem_ptr {
|
if is_elem_ptr {
|
||||||
return '*'.repeat(typ.nr_muls()), '&'.repeat(typ.nr_muls())
|
return '*'.repeat(typ.nr_muls()), '&'.repeat(typ.nr_muls())
|
||||||
@ -936,7 +943,11 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, typ_str string,
|
|||||||
caller_should_free = false
|
caller_should_free = false
|
||||||
} else if ftyp_noshared.is_ptr() {
|
} else if ftyp_noshared.is_ptr() {
|
||||||
// reference types can be "nil"
|
// reference types can be "nil"
|
||||||
funcprefix += 'isnil(it.${c_name(field.name)})'
|
if ftyp_noshared.has_flag(.option) {
|
||||||
|
funcprefix += 'isnil(&it.${c_name(field.name)})'
|
||||||
|
} else {
|
||||||
|
funcprefix += 'isnil(it.${c_name(field.name)})'
|
||||||
|
}
|
||||||
funcprefix += ' ? _SLIT("nil") : '
|
funcprefix += ' ? _SLIT("nil") : '
|
||||||
// struct, floats and ints have a special case through the _str function
|
// struct, floats and ints have a special case through the _str function
|
||||||
if sym.kind !in [.struct_, .alias, .enum_, .sum_type, .map, .interface_]
|
if sym.kind !in [.struct_, .alias, .enum_, .sum_type, .map, .interface_]
|
||||||
|
@ -125,7 +125,7 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
|
|||||||
if str_method_expects_ptr && !is_ptr {
|
if str_method_expects_ptr && !is_ptr {
|
||||||
g.write('&')
|
g.write('&')
|
||||||
} else if is_ptr && typ.has_flag(.option) {
|
} else if is_ptr && typ.has_flag(.option) {
|
||||||
g.write('*(${g.typ(typ.set_nr_muls(0))}*)&')
|
g.write('*(${g.typ(typ)}*)&')
|
||||||
} else if !str_method_expects_ptr && !is_shared && (is_ptr || is_var_mut) {
|
} else if !str_method_expects_ptr && !is_shared && (is_ptr || is_var_mut) {
|
||||||
g.write('*'.repeat(typ.nr_muls()))
|
g.write('*'.repeat(typ.nr_muls()))
|
||||||
}
|
}
|
||||||
|
14
vlib/v/tests/option_print_ptr_test.v
Normal file
14
vlib/v/tests/option_print_ptr_test.v
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
struct One {
|
||||||
|
thing string = 'thing 1'
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Two {
|
||||||
|
maybe_one ?&One
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_dump_option_ptr() {
|
||||||
|
a := Two{}
|
||||||
|
println(a)
|
||||||
|
println(Two{})
|
||||||
|
dump(Two{})
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user