From ea322bdd97b4247217be4711b2544e2603605af6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Fri, 17 Jul 2020 18:28:45 +0200 Subject: [PATCH] checker, cgen: fix call of generic function returning normal type (#5865) --- vlib/v/checker/checker.v | 2 +- vlib/v/gen/cgen.v | 7 ++++--- vlib/v/tests/sizeof_2_test.v | 8 ++++++++ 3 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 vlib/v/tests/sizeof_2_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 5046ac9f5a..2aa05a7fa7 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1116,7 +1116,7 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type { if f.is_deprecated { c.warn('function `$f.name` has been deprecated', call_expr.pos) } - if f.is_generic { + if f.is_generic && f.return_type.has_flag(.generic) { rts := c.table.get_type_symbol(f.return_type) if rts.kind == .struct_ { rts_info := rts.info as table.Struct diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index e2b9082f76..d58cefd0d1 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1785,14 +1785,15 @@ fn (mut g Gen) expr(node ast.Expr) { } ast.SizeOf { if node.is_type { + node_typ := g.unwrap_generic(node.typ) mut styp := node.type_name if styp.starts_with('C.') { styp = styp[2..] } - if node.type_name == '' { - styp = g.typ(node.typ) + if node.type_name == '' || node.typ.has_flag(.generic) { + styp = g.typ(node_typ) } else { - sym := g.table.get_type_symbol(node.typ) + sym := g.table.get_type_symbol(node_typ) if sym.kind == .struct_ { info := sym.info as table.Struct if !info.is_typedef { diff --git a/vlib/v/tests/sizeof_2_test.v b/vlib/v/tests/sizeof_2_test.v new file mode 100644 index 0000000000..1e656b15f4 --- /dev/null +++ b/vlib/v/tests/sizeof_2_test.v @@ -0,0 +1,8 @@ +fn getsize

() u32 { + return sizeof(P) +} + +fn test_sizeof_2() { + assert getsize() == 8 + assert 4 == getsize() +}