From db5e0f21171c05fff3492754eb7aa09b8783c3f1 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 25 Jul 2021 23:23:16 +0300 Subject: [PATCH] v.gen.c: fix cgen regression after f457b94 (prevented vinix builds), add tests --- vlib/v/gen/c/fn.v | 12 ++++++++-- vlib/v/tests/generics_indirect_test.v | 32 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/generics_indirect_test.v 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)) +}