Skip to content

Commit

Permalink
checker: fix comptime indexexpr resolving (#23333)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Jan 1, 2025
1 parent c59d640 commit 87c0a9c
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
8 changes: 8 additions & 0 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -2043,6 +2043,14 @@ fn (mut c Checker) resolve_comptime_args(func &ast.Fn, node_ ast.CallExpr, concr
}
} else if call_arg.expr is ast.ComptimeCall {
comptime_args[k] = c.type_resolver.get_type(call_arg.expr)
} else if call_arg.expr is ast.IndexExpr && c.comptime.is_comptime(call_arg.expr) {
mut ctyp := c.type_resolver.get_type(call_arg.expr)
param_typ_sym := c.table.sym(param_typ)
cparam_type_sym := c.table.sym(c.unwrap_generic(ctyp))
if param_typ_sym.kind == .array && cparam_type_sym.info is ast.Array {
ctyp = cparam_type_sym.info.elem_type
}
comptime_args[k] = ctyp
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,14 @@ fn (mut g Gen) resolve_comptime_args(func &ast.Fn, mut node_ ast.CallExpr, concr
comptime_args[k] = cparam_type_sym.info.key_type
comptime_args[k + 1] = cparam_type_sym.info.value_type
}
} else if mut call_arg.expr is ast.IndexExpr && g.comptime.is_comptime(call_arg.expr) {
mut ctyp := g.type_resolver.get_type(call_arg.expr)
param_typ_sym := g.table.sym(param_typ)
cparam_type_sym := g.table.sym(g.unwrap_generic(ctyp))
if param_typ_sym.kind == .array && cparam_type_sym.info is ast.Array {
ctyp = cparam_type_sym.info.elem_type
}
comptime_args[k] = ctyp
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions vlib/x/json2/decoder.v
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ fn decode_array_item[T](mut field T, arr []Any) {
$else $if T is [][]?u64 { field << arr.map(it.as_map().values().map(?u64(it.u64()))) }
$else $if T is [][]bool { field << arr.map(it.as_map().values().map(it.bool())) }
$else $if T is [][]?bool { field << arr.map(it.as_map().values().map(?bool(it.bool()))) }
$else $if T is [][]string { field << arr.map(it.as_map().values().map(it.string())) }
$else $if T is [][]?string { field << arr.map(it.as_map().values().map(?string(it.string()))) }
$else $if T is [][]string { field << arr.map(it.as_map().values().map(it.str())) }
$else $if T is [][]?string { field << arr.map(it.as_map().values().map(?string(it.str()))) }
}
}
// vfmt on
Expand Down
18 changes: 18 additions & 0 deletions vlib/x/json2/json2.v
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ pub fn (f Any) i64() i64 {
}
}

// u8 uses `Any` as a 8-bit unsigned integer.
pub fn (f Any) u8() u8 {
match f {
u8 {
return f
}
u16, u32, i8, i16, i32, int, i64, f32, f64, bool {
return u8(u16(f))
}
string {
return f.u8()
}
else {
return 0
}
}
}

// u64 uses `Any` as a 64-bit unsigned integer.
pub fn (f Any) u64() u64 {
match f {
Expand Down
39 changes: 39 additions & 0 deletions vlib/x/json2/tests/decode_nested_array_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import x.json2

struct Dataset {
values [][]string
}

struct Dataset2 {
values [][]u8
}

fn test_string() {
d := Dataset{
values: [
['a', 'b', 'c'],
['d', 'e', 'f'],
]
}
s := json2.encode(d)
println(s)
d2 := json2.decode[Dataset](s)!
assert d2.str() == "Dataset{
values: [['a', 'b', 'c'], ['d', 'e', 'f']]
}"
}

fn test_u8() {
d := Dataset2{
values: [
[u8(1), 2, 3, 4, 5],
[u8(2), 3, 4, 5, 6],
]
}
s := json2.encode(d)
println(s)
d2 := json2.decode[Dataset2](s)!
assert d2.str() == 'Dataset2{
values: [[1, 2, 3, 4, 5], [2, 3, 4, 5, 6]]
}'
}

0 comments on commit 87c0a9c

Please sign in to comment.