Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Jan 2, 2025
1 parent 8a681ba commit e8f0dea
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vlib/v/checker/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
mut right_first_type := ast.void_type
for i, mut right in node.right {
if right in [ast.CallExpr, ast.IfExpr, ast.LockExpr, ast.MatchExpr, ast.DumpExpr,
ast.SelectorExpr, ast.ParExpr, ast.ComptimeCall] {
ast.SelectorExpr, ast.ParExpr, ast.ComptimeCall, ast.InfixExpr] {
if right in [ast.IfExpr, ast.MatchExpr] && node.left.len == node.right.len && !is_decl
&& node.left[i] in [ast.Ident, ast.SelectorExpr] && !node.left[i].is_blank_ident() {
mut expr := node.left[i]
Expand Down
62 changes: 62 additions & 0 deletions vlib/v/tests/comptime/comptime_infix_assign_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
enum Flag {
usa_old_glory
all_other_bad_excuses_for_a_flag
}

struct Test {
is_foo bool
name [5]u8
}

fn enc[T](item T) string {
$if T is $int {
len := match typeof(item).name {
'i8', 'u8' { u8(2) }
'i16', 'u16' { 4 }
'int', 'u32', 'i32' { 8 }
'i64', 'u64' { 16 }
else { return '' }
}
return u64_to_hex(item, len)
} $else $if T is $array {
mut hex := ''
for val in item {
hex += enc(val)
}
return hex
} $else $if T is $struct {
mut hex := ''
$for field in T.fields {
hex += enc(item.$(field.name)) + '_'
}
return hex
} $else {
if typeof(item).name == 'bool' {
return enc(int(item))
}
$if debug {
println('cannot encode ${T}(s)')
}
return ''
}
}

@[direct_array_access; inline]
fn u64_to_hex(nn u64, len u8) string {
mut n := nn
mut buf := [17]u8{}
buf[len] = 0
mut i := 0
for i = len - 1; i >= 0; i-- {
d := u8(n & 0xF)
buf[i] = if d < 10 { d + `0` } else { d + 87 }
n = n >> 4
}
return unsafe { tos(memdup(&buf[0], len + 1), len) }
}

fn test_main() {
assert enc(Test{}) == '00000000_0000000000_'
assert enc(Test{ is_foo: true }) == '00000001_0000000000_'
assert enc(Test{ name: [u8(1), 2, 3, 4, 5]! }) == '00000000_0102030405_'
}

0 comments on commit e8f0dea

Please sign in to comment.