Skip to content

Commit abf3527

Browse files
authored
checker: fix json decoder with generic struct (#14700)
1 parent ce26d5b commit abf3527

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import json
2+
3+
struct Result<T> {
4+
ok bool
5+
result T
6+
}
7+
8+
struct User {
9+
id int
10+
username string
11+
}
12+
13+
fn func<T>() ?T {
14+
text := '{"ok": true, "result":{"id":37467243, "username": "ciao"}}'
15+
a := json.decode(Result<T>, text)?
16+
return a.result
17+
}
18+
19+
fn test_decode_with_generic_struct() ? {
20+
ret := func<User>()?
21+
println(ret)
22+
assert ret.id == 37467243
23+
assert ret.username == 'ciao'
24+
}

vlib/v/checker/fn.v

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,12 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
506506
}
507507
expr := node.args[0].expr
508508
if expr is ast.TypeNode {
509-
sym := c.table.sym(c.unwrap_generic(expr.typ))
509+
mut unwrapped_typ := c.unwrap_generic(expr.typ)
510+
if c.table.sym(expr.typ).kind == .struct_ && expr.typ.has_flag(.generic) {
511+
unwrapped_typ = c.table.unwrap_generic_type(expr.typ, c.table.cur_fn.generic_names,
512+
c.table.cur_concrete_types)
513+
}
514+
sym := c.table.sym(unwrapped_typ)
510515
if c.table.known_type(sym.name) && sym.kind != .placeholder {
511516
mut kind := sym.kind
512517
if sym.info is ast.Alias {

0 commit comments

Comments
 (0)