diff --git a/vlib/v/gen/c/str.v b/vlib/v/gen/c/str.v index 9183ff5e03..a1a64e6d5f 100644 --- a/vlib/v/gen/c/str.v +++ b/vlib/v/gen/c/str.v @@ -126,9 +126,13 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) { g.write('}}}))') } } else { + is_ptr := typ.is_ptr() + is_var_mut := expr.is_auto_deref_var() str_fn_name := g.get_str_fn(typ) g.write('${str_fn_name}(') - if expr.is_auto_deref_var() { + if str_method_expects_ptr && !is_ptr { + g.write('&') + } else if (!str_method_expects_ptr && is_ptr && !is_shared) || is_var_mut { g.write('*') } if sym.kind != .function { diff --git a/vlib/v/tests/inout/printing_reference_alias.out b/vlib/v/tests/inout/printing_reference_alias.out new file mode 100644 index 0000000000..331fd4103f --- /dev/null +++ b/vlib/v/tests/inout/printing_reference_alias.out @@ -0,0 +1,6 @@ +Point(Quad{ + x: 1 + y: 2 + z: 3 + w: 1 +}) diff --git a/vlib/v/tests/inout/printing_reference_alias.vv b/vlib/v/tests/inout/printing_reference_alias.vv new file mode 100644 index 0000000000..7adfd79959 --- /dev/null +++ b/vlib/v/tests/inout/printing_reference_alias.vv @@ -0,0 +1,23 @@ +struct Quad { +mut: + x f64 + y f64 + z f64 + w f64 +} + +type Point = Quad +type Vector = Quad + +fn new_point(x f64, y f64, z f64) &Point { + return &Point{x, y, z, 1} +} + +fn new_vector(x f64, y f64, z f64) &Vector { + return &Vector{x, y, z, 0} +} + +fn main() { + n := new_point(1, 2, 3) + println(n) +}