From abf35270cf0114fbb5436a0c1e3b7c4521f28789 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 7 Jun 2022 00:25:02 +0800 Subject: [PATCH] checker: fix json decoder with generic struct (#14700) --- vlib/json/json_decode_with_generic_test.v | 24 +++++++++++++++++++++++ vlib/v/checker/fn.v | 7 ++++++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 vlib/json/json_decode_with_generic_test.v diff --git a/vlib/json/json_decode_with_generic_test.v b/vlib/json/json_decode_with_generic_test.v new file mode 100644 index 0000000000..0ec358cca3 --- /dev/null +++ b/vlib/json/json_decode_with_generic_test.v @@ -0,0 +1,24 @@ +import json + +struct Result { + ok bool + result T +} + +struct User { + id int + username string +} + +fn func() ?T { + text := '{"ok": true, "result":{"id":37467243, "username": "ciao"}}' + a := json.decode(Result, text)? + return a.result +} + +fn test_decode_with_generic_struct() ? { + ret := func()? + println(ret) + assert ret.id == 37467243 + assert ret.username == 'ciao' +} diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 7f6577be54..746fa7b013 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -506,7 +506,12 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) } expr := node.args[0].expr if expr is ast.TypeNode { - sym := c.table.sym(c.unwrap_generic(expr.typ)) + mut unwrapped_typ := c.unwrap_generic(expr.typ) + if c.table.sym(expr.typ).kind == .struct_ && expr.typ.has_flag(.generic) { + unwrapped_typ = c.table.unwrap_generic_type(expr.typ, c.table.cur_fn.generic_names, + c.table.cur_concrete_types) + } + sym := c.table.sym(unwrapped_typ) if c.table.known_type(sym.name) && sym.kind != .placeholder { mut kind := sym.kind if sym.info is ast.Alias {