diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 781742e600..b09855e7bb 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -1310,8 +1310,16 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as if arg.expr.is_lvalue() { g.write('(voidptr)&/*qq*/') } else { - needs_closing = true - g.write('ADDR(${g.typ(expected_deref_type)}/*qq*/, ') + mut atype := expected_deref_type + if atype.has_flag(.generic) { + atype = g.unwrap_generic(atype) + } + if atype.has_flag(.generic) { + g.write('(voidptr)&/*qq*/') + } else { + needs_closing = true + g.write('ADDR(${g.typ(atype)}/*qq*/, ') + } } } } diff --git a/vlib/v/tests/generics_indirect_test.v b/vlib/v/tests/generics_indirect_test.v new file mode 100644 index 0000000000..c33e44be5d --- /dev/null +++ b/vlib/v/tests/generics_indirect_test.v @@ -0,0 +1,32 @@ +const zzz = &Local{} + +struct Local { + aborted bool +} + +pub fn current() &Local { + return zzz +} + +pub fn store(var &T, value T) { + eprintln('store ${voidptr(var)} <- $value') + unsafe { + *var = value + } +} + +fn test_generic_over_a_local_boolean_address() { + eprintln('-'.repeat(40)) + mut mybool := false + println(mybool) + store(mybool, true) + println(mybool) + eprintln('-'.repeat(40)) +} + +fn test_generic_over_a_const_returned_by_a_fn() { + println(current().aborted) + store(current().aborted, true) + println(current().aborted) + eprintln('-'.repeat(40)) +}