alex

/

v Public
0 Issues 1 Contributor 0 Releases 4 Branches
Additions: 75 Deletions: 16 View patch
1 'do_not_remove',
2 'vlib/v/tests/const_fixed_array_containing_references_to_itself_test.v', // error C2099: initializer is not a constant
3 'vlib/v/tests/const_and_global_with_same_name_test.v', // error C2099: initializer is not a constant
4+ 'vlib/v/tests/sumtype_as_cast_test.v', // error: Compound statement expression cannot support
5 ]
6 skip_on_windows = [
7 'do_not_remove',
8
1 mut expr_type_sym := g.table.sym(g.unwrap_generic(node.expr_type))
2 if mut expr_type_sym.info is ast.SumType {
3 dot := if node.expr_type.is_ptr() { '->' } else { '.' }
4- if sym.info is ast.FnType {
5- g.write('/* as */ (${styp})__as_cast(')
6- } else {
7- g.write('/* as */ *(${styp}*)__as_cast(')
8+ $if !msvc {
9+ if node.expr is ast.CallExpr {
10+ tmp_var := g.new_tmp_var()
11+ expr_styp := g.typ(node.expr_type)
12+ g.write('({ ${expr_styp} ${tmp_var} = ')
13+ g.expr(node.expr)
14+ g.write('; ')
15+ if sym.info is ast.FnType {
16+ g.write('/* as */ (${styp})__as_cast(')
17+ } else {
18+ g.write('/* as */ *(${styp}*)__as_cast(')
19+ }
20+ g.write(tmp_var)
21+ g.write(dot)
22+ g.write('_${sym.cname},')
23+ g.write(tmp_var)
24+ g.write(dot)
25+ sidx := g.type_sidx(unwrapped_node_typ)
26+ g.write('_typ, ${sidx}); }) /*expected idx: ${sidx}, name: ${sym.name} */ ')
27+ } else {
28+ if sym.info is ast.FnType {
29+ g.write('/* as */ (${styp})__as_cast(')
30+ } else {
31+ g.write('/* as */ *(${styp}*)__as_cast(')
32+ }
33+ g.write('(')
34+ g.expr(node.expr)
35+ g.write(')')
36+ g.write(dot)
37+ g.write('_${sym.cname},')
38+ g.write('(')
39+ g.expr(node.expr)
40+ g.write(')')
41+ g.write(dot)
42+ // g.write('typ, /*expected:*/$node.typ)')
43+ sidx := g.type_sidx(unwrapped_node_typ)
44+ g.write('_typ, ${sidx}) /*expected idx: ${sidx}, name: ${sym.name} */ ')
45+ }
46+ } $else {
47+ if sym.info is ast.FnType {
48+ g.write('/* as */ (${styp})__as_cast(')
49+ } else {
50+ g.write('/* as */ *(${styp}*)__as_cast(')
51+ }
52+ g.write('(')
53+ g.expr(node.expr)
54+ g.write(')')
55+ g.write(dot)
56+ g.write('_${sym.cname},')
57+ g.write('(')
58+ g.expr(node.expr)
59+ g.write(')')
60+ g.write(dot)
61+ // g.write('typ, /*expected:*/$node.typ)')
62+ sidx := g.type_sidx(unwrapped_node_typ)
63+ g.write('_typ, ${sidx}) /*expected idx: ${sidx}, name: ${sym.name} */ ')
64 }
65- g.write('(')
66- g.expr(node.expr)
67- g.write(')')
68- g.write(dot)
69- g.write('_${sym.cname},')
70- g.write('(')
71- g.expr(node.expr)
72- g.write(')')
73- g.write(dot)
74- // g.write('typ, /*expected:*/$node.typ)')
75- sidx := g.type_sidx(unwrapped_node_typ)
76- g.write('_typ, ${sidx}) /*expected idx: ${sidx}, name: ${sym.name} */ ')
77
78 // fill as cast name table
79 for variant in expr_type_sym.info.variants {
80
1new file mode 100644
2+type Sum = int | string
3+
4+struct Count {
5+mut:
6+ count int
7+}
8+
9+fn (mut c Count) ret_sum() Sum {
10+ c.count++
11+ return c.count
12+}
13+
14+fn test_sumtype_as_cast() {
15+ mut cnt := Count{22}
16+ _ := cnt.ret_sum() as int
17+ println(cnt)
18+ assert cnt.count == 23
19+}
20