Skip to content

Commit a321ef6

Browse files
authored
cgen: fix assert expr handling + print with comptime smartcast (#20399)
1 parent b35988d commit a321ef6

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

vlib/v/gen/c/assert.v

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@ fn (mut g Gen) assert_stmt(original_assert_statement ast.AssertStmt) {
1717
}
1818
mut node := original_assert_statement
1919
g.writeln('// assert')
20+
21+
mut save_left := ast.empty_expr
22+
mut save_right := ast.empty_expr
23+
2024
if mut node.expr is ast.InfixExpr {
2125
if subst_expr := g.assert_subexpression_to_ctemp(node.expr.left, node.expr.left_type) {
26+
save_left = node.expr.left
2227
node.expr.left = subst_expr
2328
}
2429
if subst_expr := g.assert_subexpression_to_ctemp(node.expr.right, node.expr.right_type) {
30+
save_right = node.expr.right
2531
node.expr.right = subst_expr
2632
}
2733
}
@@ -56,6 +62,15 @@ fn (mut g Gen) assert_stmt(original_assert_statement ast.AssertStmt) {
5662
g.gen_assert_postfailure_mode(node)
5763
g.writeln('}')
5864
}
65+
66+
if mut node.expr is ast.InfixExpr {
67+
if node.expr.left is ast.CTempVar {
68+
node.expr.left = save_left
69+
}
70+
if node.expr.right is ast.CTempVar {
71+
node.expr.right = save_right
72+
}
73+
}
5974
}
6075

6176
struct UnsupportedAssertCtempTransform {

vlib/v/gen/c/fn.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,8 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
18161816
cast_sym := g.table.sym(typ)
18171817
if cast_sym.info is ast.Aggregate {
18181818
typ = cast_sym.info.types[g.aggregate_type_idx]
1819+
} else if expr.obj.ct_type_var == .smartcast {
1820+
typ = g.unwrap_generic(g.comptime.get_comptime_var_type(expr))
18191821
}
18201822
}
18211823
// handling println( var or { ... })
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
pub type Any = []Any
2+
| bool
3+
| f32
4+
| f64
5+
| i16
6+
| i64
7+
| i8
8+
| int
9+
| map[string]Any
10+
| string
11+
| u16
12+
| u32
13+
| u64
14+
| u8
15+
16+
fn test_main() {
17+
ana := Any([Any('')])
18+
name(ana)
19+
match ana {
20+
[]Any {
21+
for i := 0; i < ana.len; i++ {
22+
name(ana[i])
23+
}
24+
}
25+
else {
26+
assert false
27+
}
28+
}
29+
assert true
30+
}
31+
32+
fn name[T](val T) {
33+
$for v in val.variants {
34+
if val is v {
35+
dump(val.str())
36+
$if val is []Any {
37+
assert val.str() == "[Any('')]"
38+
} $else {
39+
assert val.str() == ''
40+
}
41+
dump(val)
42+
println(val)
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)