1 ## oils_failures_allowed: 1
2 ## compare_shells: bash dash mksh
3
4 #### Locals don't leak
5 f() {
6 local f_var=f_var
7 }
8 f
9 echo $f_var
10 ## stdout:
11
12 #### Globals leak
13 f() {
14 f_var=f_var
15 }
16 f
17 echo $f_var
18 ## stdout: f_var
19
20 #### Return statement
21 f() {
22 echo one
23 return 42
24 echo two
25 }
26 f
27 ## stdout: one
28 ## status: 42
29
30 #### Dynamic Scope
31 f() {
32 echo $g_var
33 }
34 g() {
35 local g_var=g_var
36 f
37 }
38 g
39 ## stdout: g_var
40
41 #### Dynamic Scope Mutation (wow this is bad)
42 f() {
43 g_var=f_mutation
44 }
45 g() {
46 local g_var=g_var
47 echo "g_var=$g_var"
48 f
49 echo "g_var=$g_var"
50 }
51 g
52 echo g_var=$g_var
53 ## STDOUT:
54 g_var=g_var
55 g_var=f_mutation
56 g_var=
57 ## END
58
59 #### Assign local separately
60 f() {
61 local f
62 f='new-value'
63 echo "[$f]"
64 }
65 f
66 ## stdout: [new-value]
67 ## status: 0
68
69 #### Assign a local and global on same line
70 myglobal=
71 f() {
72 local mylocal
73 mylocal=L myglobal=G
74 echo "[$mylocal $myglobal]"
75 }
76 f
77 echo "[$mylocal $myglobal]"
78 ## stdout-json: "[L G]\n[ G]\n"
79 ## status: 0
80
81 #### Return without args gives previous
82 f() {
83 ( exit 42 )
84 return
85 }
86 f
87 echo status=$?
88 ## STDOUT:
89 status=42
90 ## END
91
92 #### return "" (a lot of disagreement)
93 f() {
94 echo f
95 return ""
96 }
97
98 f
99 echo status=$?
100 ## STDOUT:
101 f
102 status=0
103 ## END
104 ## status: 0
105
106 ## OK dash status: 2
107 ## OK dash STDOUT:
108 f
109 ## END
110
111 ## BUG mksh STDOUT:
112 f
113 status=1
114 ## END
115
116 ## BUG bash STDOUT:
117 f
118 status=2
119 ## END
120
121 #### return $empty
122 f() {
123 echo f
124 empty=
125 return $empty
126 }
127
128 f
129 echo status=$?
130 ## STDOUT:
131 f
132 status=0
133 ## END
134
135 #### Subshell function
136
137 f() ( return 42; )
138 # BUG: OSH raises invalid control flow! I think we should just allow 'return'
139 # but maybe not 'break' etc.
140 g() ( return 42 )
141 # bash warns here but doesn't cause an error
142 # g() ( break )
143
144 f
145 echo status=$?
146 g
147 echo status=$?
148
149 ## STDOUT:
150 status=42
151 status=42
152 ## END