vxx / vlib / v / gen / c / testdata / autofree_map_if_expr.vv
90 lines · 86 sloc · 2.03 KB · 775b8f94fd8c1d1aec431624f4a1bd2ac0dc9845
Raw
1// vtest vflags: -autofree
2
3fn mark(s string) string {
4 return s + '!'
5}
6
7fn accepts(s string) bool {
8 return s.len > 0
9}
10
11fn side_effect() string {
12 println('side effect called')
13 return 'x'
14}
15
16fn main() {
17 suffix := 'tcc/lib/libgc.dylib'
18 flags := ['"thirdparty/tcc/lib/libgc.dylib"', 'other'].map(if it.ends_with(suffix + '"')
19 && it.starts_with('"') {
20 '"/tmp/libgc.dylib"'
21 } else {
22 it
23 })
24 println(flags)
25 branch_flags := ['a', 'b'].map(if it == 'a' {
26 mark(it + 'x')
27 } else {
28 it
29 })
30 println(branch_flags)
31 anon_flags := ['ax', 'b'].map(fn (it string) string {
32 suffix := 'x'
33 if it.ends_with(suffix + '') {
34 return mark(it)
35 }
36 return it
37 })
38 println(anon_flags)
39 anon_short_flags := ['a'].map(fn (it string) string {
40 if true && accepts(it + '') {
41 return mark(it)
42 }
43 return it
44 })
45 println(anon_short_flags)
46 nested_suffix := '!'
47 nested_flags := [['a'], ['b']].map(it.map(fn [nested_suffix] (s string) string {
48 return mark(s + nested_suffix)
49 })[0])
50 println(nested_flags)
51 needle := 'x'
52 nested_any_flags := [['ax'], ['b']].map(if it.any(it.ends_with(needle + '')) {
53 'hit'
54 } else {
55 'miss'
56 })
57 println(nested_any_flags)
58 nested_filter_counts := [['ax'], ['b']].map(it.filter(it.ends_with(needle + '')).len)
59 println(nested_filter_counts)
60 nested_counts := [['ax'], ['b']].map(it.count(it.ends_with(needle + '')))
61 println(nested_counts)
62 nested_all_flags := [['ax'], ['b']].map(it.all(it.ends_with(needle + '')))
63 println(nested_all_flags)
64 short_flags := ['a'].map(if false && accepts(side_effect() + '') {
65 'bad'
66 } else {
67 it
68 })
69 println(short_flags)
70 evaluated_short_flags := ['a'].map(if true && accepts(it + '') {
71 mark(it)
72 } else {
73 it
74 })
75 println(evaluated_short_flags)
76 receiver_short_flags := ['a'].map(if false && (side_effect() + '').starts_with('x') {
77 'bad'
78 } else {
79 it
80 })
81 println(receiver_short_flags)
82 evaluated_receiver_flags := ['a'].map(if true && (it + '').starts_with('a') {
83 mark(it)
84 } else {
85 it
86 })
87 println(evaluated_receiver_flags)
88 // This regression only needs the generated C to compile.
89 exit(0)
90}
91